Skip to content

Commit 6287ffd

Browse files
committed
feat(DatePicker, DateRangePicker): add the ability to set custom name attributes for input elements
1 parent 86d31a4 commit 6287ffd

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

docs/content/forms/date-picker.md

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ const datePickerList = datePickerElementList.map(datePickerEl => {
234234
| `locale` | string | `'default'` | Sets the default locale for components. If not set, it is inherited from the navigator.language. |
235235
| `maxDate` | date \| string \| null | `null` | Max selectable date. |
236236
| `minDate` | date \| string \| null | `null` | Min selectable date. |
237+
| `name` | string | `null` | Set the name attribute for the input element. |
237238
| `placeholder` | string | `'Select time'` | Specifies a short hint that is visible in the input. |
238239
| `selectAdjacementDays` | boolean | `false` | Set whether days in adjacent months shown before or after the current month are selectable. This only applies if the `showAdjacementDays` option is set to true. |
239240
| `showAdjacementDays` | boolean | `true` | Set whether to display dates in adjacent months (non-selectable) at the start and end of the current month. |

docs/content/forms/date-range-picker.md

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ const dateRangePickerList = dateRangePickerElementList.map(dateRangePickerEl =>
294294
| `disabled` | boolean | `false` | Toggle the disabled state for the component. |
295295
| `disabledDates` | array \| null | `null` | Specify the list of dates that cannot be selected. |
296296
| `endDate` | date \| string \| null | `null` | Initial selected to date (range). |
297+
| `endName` | string | `null` | Set the name attribute for the end date input element. |
297298
| `firstDayOfWeek` | number | `1` | <p>Sets the day of start week.</p> <ul><li>`0` - Sunday</li><li>`1` - Monday</li><li>`2` - Tuesday</li><li>`3` - Wednesday</li><li>`4` - Thursday</li><li>`5` - Friday</li><li>`6` - Saturday</li></ul> |
298299
| `footer` | boolean | `false` | Toggle visibility of footer element. |
299300
| `format` | string | | Set date format. We use date-fns to format dates. Visit <a href="https://date-fns.org/v2.28.0/docs/format" target="_blank" rel="nofollow">https://date-fns.org/v2.28.0/docs/format</a> to check accepted patterns. |
@@ -311,6 +312,7 @@ const dateRangePickerList = dateRangePickerElementList.map(dateRangePickerEl =>
311312
| `showAdjacementDays` | boolean | `true` | Set whether to display dates in adjacent months (non-selectable) at the start and end of the current month. |
312313
| `size` | `'sm'` \| `'lg'` | `null` | Size the component small or large. |
313314
| `startDate` | date \| string \| null | `null` | Initial selected date. |
315+
| `startName` | string | `null` | Set the name attribute for the start date input element. |
314316
| `timepicker` | boolean | `false` | Provide an additional time selection by adding select boxes to choose times. |
315317
| `todayButton` | string | `'Today'` | Today button inner HTML |
316318
| `todayButtonClasses` | array \| string | `['btn', 'btn-sm', 'me-2']` | CSS class names that will be added to the today button |

js/src/date-range-picker.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const Default = {
8181
disabled: false,
8282
disabledDates: null,
8383
endDate: null,
84+
endName: null,
8485
firstDayOfWeek: 1,
8586
footer: false,
8687
format: null,
@@ -89,6 +90,7 @@ const Default = {
8990
locale: 'default',
9091
maxDate: null,
9192
minDate: null,
93+
name: null,
9294
placeholder: ['Start date', 'End date'],
9395
range: true,
9496
ranges: {},
@@ -97,6 +99,7 @@ const Default = {
9799
separator: true,
98100
size: null,
99101
startDate: null,
102+
startName: null,
100103
selectAdjacementDays: false,
101104
selectEndDate: false,
102105
showAdjacementDays: true,
@@ -118,6 +121,7 @@ const DefaultType = {
118121
disabledDates: '(array|null)',
119122
disabled: 'boolean',
120123
endDate: '(date|string|null)',
124+
endName: 'string',
121125
firstDayOfWeek: 'number',
122126
footer: 'boolean',
123127
format: '(string|null)',
@@ -126,6 +130,7 @@ const DefaultType = {
126130
locale: 'string',
127131
maxDate: '(date|string|null)',
128132
minDate: '(date|string|null)',
133+
name: 'string',
129134
placeholder: '(array|string)',
130135
range: 'boolean',
131136
ranges: 'object',
@@ -134,6 +139,7 @@ const DefaultType = {
134139
separator: 'boolean',
135140
size: '(string|null)',
136141
startDate: '(date|string|null)',
142+
startName: 'string',
137143
selectAdjacementDays: 'boolean',
138144
selectEndDate: 'boolean',
139145
showAdjacementDays: 'boolean',
@@ -445,8 +451,21 @@ class DateRangePicker extends BaseComponent {
445451
const inputGroupEl = document.createElement('div')
446452
inputGroupEl.classList.add(CLASS_NAME_INPUT_GROUP)
447453

448-
const startInputEl = this._createInput(this._config.range ? 'date-range-picker-start-date' : 'date-picker', this._getPlaceholder()[0], this._setInputValue(this._startDate))
449-
const endInputEl = this._createInput('date-range-picker-end-date', this._getPlaceholder()[1], this._setInputValue(this._endDate))
454+
let startInputName = null
455+
456+
if (this._config.name || this._config.startName || this._element.id) {
457+
startInputName = this._config.name || this._config.startName || (this._config.range ? `date-range-picker-start-date-${this._element.id}` : `date-picker-${this._element.id}`)
458+
}
459+
460+
const startInputEl = this._createInput(startInputName, this._getPlaceholder()[0], this._setInputValue(this._startDate))
461+
462+
let endInputName = null
463+
464+
if (this._config.endName || this._element.id) {
465+
endInputName = this._config.endName || `date-range-picker-end-date-${this._element.id}`
466+
}
467+
468+
const endInputEl = this._createInput(endInputName, this._getPlaceholder()[1], this._setInputValue(this._endDate))
450469

451470
const inputGroupTextSeparatorEl = document.createElement('div')
452471
inputGroupTextSeparatorEl.classList.add(CLASS_NAME_SEPARATOR)
@@ -717,8 +736,8 @@ class DateRangePicker extends BaseComponent {
717736
inputEl.type = 'text'
718737
inputEl.value = value
719738

720-
if (this._element.id) {
721-
inputEl.name = `${name}-${this._element.id}`
739+
if (name) {
740+
inputEl.name = name
722741
}
723742

724743
const events = ['change', 'keyup', 'paste']

0 commit comments

Comments
 (0)