Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DatePicker): filter support values param to help get current values #13147

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions packages/vant/src/date-picker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,9 @@ export default defineComponent({
const currentValues = ref<string[]>(props.modelValue);
const updatedByExternalSources = ref(false);
const pickerRef = ref<PickerInstance>();

const genYearOptions = () => {
const minYear = props.minDate.getFullYear();
const maxYear = props.maxDate.getFullYear();
return genOptions(
minYear,
maxYear,
'year',
props.formatter,
props.filter,
);
};
const computedValues = computed(() =>
updatedByExternalSources.value ? props.modelValue : currentValues.value,
);

const isMinYear = (year: number) => year === props.minDate.getFullYear();
const isMaxYear = (year: number) => year === props.maxDate.getFullYear();
Expand All @@ -92,9 +83,8 @@ export default defineComponent({
const getValue = (type: DatePickerColumnType) => {
const { minDate, columnsType } = props;
const index = columnsType.indexOf(type);
const value = updatedByExternalSources.value
? props.modelValue[index]
: currentValues.value[index];
const value = computedValues.value[index];

if (value) {
return +value;
}
Expand All @@ -109,6 +99,19 @@ export default defineComponent({
}
};

const genYearOptions = () => {
const minYear = props.minDate.getFullYear();
const maxYear = props.maxDate.getFullYear();
return genOptions(
minYear,
maxYear,
'year',
props.formatter,
props.filter,
computedValues.value,
);
};

const genMonthOptions = () => {
const year = getValue('year');
const minMonth = isMinYear(year) ? props.minDate.getMonth() + 1 : 1;
Expand All @@ -120,6 +123,7 @@ export default defineComponent({
'month',
props.formatter,
props.filter,
computedValues.value,
);
};

Expand All @@ -133,7 +137,14 @@ export default defineComponent({
? props.maxDate.getDate()
: getMonthEndDay(year, month);

return genOptions(minDate, maxDate, 'day', props.formatter, props.filter);
return genOptions(
minDate,
maxDate,
'day',
props.formatter,
props.filter,
computedValues.value,
);
};

const confirm = () => pickerRef.value?.confirm();
Expand Down
2 changes: 1 addition & 1 deletion packages/vant/src/date-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default {
| show-toolbar | Whether to show toolbar | _boolean_ | `true` |
| loading | Whether to show loading prompt | _boolean_ | `false` |
| readonly | Whether to be readonly | _boolean_ | `false` |
| filter | Option filter | _(type: string, options: PickerOption[]) => PickerOption[]_ | - |
| filter | Option filter | _(type: string, options: PickerOption[], values: string[]) => PickerOption[]_ | - |
| formatter | Option formatter | _(type: string, option: PickerOption) => PickerOption_ | - |
| option-height | Option height, supports `px` `vw` `vh` `rem` unit, default `px` | _number \| string_ | `44` |
| visible-option-num | Count of visible columns | _number \| string_ | `6` |
Expand Down
2 changes: 1 addition & 1 deletion packages/vant/src/date-picker/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default {
| show-toolbar | 是否显示顶部栏 | _boolean_ | `true` |
| loading | 是否显示加载状态 | _boolean_ | `false` |
| readonly | 是否为只读状态,只读状态下无法切换选项 | _boolean_ | `false` |
| filter | 选项过滤函数 | _(type: string, options: PickerOption[]) => PickerOption[]_ | - |
| filter | 选项过滤函数 | _(type: string, options: PickerOption[], values: string[]) => PickerOption[]_ | - |
| formatter | 选项格式化函数 | _(type: string, option: PickerOption) => PickerOption_ | - |
| option-height | 选项高度,支持 `px` `vw` `vh` `rem` 单位,默认 `px` | _number \| string_ | `44` |
| visible-option-num | 可见的选项个数 | _number \| string_ | `6` |
Expand Down
17 changes: 5 additions & 12 deletions packages/vant/src/date-picker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import {
extend,
padZero,
makeArrayProp,
clamp,
type RequiredParams,
} from '../utils';
import { extend, padZero, makeArrayProp, clamp } from '../utils';
import { pickerSharedProps } from '../picker/Picker';
import type { PropType } from 'vue';
import type { PickerOption } from '../picker';

type Filter = (
columnType: string,
options: PickerOption[],
values?: string[],
values: string[],
) => PickerOption[];
export type TimeFilter = RequiredParams<Filter>;
type Formatter = (type: string, option: PickerOption) => PickerOption;

export const sharedProps = extend({}, pickerSharedProps, {
Expand Down Expand Up @@ -53,8 +46,8 @@ export const genOptions = <T extends string>(
max: number,
type: T,
formatter: Formatter,
filter?: Filter | TimeFilter,
values?: string[],
filter: Filter | undefined,
values: string[],
) => {
const options = times(max - min + 1, (index) => {
const value = padZero(min + index);
Expand All @@ -63,7 +56,7 @@ export const genOptions = <T extends string>(
value,
});
});
return filter ? filter(type, options, values!) : options;
return filter ? filter(type, options, values) : options;
};

export const formatValueRange = (values: string[], columns: PickerOption[][]) =>
Expand Down
2 changes: 0 additions & 2 deletions packages/vant/src/time-picker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
genOptions,
pickerInheritKeys,
sharedProps,
type TimeFilter,
} from '../date-picker/utils';
import {
pick,
Expand Down Expand Up @@ -57,7 +56,6 @@ export const timePickerProps = extend({}, sharedProps, {
type: Array as PropType<TimePickerColumnType[]>,
default: () => ['hour', 'minute'],
},
filter: Function as PropType<TimeFilter>,
});

export type TimePickerProps = ExtractPropTypes<typeof timePickerProps>;
Expand Down