Skip to content

Commit

Permalink
fix: allow both v-model & value+change for datepickers
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Jun 12, 2024
1 parent 0d4edfb commit 770ce56
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
25 changes: 17 additions & 8 deletions src/components/DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
type="text"
icon-left="calendar"
:placeholder="placeholder"
:value="modelValue && formatter ? formatter(modelValue) : modelValue"
:value="dateValue && formatter ? formatter(dateValue) : dateValue"
@focus="!readonly ? togglePopover() : null"
class="w-full"
:class="inputClass"
Expand Down Expand Up @@ -43,7 +43,7 @@
<TextInput
class="text-sm"
type="text"
:value="modelValue"
:value="dateValue"
@change="
selectDate(getDate($event.target.value)) || togglePopover()
"
Expand Down Expand Up @@ -80,7 +80,7 @@
'font-extrabold text-gray-900':
toValue(date) === toValue(today),
'bg-gray-800 text-white hover:bg-gray-800':
toValue(date) === modelValue,
toValue(date) === dateValue,
}"
@click="
() => {
Expand Down Expand Up @@ -118,8 +118,15 @@ import FeatherIcon from './FeatherIcon.vue'
import TextInput from './TextInput.vue'
export default {
name: 'DatePicker',
props: ['modelValue', 'placeholder', 'formatter', 'readonly', 'inputClass'],
emits: ['update:modelValue'],
props: [
'value',
'modelValue',
'placeholder',
'formatter',
'readonly',
'inputClass',
],
emits: ['update:modelValue', 'change'],
components: {
Popover,
Input,
Expand Down Expand Up @@ -185,15 +192,17 @@ export default {
})
return `${month}, ${date.getFullYear()}`
},
dateValue() {
return this.value ? this.value : this.modelValue
},
},
methods: {
selectDate(date) {
this.$emit('change', this.toValue(date))
this.$emit('update:modelValue', this.toValue(date))
},
selectCurrentMonthYear() {
let date = this.modelValue
? this.getDate(this.modelValue)
: this.getDate()
let date = this.dateValue ? this.getDate(this.dateValue) : this.getDate()
if (date === 'Invalid Date') {
date = this.getDate()
}
Expand Down
17 changes: 11 additions & 6 deletions src/components/DateRangePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
type="text"
icon-left="calendar"
:placeholder="placeholder"
:value="modelValue && formatter ? formatDates(modelValue) : modelValue"
:value="dateValue && formatter ? formatDates(dateValue) : dateValue"
@focus="!readonly ? togglePopover() : null"
class="w-full"
:class="inputClass"
Expand Down Expand Up @@ -108,7 +108,7 @@ import TextInput from './TextInput.vue'
export default {
name: 'DateRangePicker',
props: ['modelValue', 'placeholder', 'formatter', 'readonly', 'inputClass'],
emits: ['update:modelValue'],
emits: ['update:modelValue', 'change'],
components: {
Popover,
Input,
Expand All @@ -117,8 +117,8 @@ export default {
TextInput,
},
data() {
const fromDate = this.modelValue ? this.modelValue[0] : ''
const toDate = this.modelValue ? this.modelValue[1] : ''
const fromDate = this.dateValue ? this.dateValue[0] : ''
const toDate = this.dateValue ? this.dateValue[1] : ''
return {
currentYear: null,
currentMonth: null,
Expand Down Expand Up @@ -178,6 +178,9 @@ export default {
})
return `${month}, ${date.getFullYear()}`
},
dateValue() {
return this.value ? this.value : this.modelValue
},
},
methods: {
handleDateClick(date) {
Expand All @@ -192,10 +195,12 @@ export default {
this.swapDatesIfNecessary()
},
selectDates() {
let val = `${this.fromDate},${this.toDate}`
if (!this.fromDate && !this.toDate) {
return this.$emit('update:modelValue', '')
val = ''
}
this.$emit('update:modelValue', `${this.fromDate},${this.toDate}`)
this.$emit('change', val)
this.$emit('update:modelValue', val)
},
swapDatesIfNecessary() {
if (!this.fromDate || !this.toDate) {
Expand Down
34 changes: 21 additions & 13 deletions src/components/DateTimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
type="text"
icon-left="calendar"
:placeholder="placeholder"
:value="modelValue && formatter ? formatter(modelValue) : modelValue"
:value="dateValue && formatter ? formatter(dateValue) : dateValue"
@focus="!readonly ? togglePopover() : null"
class="w-full"
:class="inputClass"
Expand Down Expand Up @@ -43,7 +43,7 @@
<TextInput
class="text-sm"
type="text"
:value="modelValue"
:value="dateValue"
@change="updateDate($event.target.value) || togglePopover()"
/>
<Button
Expand Down Expand Up @@ -78,7 +78,7 @@
'font-extrabold text-gray-900':
toValue(date) === toValue(today),
'bg-gray-800 text-white hover:bg-gray-800':
toValue(date) === modelValue,
toValue(date) === dateValue,
}"
@click="
() => {
Expand Down Expand Up @@ -157,8 +157,15 @@ import FeatherIcon from './FeatherIcon.vue'
import TextInput from './TextInput.vue'
export default {
name: 'DatePicker',
props: ['modelValue', 'placeholder', 'formatter', 'readonly', 'inputClass'],
emits: ['update:modelValue'],
props: [
'value',
'modelValue',
'placeholder',
'formatter',
'readonly',
'inputClass',
],
emits: ['update:modelValue', 'change'],
components: {
Popover,
Input,
Expand Down Expand Up @@ -227,30 +234,31 @@ export default {
})
return `${month}, ${date.getFullYear()}`
},
dateValue() {
return this.value ? this.value : this.modelValue
},
},
methods: {
changeTime() {
let date = this.modelValue
? this.getDate(this.modelValue)
: this.getDate()
let date = this.dateValue ? this.getDate(this.dateValue) : this.getDate()
this.selectDate(date, true)
},
selectDate(date, isTimeChange = false, isNow = false) {
if (!isTimeChange) {
let currentDate =
this.modelValue && !isNow
? this.getDate(this.modelValue)
this.dateValue && !isNow
? this.getDate(this.dateValue)
: this.getDate()
this.hour = currentDate.getHours()
this.minute = currentDate.getMinutes()
this.second = currentDate.getSeconds()
}
this.$emit('change', this.toValue(date))
this.$emit('update:modelValue', this.toValue(date))
},
selectCurrentMonthYear() {
let date = this.modelValue
? this.getDate(this.modelValue)
: this.getDate()
let date = this.dateValue ? this.getDate(this.dateValue) : this.getDate()
if (date === 'Invalid Date') {
date = this.getDate()
}
Expand Down

0 comments on commit 770ce56

Please sign in to comment.