Skip to content

Commit 4899308

Browse files
authored
Merge pull request #517 from gselderslaghs/datepicker-date-type
(fix/enhancement) Datepicker date type #230
2 parents 7c17092 + e2bcc5c commit 4899308

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

sass/components/forms/_input-fields.scss

+24-1
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,27 @@ textarea {
317317
margin: 5px 15px;
318318
}
319319
}
320-
}
320+
}
321+
322+
/* Datepicker date input fields */
323+
.datepicker-date-input {
324+
position: relative;
325+
text-indent: -9999px;
326+
327+
&::after {
328+
display: block;
329+
position: absolute;
330+
top: 1.10rem;
331+
content: attr(data-date);
332+
color: var(--input-color);
333+
text-indent: 0;
334+
}
335+
336+
&:focus-visible {
337+
text-indent: 0;
338+
}
339+
340+
&:focus-visible:after {
341+
text-indent: -9999px;
342+
}
343+
}

src/datepicker.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ export class Datepicker extends Component<DatepickerOptions> {
301301
this.gotoDate(new Date());
302302
}
303303
this.isOpen = false;
304+
305+
// HTML5 input date field support
306+
if(this.el.type == 'date') {
307+
this.el.classList.add('datepicker-date-input');
308+
}
304309
}
305310

306311
static get defaults() {
@@ -417,6 +422,10 @@ export class Datepicker extends Component<DatepickerOptions> {
417422
if (typeof format === 'function') return format(this.date);
418423
if (!Datepicker._isDate(this.date)) return '';
419424
// String Format
425+
return this.formatDate(format);
426+
}
427+
428+
formatDate(format) {
420429
const formatArray = format.split(/(d{1,4}|m{1,4}|y{4}|yy|!.)/g);
421430
const formattedDate = formatArray
422431
.map(label => this.formats[label] ? this.formats[label]() : label)
@@ -458,11 +467,23 @@ export class Datepicker extends Component<DatepickerOptions> {
458467
}
459468
}
460469

470+
/**
471+
* Sets the data-date attribute on the date input field
472+
*/
473+
setDataDate() {
474+
this.el.setAttribute('data-date', this.toString())
475+
}
476+
461477
/**
462478
* Sets current date as the input value.
463479
*/
464480
setInputValue() {
465-
this.el.value = this.toString();
481+
if(this.el.type == 'date') {
482+
this.setDataDate()
483+
this.el.value = this.formatDate('yyyy-mm-dd')
484+
} else {
485+
this.el.value = this.toString();
486+
}
466487
this.el.dispatchEvent(new CustomEvent('change', {bubbles:true, cancelable:true, composed:true, detail: {firedBy: this}}));
467488
}
468489

@@ -925,7 +946,11 @@ export class Datepicker extends Component<DatepickerOptions> {
925946
this.calendarEl.removeEventListener('click', this._handleCalendarClick);
926947
}
927948

928-
_handleInputClick = () => {
949+
_handleInputClick = (e) => {
950+
// Prevents default browser datepicker modal rendering
951+
if(this.el.type == 'date') {
952+
e.preventDefault()
953+
}
929954
this.open();
930955
}
931956

@@ -1008,7 +1033,12 @@ export class Datepicker extends Component<DatepickerOptions> {
10081033
else {
10091034
date = new Date(Date.parse(this.el.value));
10101035
}
1011-
if (Datepicker._isDate(date)) this.setDate(date);
1036+
if (Datepicker._isDate(date)) {
1037+
this.setDate(date);
1038+
if (this.el.type == 'date') {
1039+
this.setDataDate();
1040+
}
1041+
}
10121042
}
10131043

10141044
renderDayName(opts, day, abbr: boolean = false) {

0 commit comments

Comments
 (0)