mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-08-29 12:23:20 +08:00
feat(Area): add getSelectedOptions method
This commit is contained in:
parent
83295e655d
commit
35a50012c7
@ -18,11 +18,15 @@ import {
|
|||||||
import { pickerSharedProps } from '../picker/Picker';
|
import { pickerSharedProps } from '../picker/Picker';
|
||||||
import { INHERIT_PROPS, INHERIT_SLOTS, formatDataForCascade } from './utils';
|
import { INHERIT_PROPS, INHERIT_SLOTS, formatDataForCascade } from './utils';
|
||||||
|
|
||||||
|
// Composables
|
||||||
|
import { useExpose } from '../composables/use-expose';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import { Picker } from '../picker';
|
import { Picker, type PickerInstance } from '../picker';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import type { AreaList } from './types';
|
import type { AreaList } from './types';
|
||||||
|
import type { PickerExpose } from '../picker/types';
|
||||||
|
|
||||||
const [name, bem] = createNamespace('area');
|
const [name, bem] = createNamespace('area');
|
||||||
|
|
||||||
@ -47,6 +51,8 @@ export default defineComponent({
|
|||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const codes = ref<string[]>([]);
|
const codes = ref<string[]>([]);
|
||||||
|
const picker = ref<PickerInstance>();
|
||||||
|
|
||||||
const columns = computed(() => formatDataForCascade(props));
|
const columns = computed(() => formatDataForCascade(props));
|
||||||
const onChange = (...args: unknown[]) => emit('change', ...args);
|
const onChange = (...args: unknown[]) => emit('change', ...args);
|
||||||
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
|
const onCancel = (...args: unknown[]) => emit('cancel', ...args);
|
||||||
@ -84,8 +90,14 @@ export default defineComponent({
|
|||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useExpose<PickerExpose>({
|
||||||
|
confirm: () => picker.value?.confirm(),
|
||||||
|
getSelectedOptions: () => picker.value?.getSelectedOptions() || [],
|
||||||
|
});
|
||||||
|
|
||||||
return () => (
|
return () => (
|
||||||
<Picker
|
<Picker
|
||||||
|
ref={picker}
|
||||||
v-model={codes.value}
|
v-model={codes.value}
|
||||||
v-slots={pick(slots, INHERIT_SLOTS)}
|
v-slots={pick(slots, INHERIT_SLOTS)}
|
||||||
class={bem()}
|
class={bem()}
|
||||||
|
@ -152,6 +152,15 @@ export default {
|
|||||||
| columns-top | Custom content above columns | - |
|
| columns-top | Custom content above columns | - |
|
||||||
| columns-bottom | Custom content below 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
|
### Types
|
||||||
|
|
||||||
The component exports the following type definitions:
|
The component exports the following type definitions:
|
||||||
@ -168,5 +177,5 @@ import type { AreaInstance } from 'vant';
|
|||||||
|
|
||||||
const areaRef = ref<AreaInstance>();
|
const areaRef = ref<AreaInstance>();
|
||||||
|
|
||||||
areaRef.value?.reset();
|
areaRef.value?.confirm();
|
||||||
```
|
```
|
||||||
|
@ -154,6 +154,15 @@ export default {
|
|||||||
| columns-top | 自定义选项上方内容 | - |
|
| columns-top | 自定义选项上方内容 | - |
|
||||||
| columns-bottom | 自定义选项下方内容 | - |
|
| 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>();
|
const areaRef = ref<AreaInstance>();
|
||||||
|
|
||||||
areaRef.value?.reset();
|
areaRef.value?.confirm();
|
||||||
```
|
```
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/* eslint-disable camelcase */
|
/* eslint-disable camelcase */
|
||||||
import type { ComponentPublicInstance } from 'vue';
|
import type { ComponentPublicInstance } from 'vue';
|
||||||
|
import { PickerExpose } from '../picker/types';
|
||||||
import type { AreaProps } from './Area';
|
import type { AreaProps } from './Area';
|
||||||
|
|
||||||
export type AreaList = {
|
export type AreaList = {
|
||||||
@ -8,4 +9,4 @@ export type AreaList = {
|
|||||||
province_list: Record<string, string>;
|
province_list: Record<string, string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AreaInstance = ComponentPublicInstance<AreaProps>;
|
export type AreaInstance = ComponentPublicInstance<AreaProps, PickerExpose>;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import type { AreaProps } from '.';
|
import type { AreaProps } from '.';
|
||||||
import type { PickerOption } from '../picker';
|
import type { PickerOption } from '../picker';
|
||||||
|
|
||||||
const EMPTY_CODE = '000000';
|
export const AREA_EMPTY_CODE = '000000';
|
||||||
|
|
||||||
export const INHERIT_SLOTS = [
|
export const INHERIT_SLOTS = [
|
||||||
'title',
|
'title',
|
||||||
@ -24,8 +24,8 @@ export const INHERIT_PROPS = [
|
|||||||
|
|
||||||
const makeOption = (
|
const makeOption = (
|
||||||
text = '',
|
text = '',
|
||||||
value = EMPTY_CODE,
|
value = AREA_EMPTY_CODE,
|
||||||
children?: PickerOption[]
|
children: PickerOption[] | undefined = undefined
|
||||||
): PickerOption => ({
|
): PickerOption => ({
|
||||||
text,
|
text,
|
||||||
value,
|
value,
|
||||||
@ -48,7 +48,13 @@ export function formatDataForCascade({
|
|||||||
const getProvinceChildren = () => {
|
const getProvinceChildren = () => {
|
||||||
if (showCity) {
|
if (showCity) {
|
||||||
return placeholder.length
|
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) {
|
if (placeholder.length) {
|
||||||
const county = showCounty ? [makeOption(placeholder[2])] : undefined;
|
const county = showCounty ? [makeOption(placeholder[2])] : undefined;
|
||||||
const city = showCity
|
const city = showCity
|
||||||
? [makeOption(placeholder[1], EMPTY_CODE, county)]
|
? [makeOption(placeholder[1], AREA_EMPTY_CODE, county)]
|
||||||
: undefined;
|
: undefined;
|
||||||
options.unshift(makeOption(placeholder[0], EMPTY_CODE, city));
|
options.unshift(makeOption(placeholder[0], AREA_EMPTY_CODE, city));
|
||||||
}
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user