feat(Area): add getSelectedOptions method

This commit is contained in:
chenjiahan 2022-02-16 12:02:46 +08:00
parent 83295e655d
commit 35a50012c7
5 changed files with 47 additions and 10 deletions

View File

@ -18,11 +18,15 @@ import {
import { pickerSharedProps } from '../picker/Picker';
import { INHERIT_PROPS, INHERIT_SLOTS, formatDataForCascade } from './utils';
// Composables
import { useExpose } from '../composables/use-expose';
// Components
import { Picker } from '../picker';
import { Picker, type PickerInstance } from '../picker';
// Types
import type { AreaList } from './types';
import type { PickerExpose } from '../picker/types';
const [name, bem] = createNamespace('area');
@ -47,6 +51,8 @@ export default defineComponent({
setup(props, { emit, slots }) {
const codes = ref<string[]>([]);
const picker = ref<PickerInstance>();
const columns = computed(() => formatDataForCascade(props));
const onChange = (...args: unknown[]) => emit('change', ...args);
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
@ -84,8 +90,14 @@ export default defineComponent({
{ immediate: true }
);
useExpose<PickerExpose>({
confirm: () => picker.value?.confirm(),
getSelectedOptions: () => picker.value?.getSelectedOptions() || [],
});
return () => (
<Picker
ref={picker}
v-model={codes.value}
v-slots={pick(slots, INHERIT_SLOTS)}
class={bem()}

View File

@ -152,6 +152,15 @@ export default {
| columns-top | Custom content above columns | - |
| columns-bottom | Custom content below columns | - |
### Methods
Use [ref](https://v3.vuejs.org/guide/component-template-refs.html) to get Area instance and call instance methods.
| Name | Description | Attribute | Return value |
| --- | --- | --- | --- |
| confirm | Stop scrolling and emit confirm event | - | - |
| getSelectedOptions | Get current selected options | - | _PickerOption[]_ |
### Types
The component exports the following type definitions:
@ -168,5 +177,5 @@ import type { AreaInstance } from 'vant';
const areaRef = ref<AreaInstance>();
areaRef.value?.reset();
areaRef.value?.confirm();
```

View File

@ -154,6 +154,15 @@ export default {
| columns-top | 自定义选项上方内容 | - |
| columns-bottom | 自定义选项下方内容 | - |
### 方法
通过 ref 可以获取到 Area 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。
| 方法名 | 说明 | 参数 | 返回值 |
| --- | --- | --- | --- |
| confirm | 停止惯性滚动并触发 `confirm` 事件 | - | - |
| getSelectedOptions | 获取当前选中的选项 | - | _PickerOption[]_ |
### 类型定义
组件导出以下类型定义:
@ -170,7 +179,7 @@ import type { AreaInstance } from 'vant';
const areaRef = ref<AreaInstance>();
areaRef.value?.reset();
areaRef.value?.confirm();
```
## 常见问题

View File

@ -1,5 +1,6 @@
/* eslint-disable camelcase */
import type { ComponentPublicInstance } from 'vue';
import { PickerExpose } from '../picker/types';
import type { AreaProps } from './Area';
export type AreaList = {
@ -8,4 +9,4 @@ export type AreaList = {
province_list: Record<string, string>;
};
export type AreaInstance = ComponentPublicInstance<AreaProps>;
export type AreaInstance = ComponentPublicInstance<AreaProps, PickerExpose>;

View File

@ -1,7 +1,7 @@
import type { AreaProps } from '.';
import type { PickerOption } from '../picker';
const EMPTY_CODE = '000000';
export const AREA_EMPTY_CODE = '000000';
export const INHERIT_SLOTS = [
'title',
@ -24,8 +24,8 @@ export const INHERIT_PROPS = [
const makeOption = (
text = '',
value = EMPTY_CODE,
children?: PickerOption[]
value = AREA_EMPTY_CODE,
children: PickerOption[] | undefined = undefined
): PickerOption => ({
text,
value,
@ -48,7 +48,13 @@ export function formatDataForCascade({
const getProvinceChildren = () => {
if (showCity) {
return placeholder.length
? [makeOption(placeholder[0], EMPTY_CODE, showCounty ? [] : undefined)]
? [
makeOption(
placeholder[0],
AREA_EMPTY_CODE,
showCounty ? [] : undefined
),
]
: [];
}
};
@ -94,9 +100,9 @@ export function formatDataForCascade({
if (placeholder.length) {
const county = showCounty ? [makeOption(placeholder[2])] : undefined;
const city = showCity
? [makeOption(placeholder[1], EMPTY_CODE, county)]
? [makeOption(placeholder[1], AREA_EMPTY_CODE, county)]
: undefined;
options.unshift(makeOption(placeholder[0], EMPTY_CODE, city));
options.unshift(makeOption(placeholder[0], AREA_EMPTY_CODE, city));
}
return options;