diff --git a/packages/vant/src/picker/Picker.tsx b/packages/vant/src/picker/Picker.tsx index a57c35df2..0fa3e560e 100644 --- a/packages/vant/src/picker/Picker.tsx +++ b/packages/vant/src/picker/Picker.tsx @@ -259,7 +259,10 @@ export default defineComponent({ options.length && !isOptionExist(options, selectedValues.value[index], fields.value) ) { - setValue(index, getFirstEnabledOption(options)[fields.value.value]); + setValue( + index, + getFirstEnabledOption(options)![fields.value.value] + ); } }); }, diff --git a/packages/vant/src/picker/README.md b/packages/vant/src/picker/README.md index f933f0a4b..66ea84263 100644 --- a/packages/vant/src/picker/README.md +++ b/packages/vant/src/picker/README.md @@ -358,7 +358,7 @@ Use [ref](https://v3.vuejs.org/guide/component-template-refs.html) to get Picker | Name | Description | Attribute | Return value | | --- | --- | --- | --- | | confirm | Stop scrolling and emit confirm event | - | - | -| getSelectedOptions | Get current selected options | - | _PickerOption[]_ | +| getSelectedOptions | Get current selected options | - | _(PickerOption \| undefined)[]_ | ### Types diff --git a/packages/vant/src/picker/README.zh-CN.md b/packages/vant/src/picker/README.zh-CN.md index 4f577f1c4..c7859af63 100644 --- a/packages/vant/src/picker/README.zh-CN.md +++ b/packages/vant/src/picker/README.zh-CN.md @@ -377,7 +377,7 @@ export default { | 方法名 | 说明 | 参数 | 返回值 | | --- | --- | --- | --- | | confirm | 停止惯性滚动并触发 `confirm` 事件 | - | - | -| getSelectedOptions | 获取当前选中的选项 | - | _PickerOption[]_ | +| getSelectedOptions | 获取当前选中的选项 | - | _(PickerOption \| undefined)[]_ | ### 类型定义 diff --git a/packages/vant/src/picker/types.ts b/packages/vant/src/picker/types.ts index b72e4105b..df233f3be 100644 --- a/packages/vant/src/picker/types.ts +++ b/packages/vant/src/picker/types.ts @@ -24,7 +24,7 @@ export type PickerColumn = PickerOption[]; export type PickerExpose = { confirm: () => void; - getSelectedOptions: () => PickerOption[]; + getSelectedOptions: () => Array; }; export type PickerColumnProvide = { @@ -45,7 +45,7 @@ export type PickerInstance = ComponentPublicInstance; export type PickerConfirmEventParams = { selectedValues: Array; - selectedOptions: PickerOption[]; + selectedOptions: Array; }; export type PickerCancelEventParams = PickerConfirmEventParams; diff --git a/packages/vant/src/picker/utils.ts b/packages/vant/src/picker/utils.ts index 58f304746..2e4bb6537 100644 --- a/packages/vant/src/picker/utils.ts +++ b/packages/vant/src/picker/utils.ts @@ -2,9 +2,10 @@ import { isDef, clamp, extend } from '../utils'; import type { Ref } from 'vue'; import type { PickerOption, PickerColumn, PickerFieldNames } from './types'; -export function getFirstEnabledOption(options: PickerOption[]) { - return options.find((option) => !option.disabled) || options[0]; -} +export const getFirstEnabledOption = ( + options: PickerOption[] +): PickerOption | undefined => + options.find((option) => !option.disabled) || options[0]; export function getColumnsType( columns: PickerColumn | PickerColumn[], @@ -50,7 +51,7 @@ export function findOptionByValue( options: PickerOption[], value: number | string, fields: Required -) { +): PickerOption | undefined { const index = options.findIndex((option) => option[fields.value] === value); const enabledIndex = findIndexOfEnabledOption(options, index); return options[enabledIndex]; @@ -77,7 +78,7 @@ export function formatCascadeColumns( : undefined; if (!cursor && options.length) { - const firstValue = getFirstEnabledOption(options)[fields.value]; + const firstValue = getFirstEnabledOption(options)![fields.value]; cursor = findOptionByValue(options, firstValue, fields); }