diff --git a/packages/vant/src/picker/Picker.tsx b/packages/vant/src/picker/Picker.tsx index d9358f0dc..51f606999 100644 --- a/packages/vant/src/picker/Picker.tsx +++ b/packages/vant/src/picker/Picker.tsx @@ -21,6 +21,7 @@ import { BORDER_UNSET_TOP_BOTTOM, } from '../utils'; import { + isOptionExist, isValuesEqual, getColumnsType, findOptionByValue, @@ -118,7 +119,7 @@ export default defineComponent({ // reset values after cascading selectedValues.value.forEach((value, index) => { const options = currentColumns.value[index]; - if (!options.find((option) => option[fields.value.value] === value)) { + if (!isOptionExist(options, value, fields.value)) { selectedValues.value[index] = options.length ? options[0][fields.value.value] : undefined; @@ -245,7 +246,10 @@ export default defineComponent({ currentColumns, (columns) => { columns.forEach((options, index) => { - if (selectedValues.value[index] === undefined && options.length) { + if ( + options.length && + !isOptionExist(options, selectedValues.value[index], fields.value) + ) { selectedValues.value[index] = getFirstEnabledOption(options)[fields.value.value]; } diff --git a/packages/vant/src/picker/utils.ts b/packages/vant/src/picker/utils.ts index 7836478e5..579bc5eb1 100644 --- a/packages/vant/src/picker/utils.ts +++ b/packages/vant/src/picker/utils.ts @@ -38,6 +38,14 @@ export function findIndexOfEnabledOption( return 0; } +export const isOptionExist = ( + options: PickerOption[], + value: number | string | undefined, + fields: Required +) => + value !== undefined && + !!options.find((option) => option[fields.value] === value); + export function findOptionByValue( options: PickerOption[], value: number | string,