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 &&
!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 |
| --- | --- | --- | --- |
| confirm | Stop scrolling and emit confirm event | - | - |
| getSelectedOptions | Get current selected options | - | _PickerOption[]_ |
| getSelectedOptions | Get current selected options | - | _(PickerOption \| undefined)[]_ |
### Types

View File

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

View File

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

View File

@ -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<PickerFieldNames>
) {
): 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);
}