Commit eeeceb54462bea5126881935f40b7ff72daa77a5
Committed by
GitHub
Merge pull request #3314 from SergioCrisostomo/fix-3246
Allow disableDate function to limit time-spinner numbers
Showing
2 changed files
with
57 additions
and
6 deletions
Show diff stats
src/components/date-picker/panel/Date/date.vue
| @@ -50,6 +50,7 @@ | @@ -50,6 +50,7 @@ | ||
| 50 | :value="dates" | 50 | :value="dates" |
| 51 | :format="format" | 51 | :format="format" |
| 52 | :time-disabled="timeDisabled" | 52 | :time-disabled="timeDisabled" |
| 53 | + :disabled-date="disabledDate" | ||
| 53 | v-bind="timePickerOptions" | 54 | v-bind="timePickerOptions" |
| 54 | @on-pick="handlePick" | 55 | @on-pick="handlePick" |
| 55 | @on-pick-click="handlePickClick" | 56 | @on-pick-click="handlePickClick" |
| @@ -150,6 +151,13 @@ | @@ -150,6 +151,13 @@ | ||
| 150 | currentView (currentView) { | 151 | currentView (currentView) { |
| 151 | this.$emit('on-selection-mode-change', currentView); | 152 | this.$emit('on-selection-mode-change', currentView); |
| 152 | this.pickertable = this.getTableType(currentView); | 153 | this.pickertable = this.getTableType(currentView); |
| 154 | + | ||
| 155 | + if (this.currentView === 'time') { | ||
| 156 | + this.$nextTick(() => { | ||
| 157 | + const spinner = this.$refs.timePicker.$refs.timeSpinner; | ||
| 158 | + spinner.updateScroll(); | ||
| 159 | + }); | ||
| 160 | + } | ||
| 153 | }, | 161 | }, |
| 154 | selectionMode(type){ | 162 | selectionMode(type){ |
| 155 | this.currentView = type; | 163 | this.currentView = type; |
| @@ -186,6 +194,7 @@ | @@ -186,6 +194,7 @@ | ||
| 186 | else if (selectionMode === 'month') value = new Date(panelDate.getFullYear(), value.getMonth(), 1); | 194 | else if (selectionMode === 'month') value = new Date(panelDate.getFullYear(), value.getMonth(), 1); |
| 187 | else value = new Date(value); | 195 | else value = new Date(value); |
| 188 | 196 | ||
| 197 | + this.dates = [value]; | ||
| 189 | this.$emit('on-pick', value); | 198 | this.$emit('on-pick', value); |
| 190 | }, | 199 | }, |
| 191 | }, | 200 | }, |
src/components/date-picker/panel/Time/time.vue
| @@ -7,12 +7,12 @@ | @@ -7,12 +7,12 @@ | ||
| 7 | ref="timeSpinner" | 7 | ref="timeSpinner" |
| 8 | :show-seconds="showSeconds" | 8 | :show-seconds="showSeconds" |
| 9 | :steps="steps" | 9 | :steps="steps" |
| 10 | - :hours="value[0] && date.getHours()" | ||
| 11 | - :minutes="value[0] && date.getMinutes()" | ||
| 12 | - :seconds="value[0] && date.getSeconds()" | ||
| 13 | - :disabled-hours="disabledHours" | ||
| 14 | - :disabled-minutes="disabledMinutes" | ||
| 15 | - :disabled-seconds="disabledSeconds" | 10 | + :hours="timeSlots[0]" |
| 11 | + :minutes="timeSlots[1]" | ||
| 12 | + :seconds="timeSlots[2]" | ||
| 13 | + :disabled-hours="disabledHMS.disabledHours" | ||
| 14 | + :disabled-minutes="disabledHMS.disabledMinutes" | ||
| 15 | + :disabled-seconds="disabledHMS.disabledSeconds" | ||
| 16 | :hide-disabled-options="hideDisabledOptions" | 16 | :hide-disabled-options="hideDisabledOptions" |
| 17 | @on-change="handleChange" | 17 | @on-change="handleChange" |
| 18 | @on-pick-click="handlePickClick"></time-spinner> | 18 | @on-pick-click="handlePickClick"></time-spinner> |
| @@ -39,12 +39,25 @@ | @@ -39,12 +39,25 @@ | ||
| 39 | const timePrefixCls = 'ivu-time-picker'; | 39 | const timePrefixCls = 'ivu-time-picker'; |
| 40 | 40 | ||
| 41 | const capitalize = (str) => str[0].toUpperCase() + str.slice(1); | 41 | const capitalize = (str) => str[0].toUpperCase() + str.slice(1); |
| 42 | + const mergeDateHMS = (date, hours, minutes, seconds) => { | ||
| 43 | + const newDate = new Date(date.getTime()); | ||
| 44 | + newDate.setHours(hours); | ||
| 45 | + newDate.setMinutes(minutes); | ||
| 46 | + newDate.setSeconds(seconds); | ||
| 47 | + return newDate; | ||
| 48 | + }; | ||
| 49 | + const unique = (el, i, arr) => arr.indexOf(el) === i; | ||
| 50 | + const returnFalse = () => false; | ||
| 42 | 51 | ||
| 43 | export default { | 52 | export default { |
| 44 | name: 'TimePickerPanel', | 53 | name: 'TimePickerPanel', |
| 45 | mixins: [ Mixin, Locale, Options ], | 54 | mixins: [ Mixin, Locale, Options ], |
| 46 | components: { TimeSpinner, Confirm }, | 55 | components: { TimeSpinner, Confirm }, |
| 47 | props: { | 56 | props: { |
| 57 | + disabledDate: { | ||
| 58 | + type: Function, | ||
| 59 | + default: returnFalse | ||
| 60 | + }, | ||
| 48 | steps: { | 61 | steps: { |
| 49 | type: Array, | 62 | type: Array, |
| 50 | default: () => [] | 63 | default: () => [] |
| @@ -76,6 +89,35 @@ | @@ -76,6 +89,35 @@ | ||
| 76 | const tYear = this.t('i.datepicker.year'); | 89 | const tYear = this.t('i.datepicker.year'); |
| 77 | const tMonth = this.t(`i.datepicker.month${month}`); | 90 | const tMonth = this.t(`i.datepicker.month${month}`); |
| 78 | return `${date.getFullYear()}${tYear} ${tMonth}`; | 91 | return `${date.getFullYear()}${tYear} ${tMonth}`; |
| 92 | + }, | ||
| 93 | + timeSlots(){ | ||
| 94 | + if (!this.value[0]) return []; | ||
| 95 | + return ['getHours', 'getMinutes', 'getSeconds'].map(slot => this.date[slot]()); | ||
| 96 | + }, | ||
| 97 | + disabledHMS(){ | ||
| 98 | + const disabledTypes = ['disabledHours', 'disabledMinutes', 'disabledSeconds']; | ||
| 99 | + if (this.disabledDate === returnFalse || !this.value[0]) { | ||
| 100 | + const disabled = disabledTypes.reduce( | ||
| 101 | + (obj, type) => (obj[type] = this[type], obj), {} | ||
| 102 | + ); | ||
| 103 | + return disabled; | ||
| 104 | + } else { | ||
| 105 | + const slots = [24, 60, 60]; | ||
| 106 | + const disabled = ['Hours', 'Minutes', 'Seconds'].map(type => this[`disabled${type}`]); | ||
| 107 | + const disabledHMS = disabled.map((preDisabled, j) => { | ||
| 108 | + const slot = slots[j]; | ||
| 109 | + const toDisable = preDisabled; | ||
| 110 | + for (let i = 0; i < slot; i+= (this.steps[j] || 1)){ | ||
| 111 | + const hms = this.timeSlots.map((slot, x) => x === j ? i : slot); | ||
| 112 | + const testDateTime = mergeDateHMS(this.date, ...hms); | ||
| 113 | + if (this.disabledDate(testDateTime, true)) toDisable.push(i); | ||
| 114 | + } | ||
| 115 | + return toDisable.filter(unique); | ||
| 116 | + }); | ||
| 117 | + return disabledTypes.reduce( | ||
| 118 | + (obj, type, i) => (obj[type] = disabledHMS[i], obj), {} | ||
| 119 | + ); | ||
| 120 | + } | ||
| 79 | } | 121 | } |
| 80 | }, | 122 | }, |
| 81 | watch: { | 123 | watch: { |