mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
refactor(DatePicker): v-model now is string array
This commit is contained in:
parent
e8ffc80dc6
commit
ef8e66a924
@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ref,
|
ref,
|
||||||
|
watch,
|
||||||
computed,
|
computed,
|
||||||
defineComponent,
|
defineComponent,
|
||||||
type PropType,
|
type PropType,
|
||||||
@ -9,10 +10,11 @@ import {
|
|||||||
// Utils
|
// Utils
|
||||||
import {
|
import {
|
||||||
pick,
|
pick,
|
||||||
clamp,
|
|
||||||
extend,
|
extend,
|
||||||
isDate,
|
isDate,
|
||||||
padZero,
|
padZero,
|
||||||
|
isSameValue,
|
||||||
|
makeArrayProp,
|
||||||
createNamespace,
|
createNamespace,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
import {
|
import {
|
||||||
@ -31,7 +33,7 @@ const [name] = createNamespace('date-picker');
|
|||||||
export type DatePickerColumnType = 'year' | 'month' | 'day';
|
export type DatePickerColumnType = 'year' | 'month' | 'day';
|
||||||
|
|
||||||
const datePickerProps = extend({}, sharedProps, {
|
const datePickerProps = extend({}, sharedProps, {
|
||||||
modelValue: Date,
|
modelValue: makeArrayProp<string>(),
|
||||||
columnsType: {
|
columnsType: {
|
||||||
type: Array as PropType<DatePickerColumnType[]>,
|
type: Array as PropType<DatePickerColumnType[]>,
|
||||||
default: () => ['year', 'month', 'day'],
|
default: () => ['year', 'month', 'day'],
|
||||||
@ -58,42 +60,7 @@ export default defineComponent({
|
|||||||
emits: ['confirm', 'cancel', 'change', 'update:modelValue'],
|
emits: ['confirm', 'cancel', 'change', 'update:modelValue'],
|
||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const currentValues = ref<string[]>([]);
|
const currentValues = ref<string[]>(props.modelValue);
|
||||||
|
|
||||||
// const setValue = (type: DatePickerColumnType, newValue: string) => {
|
|
||||||
// const index = props.columnsType.indexOf(type);
|
|
||||||
// currentValues.value[index] = newValue;
|
|
||||||
// };
|
|
||||||
|
|
||||||
const getValue = (type: DatePickerColumnType) => {
|
|
||||||
const index = props.columnsType.indexOf(type);
|
|
||||||
return +currentValues.value[index];
|
|
||||||
};
|
|
||||||
|
|
||||||
const formatValue = (value: Date) => {
|
|
||||||
const timestamp = clamp(
|
|
||||||
value.getTime(),
|
|
||||||
props.minDate.getTime(),
|
|
||||||
props.maxDate.getTime()
|
|
||||||
);
|
|
||||||
|
|
||||||
const date = new Date(timestamp);
|
|
||||||
return props.columnsType.map((type) => {
|
|
||||||
switch (type) {
|
|
||||||
case 'year':
|
|
||||||
return String(date.getFullYear());
|
|
||||||
case 'month':
|
|
||||||
return padZero(date.getMonth() + 1);
|
|
||||||
case 'day':
|
|
||||||
default:
|
|
||||||
return padZero(date.getDate());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (props.modelValue) {
|
|
||||||
currentValues.value = formatValue(props.modelValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
const genOptions = (
|
const genOptions = (
|
||||||
min: number,
|
min: number,
|
||||||
@ -120,6 +87,11 @@ export default defineComponent({
|
|||||||
const isMaxMonth = (month: number) =>
|
const isMaxMonth = (month: number) =>
|
||||||
month === props.maxDate.getMonth() + 1;
|
month === props.maxDate.getMonth() + 1;
|
||||||
|
|
||||||
|
const getValue = (type: DatePickerColumnType) => {
|
||||||
|
const index = props.columnsType.indexOf(type);
|
||||||
|
return +currentValues.value[index];
|
||||||
|
};
|
||||||
|
|
||||||
const genMonthOptions = () => {
|
const genMonthOptions = () => {
|
||||||
if (isMaxYear(getValue('year'))) {
|
if (isMaxYear(getValue('year'))) {
|
||||||
return genOptions(1, props.maxDate.getMonth() + 1, 'month');
|
return genOptions(1, props.maxDate.getMonth() + 1, 'month');
|
||||||
@ -156,20 +128,26 @@ export default defineComponent({
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// watch(currentValues, (value, oldValue) =>
|
watch(
|
||||||
// emit('update:modelValue', oldValue ? value : null)
|
currentValues,
|
||||||
// );
|
(newValues) => {
|
||||||
|
if (isSameValue(newValues, props.modelValue)) {
|
||||||
|
emit('update:modelValue', newValues);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
deep: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// watch(
|
watch(
|
||||||
// () => props.modelValue,
|
() => props.modelValue,
|
||||||
// (value) => {
|
(newValues) => {
|
||||||
// value = formatValue(value);
|
if (isSameValue(newValues, currentValues.value)) {
|
||||||
|
currentValues.value = newValues;
|
||||||
// if (value && value.valueOf() !== currentValues.value?.valueOf()) {
|
}
|
||||||
// currentValues.value = value;
|
}
|
||||||
// }
|
);
|
||||||
// }
|
|
||||||
// );
|
|
||||||
|
|
||||||
const onChange = (...args: unknown[]) => emit('change', ...args);
|
const onChange = (...args: unknown[]) => emit('change', ...args);
|
||||||
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
|
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
|
||||||
|
@ -29,7 +29,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
return {
|
return {
|
||||||
minDate: new Date(2020, 0, 1),
|
minDate: new Date(2020, 0, 1),
|
||||||
maxDate: new Date(2025, 5, 1),
|
maxDate: new Date(2025, 5, 1),
|
||||||
@ -65,7 +65,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
return {
|
return {
|
||||||
minDate: new Date(2020, 0, 1),
|
minDate: new Date(2020, 0, 1),
|
||||||
maxDate: new Date(2025, 5, 1),
|
maxDate: new Date(2025, 5, 1),
|
||||||
@ -93,7 +93,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
|
|
||||||
const formatter = (type, val) => {
|
const formatter = (type, val) => {
|
||||||
if (type === 'year') {
|
if (type === 'year') {
|
||||||
@ -133,7 +133,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
const filter = (type, options) => {
|
const filter = (type, options) => {
|
||||||
if (type === 'month') {
|
if (type === 'month') {
|
||||||
return options.filter((option) => Number(option.value) % 6 === 0);
|
return options.filter((option) => Number(option.value) % 6 === 0);
|
||||||
@ -157,6 +157,7 @@ export default {
|
|||||||
|
|
||||||
| Attribute | Description | Type | Default |
|
| Attribute | Description | Type | Default |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
|
| v-model | Current date | _string[]_ | `[]` |
|
||||||
| columns-type | Columns type | _string[]_ | `['year', 'month', 'day']` |
|
| columns-type | Columns type | _string[]_ | `['year', 'month', 'day']` |
|
||||||
| min-date | Min date | _Date_ | Ten years ago on January 1 |
|
| min-date | Min date | _Date_ | Ten years ago on January 1 |
|
||||||
| max-date | Max date | _Date_ | Ten years later on December 31 |
|
| max-date | Max date | _Date_ | Ten years later on December 31 |
|
||||||
|
@ -31,7 +31,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
return {
|
return {
|
||||||
minDate: new Date(2020, 0, 1),
|
minDate: new Date(2020, 0, 1),
|
||||||
maxDate: new Date(2025, 5, 1),
|
maxDate: new Date(2025, 5, 1),
|
||||||
@ -67,7 +67,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
return {
|
return {
|
||||||
minDate: new Date(2020, 0, 1),
|
minDate: new Date(2020, 0, 1),
|
||||||
maxDate: new Date(2025, 5, 1),
|
maxDate: new Date(2025, 5, 1),
|
||||||
@ -97,7 +97,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
|
|
||||||
const formatter = (type, val) => {
|
const formatter = (type, val) => {
|
||||||
if (type === 'year') {
|
if (type === 'year') {
|
||||||
@ -139,7 +139,7 @@ import { ref } from 'vue';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const currentDate = ref(new Date(2021, 0, 1));
|
const currentDate = ref(['2021', '01', '01']);
|
||||||
const filter = (type, options) => {
|
const filter = (type, options) => {
|
||||||
if (type === 'month') {
|
if (type === 'month') {
|
||||||
return options.filter((option) => Number(option.value) % 6 === 0);
|
return options.filter((option) => Number(option.value) % 6 === 0);
|
||||||
@ -163,6 +163,7 @@ export default {
|
|||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 |
|
| 参数 | 说明 | 类型 | 默认值 |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
|
| v-model | 当前选中的日期 | _string[]_ | `[]` |
|
||||||
| columns-type | 选项类型,由 `year`、`month` 和 `day` 组成的数组 | _string[]_ | `['year', 'month', 'day']` |
|
| columns-type | 选项类型,由 `year`、`month` 和 `day` 组成的数组 | _string[]_ | `['year', 'month', 'day']` |
|
||||||
| min-date | 可选的最小时间,精确到日 | _Date_ | 十年前 |
|
| min-date | 可选的最小时间,精确到日 | _Date_ | 十年前 |
|
||||||
| max-date | 可选的最大时间,精确到日 | _Date_ | 十年后 |
|
| max-date | 可选的最大时间,精确到日 | _Date_ | 十年后 |
|
||||||
|
@ -29,10 +29,10 @@ const t = useTranslate({
|
|||||||
|
|
||||||
const minDate = new Date(2020, 0, 1);
|
const minDate = new Date(2020, 0, 1);
|
||||||
const maxDate = new Date(2025, 5, 1);
|
const maxDate = new Date(2025, 5, 1);
|
||||||
const basicDate = ref(new Date(2021, 0, 1));
|
const basicDate = ref(['2021', '01', '01']);
|
||||||
const yearMonthDate = ref(new Date(2021, 0, 1));
|
const yearMonthDate = ref(['2021', '01', '01']);
|
||||||
const formatterDate = ref(new Date(2021, 0, 1));
|
const formatterDate = ref(['2021', '01', '01']);
|
||||||
const filterDate = ref(new Date(2021, 0, 1));
|
const filterDate = ref(['2021', '01', '01']);
|
||||||
|
|
||||||
const filter = (type: string, options: PickerOption[]) => {
|
const filter = (type: string, options: PickerOption[]) => {
|
||||||
if (type === 'month') {
|
if (type === 'month') {
|
||||||
|
@ -13,6 +13,7 @@ import {
|
|||||||
addUnit,
|
addUnit,
|
||||||
addNumber,
|
addNumber,
|
||||||
numericProp,
|
numericProp,
|
||||||
|
isSameValue,
|
||||||
getSizeStyle,
|
getSizeStyle,
|
||||||
preventDefault,
|
preventDefault,
|
||||||
stopPropagation,
|
stopPropagation,
|
||||||
@ -131,9 +132,6 @@ export default defineComponent({
|
|||||||
return addNumber(min, diff);
|
return addNumber(min, diff);
|
||||||
};
|
};
|
||||||
|
|
||||||
const isSameValue = (newValue: SliderValue, oldValue: SliderValue) =>
|
|
||||||
JSON.stringify(newValue) === JSON.stringify(oldValue);
|
|
||||||
|
|
||||||
const handleRangeValue = (value: NumberRange) => {
|
const handleRangeValue = (value: NumberRange) => {
|
||||||
// 设置默认值
|
// 设置默认值
|
||||||
const left = value[0] ?? Number(props.min);
|
const left = value[0] ?? Number(props.min);
|
||||||
|
@ -34,3 +34,6 @@ export function pick<T, U extends keyof T>(
|
|||||||
return ret;
|
return ret;
|
||||||
}, {} as Writeable<Pick<T, U>>);
|
}, {} as Writeable<Pick<T, U>>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isSameValue = (newValue: unknown, oldValue: unknown) =>
|
||||||
|
JSON.stringify(newValue) === JSON.stringify(oldValue);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user