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 | 50 | :value="dates" |
| 51 | 51 | :format="format" |
| 52 | 52 | :time-disabled="timeDisabled" |
| 53 | + :disabled-date="disabledDate" | |
| 53 | 54 | v-bind="timePickerOptions" |
| 54 | 55 | @on-pick="handlePick" |
| 55 | 56 | @on-pick-click="handlePickClick" |
| ... | ... | @@ -150,6 +151,13 @@ |
| 150 | 151 | currentView (currentView) { |
| 151 | 152 | this.$emit('on-selection-mode-change', currentView); |
| 152 | 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 | 162 | selectionMode(type){ |
| 155 | 163 | this.currentView = type; |
| ... | ... | @@ -186,6 +194,7 @@ |
| 186 | 194 | else if (selectionMode === 'month') value = new Date(panelDate.getFullYear(), value.getMonth(), 1); |
| 187 | 195 | else value = new Date(value); |
| 188 | 196 | |
| 197 | + this.dates = [value]; | |
| 189 | 198 | this.$emit('on-pick', value); |
| 190 | 199 | }, |
| 191 | 200 | }, | ... | ... |
src/components/date-picker/panel/Time/time.vue
| ... | ... | @@ -7,12 +7,12 @@ |
| 7 | 7 | ref="timeSpinner" |
| 8 | 8 | :show-seconds="showSeconds" |
| 9 | 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 | 16 | :hide-disabled-options="hideDisabledOptions" |
| 17 | 17 | @on-change="handleChange" |
| 18 | 18 | @on-pick-click="handlePickClick"></time-spinner> |
| ... | ... | @@ -39,12 +39,25 @@ |
| 39 | 39 | const timePrefixCls = 'ivu-time-picker'; |
| 40 | 40 | |
| 41 | 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 | 52 | export default { |
| 44 | 53 | name: 'TimePickerPanel', |
| 45 | 54 | mixins: [ Mixin, Locale, Options ], |
| 46 | 55 | components: { TimeSpinner, Confirm }, |
| 47 | 56 | props: { |
| 57 | + disabledDate: { | |
| 58 | + type: Function, | |
| 59 | + default: returnFalse | |
| 60 | + }, | |
| 48 | 61 | steps: { |
| 49 | 62 | type: Array, |
| 50 | 63 | default: () => [] |
| ... | ... | @@ -76,6 +89,35 @@ |
| 76 | 89 | const tYear = this.t('i.datepicker.year'); |
| 77 | 90 | const tMonth = this.t(`i.datepicker.month${month}`); |
| 78 | 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 | 123 | watch: { | ... | ... |