types(Picker): fix return type of getSelectedOptions

This commit is contained in:
chenjiahan 2022-02-17 16:25:53 +08:00
parent c65d4f9672
commit 967cb56c18
5 changed files with 14 additions and 10 deletions

View File

@ -259,7 +259,10 @@ export default defineComponent({
options.length && options.length &&
!isOptionExist(options, selectedValues.value[index], fields.value) !isOptionExist(options, selectedValues.value[index], fields.value)
) { ) {
setValue(index, getFirstEnabledOption(options)[fields.value.value]); setValue(
index,
getFirstEnabledOption(options)![fields.value.value]
);
} }
}); });
}, },

View File

@ -358,7 +358,7 @@ Use [ref](https://v3.vuejs.org/guide/component-template-refs.html) to get Picker
| Name | Description | Attribute | Return value | | Name | Description | Attribute | Return value |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| confirm | Stop scrolling and emit confirm event | - | - | | confirm | Stop scrolling and emit confirm event | - | - |
| getSelectedOptions | Get current selected options | - | _PickerOption[]_ | | getSelectedOptions | Get current selected options | - | _(PickerOption \| undefined)[]_ |
### Types ### Types

View File

@ -377,7 +377,7 @@ export default {
| 方法名 | 说明 | 参数 | 返回值 | | 方法名 | 说明 | 参数 | 返回值 |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| confirm | 停止惯性滚动并触发 `confirm` 事件 | - | - | | confirm | 停止惯性滚动并触发 `confirm` 事件 | - | - |
| getSelectedOptions | 获取当前选中的选项 | - | _PickerOption[]_ | | getSelectedOptions | 获取当前选中的选项 | - | _(PickerOption \| undefined)[]_ |
### 类型定义 ### 类型定义

View File

@ -24,7 +24,7 @@ export type PickerColumn = PickerOption[];
export type PickerExpose = { export type PickerExpose = {
confirm: () => void; confirm: () => void;
getSelectedOptions: () => PickerOption[]; getSelectedOptions: () => Array<PickerOption | undefined>;
}; };
export type PickerColumnProvide = { export type PickerColumnProvide = {
@ -45,7 +45,7 @@ export type PickerInstance = ComponentPublicInstance<PickerProps, PickerExpose>;
export type PickerConfirmEventParams = { export type PickerConfirmEventParams = {
selectedValues: Array<number | string>; selectedValues: Array<number | string>;
selectedOptions: PickerOption[]; selectedOptions: Array<PickerOption | undefined>;
}; };
export type PickerCancelEventParams = PickerConfirmEventParams; export type PickerCancelEventParams = PickerConfirmEventParams;

View File

@ -2,9 +2,10 @@ import { isDef, clamp, extend } from '../utils';
import type { Ref } from 'vue'; import type { Ref } from 'vue';
import type { PickerOption, PickerColumn, PickerFieldNames } from './types'; import type { PickerOption, PickerColumn, PickerFieldNames } from './types';
export function getFirstEnabledOption(options: PickerOption[]) { export const getFirstEnabledOption = (
return options.find((option) => !option.disabled) || options[0]; options: PickerOption[]
} ): PickerOption | undefined =>
options.find((option) => !option.disabled) || options[0];
export function getColumnsType( export function getColumnsType(
columns: PickerColumn | PickerColumn[], columns: PickerColumn | PickerColumn[],
@ -50,7 +51,7 @@ export function findOptionByValue(
options: PickerOption[], options: PickerOption[],
value: number | string, value: number | string,
fields: Required<PickerFieldNames> fields: Required<PickerFieldNames>
) { ): PickerOption | undefined {
const index = options.findIndex((option) => option[fields.value] === value); const index = options.findIndex((option) => option[fields.value] === value);
const enabledIndex = findIndexOfEnabledOption(options, index); const enabledIndex = findIndexOfEnabledOption(options, index);
return options[enabledIndex]; return options[enabledIndex];
@ -77,7 +78,7 @@ export function formatCascadeColumns(
: undefined; : undefined;
if (!cursor && options.length) { if (!cursor && options.length) {
const firstValue = getFirstEnabledOption(options)[fields.value]; const firstValue = getFirstEnabledOption(options)![fields.value];
cursor = findOptionByValue(options, firstValue, fields); cursor = findOptionByValue(options, firstValue, fields);
} }