mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
refactor(TimePicker): refactor with composition api
This commit is contained in:
parent
c4e9f2c991
commit
486ad58042
@ -31,7 +31,7 @@ export default createComponent({
|
|||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
innerValue: this.formatValue(this.modelValue),
|
currentDate: this.formatValue(this.modelValue),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ export default createComponent({
|
|||||||
maxMonth,
|
maxMonth,
|
||||||
maxHour,
|
maxHour,
|
||||||
maxMinute,
|
maxMinute,
|
||||||
} = this.getBoundary('max', this.innerValue);
|
} = this.getBoundary('max', this.currentDate);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
minYear,
|
minYear,
|
||||||
@ -51,7 +51,7 @@ export default createComponent({
|
|||||||
minMonth,
|
minMonth,
|
||||||
minHour,
|
minHour,
|
||||||
minMinute,
|
minMinute,
|
||||||
} = this.getBoundary('min', this.innerValue);
|
} = this.getBoundary('min', this.currentDate);
|
||||||
|
|
||||||
let result = [
|
let result = [
|
||||||
{
|
{
|
||||||
@ -136,15 +136,15 @@ export default createComponent({
|
|||||||
maxDate: 'updateInnerValue',
|
maxDate: 'updateInnerValue',
|
||||||
columns: 'updateColumnValue',
|
columns: 'updateColumnValue',
|
||||||
|
|
||||||
innerValue(val) {
|
currentDate(val) {
|
||||||
this.$emit('update:modelValue', val);
|
this.$emit('update:modelValue', val);
|
||||||
},
|
},
|
||||||
|
|
||||||
modelValue(val) {
|
modelValue(val) {
|
||||||
val = this.formatValue(val);
|
val = this.formatValue(val);
|
||||||
|
|
||||||
if (val.valueOf() !== this.innerValue.valueOf()) {
|
if (val.valueOf() !== this.currentDate.valueOf()) {
|
||||||
this.innerValue = val;
|
this.currentDate = val;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -164,7 +164,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onConfirm() {
|
onConfirm() {
|
||||||
this.$emit('confirm', this.innerValue);
|
this.$emit('confirm', this.currentDate);
|
||||||
},
|
},
|
||||||
|
|
||||||
onCancel() {
|
onCancel() {
|
||||||
@ -237,7 +237,7 @@ export default createComponent({
|
|||||||
let month;
|
let month;
|
||||||
let day;
|
let day;
|
||||||
if (type === 'month-day') {
|
if (type === 'month-day') {
|
||||||
year = this.innerValue.getFullYear();
|
year = this.currentDate.getFullYear();
|
||||||
month = getValue('month');
|
month = getValue('month');
|
||||||
day = getValue('day');
|
day = getValue('day');
|
||||||
} else {
|
} else {
|
||||||
@ -263,7 +263,7 @@ export default createComponent({
|
|||||||
|
|
||||||
const value = new Date(year, month - 1, day, hour, minute);
|
const value = new Date(year, month - 1, day, hour, minute);
|
||||||
|
|
||||||
this.innerValue = this.formatValue(value);
|
this.currentDate = this.formatValue(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
onChange(picker) {
|
onChange(picker) {
|
||||||
@ -277,7 +277,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
updateColumnValue() {
|
updateColumnValue() {
|
||||||
const value = this.innerValue;
|
const value = this.currentDate;
|
||||||
const { formatter } = this;
|
const { formatter } = this;
|
||||||
|
|
||||||
const values = this.originColumns.map((column) => {
|
const values = this.originColumns.map((column) => {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { ref, watch, computed, nextTick, onMounted } from 'vue';
|
||||||
import { padZero } from '../utils/format/string';
|
import { createNamespace, pick } from '../utils';
|
||||||
import { range } from '../utils/format/number';
|
import { range } from '../utils/format/number';
|
||||||
|
import { padZero } from '../utils/format/string';
|
||||||
import { times, sharedProps } from './utils';
|
import { times, sharedProps } from './utils';
|
||||||
import Picker from '../picker';
|
import Picker from '../picker';
|
||||||
import { pickerProps } from '../picker/shared';
|
import { pickerProps } from '../picker/shared';
|
||||||
@ -30,156 +31,144 @@ export default createComponent({
|
|||||||
|
|
||||||
emits: ['confirm', 'cancel', 'change', 'update:modelValue'],
|
emits: ['confirm', 'cancel', 'change', 'update:modelValue'],
|
||||||
|
|
||||||
data() {
|
setup(props, { emit }) {
|
||||||
return {
|
const formatValue = (value) => {
|
||||||
innerValue: this.formatValue(this.modelValue),
|
const { minHour, maxHour, maxMinute, minMinute } = props;
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
value = `${padZero(minHour)}:${padZero(minMinute)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let [hour, minute] = value.split(':');
|
||||||
|
hour = padZero(range(hour, minHour, maxHour));
|
||||||
|
minute = padZero(range(minute, minMinute, maxMinute));
|
||||||
|
|
||||||
|
return `${hour}:${minute}`;
|
||||||
};
|
};
|
||||||
},
|
|
||||||
|
|
||||||
computed: {
|
const picker = ref();
|
||||||
ranges() {
|
const currentDate = ref(formatValue(props.modelValue));
|
||||||
return [
|
|
||||||
{
|
|
||||||
type: 'hour',
|
|
||||||
range: [+this.minHour, +this.maxHour],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'minute',
|
|
||||||
range: [+this.minMinute, +this.maxMinute],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
},
|
|
||||||
|
|
||||||
originColumns() {
|
const ranges = computed(() => [
|
||||||
return this.ranges.map(({ type, range: rangeArr }) => {
|
{
|
||||||
let values = times(rangeArr[1] - rangeArr[0] + 1, (index) => {
|
type: 'hour',
|
||||||
const value = padZero(rangeArr[0] + index);
|
range: [+props.minHour, +props.maxHour],
|
||||||
return value;
|
},
|
||||||
});
|
{
|
||||||
|
type: 'minute',
|
||||||
|
range: [+props.minMinute, +props.maxMinute],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
if (this.filter) {
|
const originColumns = computed(() =>
|
||||||
values = this.filter(type, values);
|
ranges.value.map(({ type, range: rangeArr }) => {
|
||||||
|
let values = times(rangeArr[1] - rangeArr[0] + 1, (index) =>
|
||||||
|
padZero(rangeArr[0] + index)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (props.filter) {
|
||||||
|
values = props.filter(type, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
values,
|
values,
|
||||||
};
|
};
|
||||||
});
|
})
|
||||||
},
|
);
|
||||||
|
|
||||||
columns() {
|
const columns = computed(() =>
|
||||||
return this.originColumns.map((column) => ({
|
originColumns.value.map((column) => ({
|
||||||
values: column.values.map((value) =>
|
values: column.values.map((value) =>
|
||||||
this.formatter(column.type, value)
|
props.formatter(column.type, value)
|
||||||
),
|
),
|
||||||
}));
|
}))
|
||||||
},
|
);
|
||||||
},
|
|
||||||
|
|
||||||
watch: {
|
const updateColumnValue = () => {
|
||||||
filter: 'updateInnerValue',
|
const pair = currentDate.value.split(':');
|
||||||
minHour: 'updateInnerValue',
|
const values = [
|
||||||
maxHour: 'updateInnerValue',
|
props.formatter('hour', pair[0]),
|
||||||
minMinute: 'updateInnerValue',
|
props.formatter('minute', pair[1]),
|
||||||
maxMinute: 'updateInnerValue',
|
];
|
||||||
columns: 'updateColumnValue',
|
|
||||||
|
|
||||||
innerValue(val) {
|
nextTick(() => {
|
||||||
this.$emit('update:modelValue', val);
|
picker.value.setValues(values);
|
||||||
},
|
});
|
||||||
|
};
|
||||||
|
|
||||||
value(val) {
|
const updateInnerValue = () => {
|
||||||
val = this.formatValue(val);
|
const [hourIndex, minuteIndex] = picker.value.getIndexes();
|
||||||
|
const [hourColumn, minuteColumn] = originColumns.value;
|
||||||
if (val !== this.innerValue) {
|
|
||||||
this.innerValue = val;
|
|
||||||
this.updateColumnValue();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
mounted() {
|
|
||||||
this.updateColumnValue();
|
|
||||||
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.updateInnerValue();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
// @exposed-api
|
|
||||||
getPicker() {
|
|
||||||
return this.$refs.picker;
|
|
||||||
},
|
|
||||||
|
|
||||||
onConfirm() {
|
|
||||||
this.$emit('confirm', this.innerValue);
|
|
||||||
},
|
|
||||||
|
|
||||||
onCancel() {
|
|
||||||
this.$emit('cancel');
|
|
||||||
},
|
|
||||||
|
|
||||||
formatValue(value) {
|
|
||||||
if (!value) {
|
|
||||||
value = `${padZero(this.minHour)}:${padZero(this.minMinute)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
let [hour, minute] = value.split(':');
|
|
||||||
hour = padZero(range(hour, this.minHour, this.maxHour));
|
|
||||||
minute = padZero(range(minute, this.minMinute, this.maxMinute));
|
|
||||||
|
|
||||||
return `${hour}:${minute}`;
|
|
||||||
},
|
|
||||||
|
|
||||||
updateInnerValue() {
|
|
||||||
const [hourIndex, minuteIndex] = this.getPicker().getIndexes();
|
|
||||||
const [hourColumn, minuteColumn] = this.originColumns;
|
|
||||||
|
|
||||||
const hour = hourColumn.values[hourIndex] || hourColumn.values[0];
|
const hour = hourColumn.values[hourIndex] || hourColumn.values[0];
|
||||||
const minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
|
const minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
|
||||||
|
|
||||||
this.innerValue = this.formatValue(`${hour}:${minute}`);
|
currentDate.value = formatValue(`${hour}:${minute}`);
|
||||||
this.updateColumnValue();
|
updateColumnValue();
|
||||||
},
|
};
|
||||||
|
|
||||||
onChange(picker) {
|
const onConfirm = () => {
|
||||||
this.updateInnerValue();
|
emit('confirm', currentDate.value);
|
||||||
|
};
|
||||||
|
|
||||||
this.$nextTick(() => {
|
const onCancel = () => {
|
||||||
this.$nextTick(() => {
|
emit('cancel');
|
||||||
this.$emit('change', picker);
|
};
|
||||||
|
|
||||||
|
const onChange = () => {
|
||||||
|
updateInnerValue();
|
||||||
|
|
||||||
|
nextTick(() => {
|
||||||
|
nextTick(() => {
|
||||||
|
emit('change', picker.value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
};
|
||||||
|
|
||||||
updateColumnValue() {
|
onMounted(() => {
|
||||||
const { formatter } = this;
|
updateColumnValue();
|
||||||
const pair = this.innerValue.split(':');
|
nextTick(updateInnerValue);
|
||||||
const values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
|
|
||||||
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.getPicker().setValues(values);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
render() {
|
|
||||||
const props = {};
|
|
||||||
Object.keys(pickerProps).forEach((key) => {
|
|
||||||
props[key] = this[key];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
watch(columns, updateColumnValue);
|
||||||
|
|
||||||
|
watch(
|
||||||
|
[
|
||||||
|
() => props.filter,
|
||||||
|
() => props.minHour,
|
||||||
|
() => props.maxHour,
|
||||||
|
() => props.minMinute,
|
||||||
|
() => props.maxMinute,
|
||||||
|
],
|
||||||
|
updateInnerValue
|
||||||
|
);
|
||||||
|
|
||||||
|
watch(currentDate, (value) => {
|
||||||
|
emit('update:modelValue', value);
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.modelValue,
|
||||||
|
(value) => {
|
||||||
|
value = formatValue(value);
|
||||||
|
|
||||||
|
if (value !== currentDate.value) {
|
||||||
|
currentDate.value = value;
|
||||||
|
updateColumnValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return () => (
|
||||||
<Picker
|
<Picker
|
||||||
ref="picker"
|
ref={picker}
|
||||||
columns={this.columns}
|
columns={columns.value}
|
||||||
readonly={this.readonly}
|
readonly={props.readonly}
|
||||||
onChange={this.onChange}
|
onChange={onChange}
|
||||||
onConfirm={this.onConfirm}
|
onCancel={onCancel}
|
||||||
onCancel={this.onCancel}
|
onConfirm={onConfirm}
|
||||||
{...props}
|
{...pick(props, Object.keys(pickerProps))}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user