Skip to content

Commit 53f1e8f

Browse files
authored
feat(dateinput): format typeable inputs on blur (sumcumo#44)
* Add prop to determine first-day-of-week * Fix error with no. of blank days * Fix minor issues with first-day-of-week * Add first-day-of-week to demo examples * Fix typeable calendar not formatting correctly on blur * Fix typeable calendar input not being formatted on blur
1 parent 78abc56 commit 53f1e8f

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

docs/.vuepress/components/Demo.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
/>
3232
<div class="coding">
3333
<code>
34-
&lt;datepicker
35-
placeholder="Select Date"
36-
first-day-of-week="mon"&gt;&lt;/datepicker&gt;
34+
&lt;datepicker placeholder="Select Date" first-day-of-week="mon"&gt;&lt;/datepicker&gt;
3735
</code>
3836
<hr />
3937
<p>{{ vModelExample }}</p>

src/components/DateInput.vue

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ export default {
115115
if (this.typedDate.length) {
116116
return this.typedDate
117117
}
118+
return this.formattedDate
119+
},
120+
formattedDate() {
118121
return typeof this.format === 'function'
119122
? this.format(new Date(this.selectedDate))
120123
: this.utils.formatDate(new Date(this.selectedDate), this.format, this.translation)
@@ -136,15 +139,20 @@ export default {
136139
this.$emit('clear-date')
137140
},
138141
/**
139-
* nullify the typed date to defer to regular formatting
140-
* called once the input is blurred
142+
* Submits a typed date if it's valid
141143
*/
142144
inputBlurred() {
143-
const parsableDate = this.parseDate(this.input.value)
144-
if (this.typeable && Number.isNaN(Date.parse(parsableDate))) {
145-
this.clearDate()
146-
this.input.value = null
147-
this.typedDate = ''
145+
if (this.typeable) {
146+
const parsableDate = this.parseDate(this.input.value)
147+
const parsedDate = Date.parse(parsableDate)
148+
149+
if (Number.isNaN(parsedDate)) {
150+
this.clearDate()
151+
} else {
152+
this.input.value = this.formattedDate
153+
this.typedDate = ''
154+
this.$emit('typed-date', parsedDate)
155+
}
148156
}
149157
this.$emit('blur')
150158
this.$emit('close-calendar')

test/unit/specs/DateInput/typedDates.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,11 @@ describe('Datepicker mount', () => {
172172

173173
expect(new Date(wrapper.vm.pageDate).getMonth()).toBe(1)
174174
})
175+
176+
it('formats the date on blur', () => {
177+
const input = wrapper.find('input')
178+
input.element.value = '2018-04-24'
179+
input.trigger('blur')
180+
expect(input.element.value).toEqual('24 Apr 2018')
181+
})
175182
})

0 commit comments

Comments
 (0)