mirror of https://github.com/ElemeFE/element
date-time-picker: add selectableRange in pickerOptions
parent
f8cabcdbeb
commit
904e60ef1d
|
@ -158,7 +158,8 @@
|
||||||
nextMonth,
|
nextMonth,
|
||||||
changeYearMonthAndClampDate,
|
changeYearMonthAndClampDate,
|
||||||
extractDateFormat,
|
extractDateFormat,
|
||||||
extractTimeFormat
|
extractTimeFormat,
|
||||||
|
timeWithinRange
|
||||||
} from '../util';
|
} from '../util';
|
||||||
import Clickoutside from 'element-ui/src/utils/clickoutside';
|
import Clickoutside from 'element-ui/src/utils/clickoutside';
|
||||||
import Locale from 'element-ui/src/mixins/locale';
|
import Locale from 'element-ui/src/mixins/locale';
|
||||||
|
@ -222,13 +223,16 @@
|
||||||
const format = timeFormat => {this.$refs.timepicker.format = timeFormat;};
|
const format = timeFormat => {this.$refs.timepicker.format = timeFormat;};
|
||||||
const value = value => {this.$refs.timepicker.value = value;};
|
const value = value => {this.$refs.timepicker.value = value;};
|
||||||
const date = date => {this.$refs.timepicker.date = date;};
|
const date = date => {this.$refs.timepicker.date = date;};
|
||||||
|
const selectableRange = selectableRange => {this.$refs.timepicker.selectableRange = selectableRange;};
|
||||||
|
|
||||||
this.$watch('value', value);
|
this.$watch('value', value);
|
||||||
this.$watch('date', date);
|
this.$watch('date', date);
|
||||||
|
this.$watch('selectableRange', selectableRange);
|
||||||
|
|
||||||
format(this.timeFormat);
|
format(this.timeFormat);
|
||||||
value(this.value);
|
value(this.value);
|
||||||
date(this.date);
|
date(this.date);
|
||||||
|
selectableRange(this.selectableRange);
|
||||||
},
|
},
|
||||||
|
|
||||||
handleClear() {
|
handleClear() {
|
||||||
|
@ -333,9 +337,14 @@
|
||||||
|
|
||||||
handleDatePick(value) {
|
handleDatePick(value) {
|
||||||
if (this.selectionMode === 'day') {
|
if (this.selectionMode === 'day') {
|
||||||
this.date = this.value
|
let newDate = this.value
|
||||||
? modifyDate(this.value, value.getFullYear(), value.getMonth(), value.getDate())
|
? modifyDate(this.value, value.getFullYear(), value.getMonth(), value.getDate())
|
||||||
: modifyWithTimeString(value, this.defaultTime);
|
: modifyWithTimeString(value, this.defaultTime);
|
||||||
|
// change default time while out of selectableRange
|
||||||
|
if (!this.checkDateWithinRange(newDate)) {
|
||||||
|
newDate = modifyDate(this.selectableRange[0][0], value.getFullYear(), value.getMonth(), value.getDate());
|
||||||
|
}
|
||||||
|
this.date = newDate;
|
||||||
this.emit(this.date, this.showTime);
|
this.emit(this.date, this.showTime);
|
||||||
} else if (this.selectionMode === 'week') {
|
} else if (this.selectionMode === 'week') {
|
||||||
this.emit(value.date);
|
this.emit(value.date);
|
||||||
|
@ -359,7 +368,7 @@
|
||||||
changeToNow() {
|
changeToNow() {
|
||||||
// NOTE: not a permanent solution
|
// NOTE: not a permanent solution
|
||||||
// consider disable "now" button in the future
|
// consider disable "now" button in the future
|
||||||
if (!this.disabledDate || !this.disabledDate(new Date())) {
|
if ((!this.disabledDate || !this.disabledDate(new Date())) && this.checkDateWithinRange(new Date())) {
|
||||||
this.date = new Date();
|
this.date = new Date();
|
||||||
this.emit(this.date);
|
this.emit(this.date);
|
||||||
}
|
}
|
||||||
|
@ -446,7 +455,7 @@
|
||||||
|
|
||||||
handleVisibleTimeChange(value) {
|
handleVisibleTimeChange(value) {
|
||||||
const time = parseDate(value, this.timeFormat);
|
const time = parseDate(value, this.timeFormat);
|
||||||
if (time) {
|
if (time && this.checkDateWithinRange(time)) {
|
||||||
this.date = modifyDate(time, this.year, this.month, this.monthDate);
|
this.date = modifyDate(time, this.year, this.month, this.monthDate);
|
||||||
this.userInputTime = null;
|
this.userInputTime = null;
|
||||||
this.$refs.timepicker.value = this.date;
|
this.$refs.timepicker.value = this.date;
|
||||||
|
@ -473,13 +482,19 @@
|
||||||
typeof this.disabledDate === 'function'
|
typeof this.disabledDate === 'function'
|
||||||
? !this.disabledDate(value)
|
? !this.disabledDate(value)
|
||||||
: true
|
: true
|
||||||
);
|
) && this.checkDateWithinRange(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
getDefaultValue() {
|
getDefaultValue() {
|
||||||
// if default-value is set, return it
|
// if default-value is set, return it
|
||||||
// otherwise, return now (the moment this method gets called)
|
// otherwise, return now (the moment this method gets called)
|
||||||
return this.defaultValue ? new Date(this.defaultValue) : new Date();
|
return this.defaultValue ? new Date(this.defaultValue) : new Date();
|
||||||
|
},
|
||||||
|
|
||||||
|
checkDateWithinRange(date) {
|
||||||
|
return this.selectableRange.length > 0
|
||||||
|
? timeWithinRange(date, this.selectableRange, this.format || 'HH:mm:ss')
|
||||||
|
: true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -500,6 +515,7 @@
|
||||||
visible: false,
|
visible: false,
|
||||||
currentView: 'date',
|
currentView: 'date',
|
||||||
disabledDate: '',
|
disabledDate: '',
|
||||||
|
selectableRange: [],
|
||||||
firstDayOfWeek: 7,
|
firstDayOfWeek: 7,
|
||||||
showWeekNumber: false,
|
showWeekNumber: false,
|
||||||
timePickerVisible: false,
|
timePickerVisible: false,
|
||||||
|
|
|
@ -864,7 +864,6 @@ export default {
|
||||||
};
|
};
|
||||||
updateOptions();
|
updateOptions();
|
||||||
this.unwatchPickerOptions = this.$watch('pickerOptions', () => updateOptions(), { deep: true });
|
this.unwatchPickerOptions = this.$watch('pickerOptions', () => updateOptions(), { deep: true });
|
||||||
|
|
||||||
this.$el.appendChild(this.picker.$el);
|
this.$el.appendChild(this.picker.$el);
|
||||||
this.picker.resetView && this.picker.resetView();
|
this.picker.resetView && this.picker.resetView();
|
||||||
|
|
||||||
|
|
|
@ -2289,6 +2289,51 @@ describe('DatePicker', () => {
|
||||||
}, DELAY);
|
}, DELAY);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('picker-options:selectableRange', () => {
|
||||||
|
let vm;
|
||||||
|
afterEach(() => destroyVM(vm));
|
||||||
|
it('selectableRange', done => {
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<el-date-picker v-model="value" type="datetime" :picker-options="pickerOptions" ref="compo"></el-date-picker>
|
||||||
|
`,
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
value: new Date(2019, 0, 1, 18, 50),
|
||||||
|
pickerOptions: {
|
||||||
|
selectableRange: ['17:30:00 - 18:30:00', '18:50:00 - 20:30:00', '21:00:00 - 22:00:00']
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
const input = vm.$el.querySelector('input');
|
||||||
|
input.blur();
|
||||||
|
input.focus();
|
||||||
|
setTimeout(() => {
|
||||||
|
const $el = vm.$refs.compo.picker.$el;
|
||||||
|
const input1 = $el.querySelectorAll('.el-date-picker__editor-wrap input')[1];
|
||||||
|
input1.blur();
|
||||||
|
input1.focus();
|
||||||
|
setTimeout(_ => {
|
||||||
|
const list = $el.querySelectorAll('.el-time-spinner__list');
|
||||||
|
const hoursEl = list[0];
|
||||||
|
const disabledHours = [].slice
|
||||||
|
.call(hoursEl.querySelectorAll('.disabled'))
|
||||||
|
.map(node => Number(node.textContent));
|
||||||
|
expect(disabledHours[disabledHours.length - 2]).to.equal(16);
|
||||||
|
expect(disabledHours[disabledHours.length - 1]).to.equal(23);
|
||||||
|
const minutesEl = list[1];
|
||||||
|
const disabledMinutes = [].slice
|
||||||
|
.call(minutesEl.querySelectorAll('.disabled'))
|
||||||
|
.map(node => Number(node.textContent));
|
||||||
|
expect(disabledMinutes.length).to.equal(19);
|
||||||
|
done();
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('picker-options:disabledDate', () => {
|
describe('picker-options:disabledDate', () => {
|
||||||
let vm;
|
let vm;
|
||||||
beforeEach(done => {
|
beforeEach(done => {
|
||||||
|
|
Loading…
Reference in New Issue