mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-09-01 21:59:45 +08:00
chore(DatePicker): reuse genOptions
This commit is contained in:
parent
b608fb23f0
commit
e2790fe95d
@ -8,15 +8,13 @@ import {
|
|||||||
} from 'vue';
|
} from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
|
import { pick, extend, isDate, isSameValue, createNamespace } from '../utils';
|
||||||
import {
|
import {
|
||||||
pick,
|
genOptions,
|
||||||
extend,
|
sharedProps,
|
||||||
isDate,
|
getMonthEndDay,
|
||||||
padZero,
|
pickerInheritKeys,
|
||||||
isSameValue,
|
} from './utils';
|
||||||
createNamespace,
|
|
||||||
} from '../utils';
|
|
||||||
import { times, sharedProps, getMonthEndDay, pickerInheritKeys } from './utils';
|
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import { Picker } from '../picker';
|
import { Picker } from '../picker';
|
||||||
@ -55,25 +53,16 @@ export default defineComponent({
|
|||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const currentValues = ref<string[]>(props.modelValue);
|
const currentValues = ref<string[]>(props.modelValue);
|
||||||
|
|
||||||
const genOptions = (
|
|
||||||
min: number,
|
|
||||||
max: number,
|
|
||||||
type: DatePickerColumnType
|
|
||||||
) => {
|
|
||||||
const options = times(max - min + 1, (index) => {
|
|
||||||
const value = padZero(min + index);
|
|
||||||
return props.formatter(type, {
|
|
||||||
text: value,
|
|
||||||
value,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return props.filter ? props.filter(type, options) : options;
|
|
||||||
};
|
|
||||||
|
|
||||||
const genYearOptions = () => {
|
const genYearOptions = () => {
|
||||||
const minYear = props.minDate.getFullYear();
|
const minYear = props.minDate.getFullYear();
|
||||||
const maxYear = props.maxDate.getFullYear();
|
const maxYear = props.maxDate.getFullYear();
|
||||||
return genOptions(minYear, maxYear, 'year');
|
return genOptions(
|
||||||
|
minYear,
|
||||||
|
maxYear,
|
||||||
|
'year',
|
||||||
|
props.formatter,
|
||||||
|
props.filter
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const isMaxYear = (year: number) => year === props.maxDate.getFullYear();
|
const isMaxYear = (year: number) => year === props.maxDate.getFullYear();
|
||||||
@ -100,9 +89,15 @@ export default defineComponent({
|
|||||||
|
|
||||||
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',
|
||||||
|
props.formatter,
|
||||||
|
props.filter
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return genOptions(1, 12, 'month');
|
return genOptions(1, 12, 'month', props.formatter, props.filter);
|
||||||
};
|
};
|
||||||
|
|
||||||
const genDayOptions = () => {
|
const genDayOptions = () => {
|
||||||
@ -114,7 +109,7 @@ export default defineComponent({
|
|||||||
maxDate = props.maxDate.getDate();
|
maxDate = props.maxDate.getDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
return genOptions(1, maxDate, 'day');
|
return genOptions(1, maxDate, 'day', props.formatter, props.filter);
|
||||||
};
|
};
|
||||||
|
|
||||||
const columns = computed(() =>
|
const columns = computed(() =>
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
import { extend, makeArrayProp } from '../utils';
|
import { extend, padZero, makeArrayProp } from '../utils';
|
||||||
import { pickerSharedProps } from '../picker/Picker';
|
import { pickerSharedProps } from '../picker/Picker';
|
||||||
import type { PropType } from 'vue';
|
import type { PropType } from 'vue';
|
||||||
import type { PickerOption } from '../picker';
|
import type { PickerOption } from '../picker';
|
||||||
|
|
||||||
|
type Filter = (columnType: string, options: PickerOption[]) => PickerOption[];
|
||||||
|
type Formatter = (type: string, option: PickerOption) => PickerOption;
|
||||||
|
|
||||||
export const sharedProps = extend({}, pickerSharedProps, {
|
export const sharedProps = extend({}, pickerSharedProps, {
|
||||||
modelValue: makeArrayProp<string>(),
|
modelValue: makeArrayProp<string>(),
|
||||||
filter: Function as PropType<
|
filter: Function as PropType<Filter>,
|
||||||
(columnType: string, options: PickerOption[]) => PickerOption[]
|
|
||||||
>,
|
|
||||||
formatter: {
|
formatter: {
|
||||||
type: Function as PropType<
|
type: Function as PropType<Formatter>,
|
||||||
(type: string, option: PickerOption) => PickerOption
|
|
||||||
>,
|
|
||||||
default: (type: string, option: PickerOption) => option,
|
default: (type: string, option: PickerOption) => option,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -37,3 +36,20 @@ export function times<T>(n: number, iteratee: (index: number) => T) {
|
|||||||
|
|
||||||
export const getMonthEndDay = (year: number, month: number): number =>
|
export const getMonthEndDay = (year: number, month: number): number =>
|
||||||
32 - new Date(year, month - 1, 32).getDate();
|
32 - new Date(year, month - 1, 32).getDate();
|
||||||
|
|
||||||
|
export const genOptions = <T extends string>(
|
||||||
|
min: number,
|
||||||
|
max: number,
|
||||||
|
type: T,
|
||||||
|
formatter: Formatter,
|
||||||
|
filter?: Filter
|
||||||
|
) => {
|
||||||
|
const options = times(max - min + 1, (index) => {
|
||||||
|
const value = padZero(min + index);
|
||||||
|
return formatter(type, {
|
||||||
|
text: value,
|
||||||
|
value,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return filter ? filter(type, options) : options;
|
||||||
|
};
|
||||||
|
@ -11,12 +11,15 @@ import {
|
|||||||
import {
|
import {
|
||||||
pick,
|
pick,
|
||||||
extend,
|
extend,
|
||||||
padZero,
|
|
||||||
createNamespace,
|
createNamespace,
|
||||||
makeNumericProp,
|
makeNumericProp,
|
||||||
isSameValue,
|
isSameValue,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
import { times, sharedProps, pickerInheritKeys } from '../date-picker/utils';
|
import {
|
||||||
|
genOptions,
|
||||||
|
sharedProps,
|
||||||
|
pickerInheritKeys,
|
||||||
|
} from '../date-picker/utils';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import { Picker } from '../picker';
|
import { Picker } from '../picker';
|
||||||
@ -50,30 +53,34 @@ export default defineComponent({
|
|||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const currentValues = ref<string[]>(props.modelValue);
|
const currentValues = ref<string[]>(props.modelValue);
|
||||||
|
|
||||||
const genOptions = (
|
|
||||||
min: number,
|
|
||||||
max: number,
|
|
||||||
type: TimePickerColumnType
|
|
||||||
) => {
|
|
||||||
const options = times(max - min + 1, (index) => {
|
|
||||||
const value = padZero(min + index);
|
|
||||||
return props.formatter(type, {
|
|
||||||
text: value,
|
|
||||||
value,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return props.filter ? props.filter(type, options) : options;
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns = computed(() =>
|
const columns = computed(() =>
|
||||||
props.columnsType.map((type) => {
|
props.columnsType.map((type) => {
|
||||||
|
const { filter, formatter } = props;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'hour':
|
case 'hour':
|
||||||
return genOptions(+props.minHour, +props.maxHour, 'hour');
|
return genOptions(
|
||||||
|
+props.minHour,
|
||||||
|
+props.maxHour,
|
||||||
|
type,
|
||||||
|
formatter,
|
||||||
|
filter
|
||||||
|
);
|
||||||
case 'minute':
|
case 'minute':
|
||||||
return genOptions(+props.minMinute, +props.maxMinute, 'minute');
|
return genOptions(
|
||||||
|
+props.minMinute,
|
||||||
|
+props.maxMinute,
|
||||||
|
type,
|
||||||
|
formatter,
|
||||||
|
filter
|
||||||
|
);
|
||||||
case 'second':
|
case 'second':
|
||||||
return genOptions(+props.minSecond, +props.maxSecond, 'second');
|
return genOptions(
|
||||||
|
+props.minSecond,
|
||||||
|
+props.maxSecond,
|
||||||
|
type,
|
||||||
|
formatter,
|
||||||
|
filter
|
||||||
|
);
|
||||||
default:
|
default:
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`[Vant] DatePicker: unsupported columns type: ${type}`
|
`[Vant] DatePicker: unsupported columns type: ${type}`
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user