mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore(Area): adjust some codes
This commit is contained in:
parent
95cf27745b
commit
6bba2906bc
@ -16,7 +16,7 @@ import {
|
|||||||
createNamespace,
|
createNamespace,
|
||||||
} from '../utils';
|
} from '../utils';
|
||||||
import { pickerSharedProps } from '../picker/Picker';
|
import { pickerSharedProps } from '../picker/Picker';
|
||||||
import { formatDataForCascade } from './utils';
|
import { INHERIT_PROPS, INHERIT_SLOTS, formatDataForCascade } from './utils';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import { Picker } from '../picker';
|
import { Picker } from '../picker';
|
||||||
@ -26,25 +26,6 @@ import type { AreaList } from './types';
|
|||||||
|
|
||||||
const [name, bem] = createNamespace('area');
|
const [name, bem] = createNamespace('area');
|
||||||
|
|
||||||
const INHERIT_SLOTS = [
|
|
||||||
'title',
|
|
||||||
'cancel',
|
|
||||||
'confirm',
|
|
||||||
'toolbar',
|
|
||||||
'columns-top',
|
|
||||||
'columns-bottom',
|
|
||||||
] as const;
|
|
||||||
const INHERIT_PROPS = [
|
|
||||||
'title',
|
|
||||||
'loading',
|
|
||||||
'readonly',
|
|
||||||
'optionHeight',
|
|
||||||
'swipeDuration',
|
|
||||||
'visibleOptionNum',
|
|
||||||
'cancelButtonText',
|
|
||||||
'confirmButtonText',
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
const areaProps = extend({}, pickerSharedProps, {
|
const areaProps = extend({}, pickerSharedProps, {
|
||||||
modelValue: String,
|
modelValue: String,
|
||||||
columnsNum: makeNumericProp(3),
|
columnsNum: makeNumericProp(3),
|
||||||
@ -66,15 +47,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const codes = ref<string[]>([]);
|
const codes = ref<string[]>([]);
|
||||||
|
const columns = computed(() => formatDataForCascade(props));
|
||||||
const columns = computed(() =>
|
|
||||||
formatDataForCascade(
|
|
||||||
props.areaList,
|
|
||||||
props.columnsNum,
|
|
||||||
props.columnsPlaceholder
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
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);
|
||||||
const onConfirm = (...args: unknown[]) => emit('confirm', ...args);
|
const onConfirm = (...args: unknown[]) => emit('confirm', ...args);
|
||||||
|
@ -1,8 +1,27 @@
|
|||||||
import type { AreaList } from '.';
|
import type { AreaProps } from '.';
|
||||||
import type { PickerOption } from '../picker';
|
import type { PickerOption } from '../picker';
|
||||||
|
|
||||||
const EMPTY_CODE = '000000';
|
const EMPTY_CODE = '000000';
|
||||||
|
|
||||||
|
export const INHERIT_SLOTS = [
|
||||||
|
'title',
|
||||||
|
'cancel',
|
||||||
|
'confirm',
|
||||||
|
'toolbar',
|
||||||
|
'columns-top',
|
||||||
|
'columns-bottom',
|
||||||
|
] as const;
|
||||||
|
export const INHERIT_PROPS = [
|
||||||
|
'title',
|
||||||
|
'loading',
|
||||||
|
'readonly',
|
||||||
|
'optionHeight',
|
||||||
|
'swipeDuration',
|
||||||
|
'visibleOptionNum',
|
||||||
|
'cancelButtonText',
|
||||||
|
'confirmButtonText',
|
||||||
|
] as const;
|
||||||
|
|
||||||
const makeOption = (
|
const makeOption = (
|
||||||
text = '',
|
text = '',
|
||||||
value = EMPTY_CODE,
|
value = EMPTY_CODE,
|
||||||
@ -13,20 +32,19 @@ const makeOption = (
|
|||||||
children,
|
children,
|
||||||
});
|
});
|
||||||
|
|
||||||
export function formatDataForCascade(
|
export function formatDataForCascade({
|
||||||
{ city_list: city, county_list: county, province_list: province }: AreaList,
|
areaList,
|
||||||
columnsNum: number | string,
|
columnsNum,
|
||||||
placeholder: string[]
|
columnsPlaceholder: placeholder,
|
||||||
) {
|
}: AreaProps) {
|
||||||
|
const {
|
||||||
|
city_list: city,
|
||||||
|
county_list: county,
|
||||||
|
province_list: province,
|
||||||
|
} = areaList;
|
||||||
const showCity = columnsNum > 1;
|
const showCity = columnsNum > 1;
|
||||||
const showCounty = columnsNum > 2;
|
const showCounty = columnsNum > 2;
|
||||||
|
|
||||||
const getCityChildren = () => {
|
|
||||||
if (showCounty) {
|
|
||||||
return placeholder.length ? [makeOption(placeholder[1])] : [];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getProvinceChildren = () => {
|
const getProvinceChildren = () => {
|
||||||
if (showCity) {
|
if (showCity) {
|
||||||
return placeholder.length
|
return placeholder.length
|
||||||
@ -45,6 +63,12 @@ export function formatDataForCascade(
|
|||||||
|
|
||||||
const cityMap = new Map<string, PickerOption>();
|
const cityMap = new Map<string, PickerOption>();
|
||||||
if (showCity) {
|
if (showCity) {
|
||||||
|
const getCityChildren = () => {
|
||||||
|
if (showCounty) {
|
||||||
|
return placeholder.length ? [makeOption(placeholder[1])] : [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Object.keys(city).forEach((code) => {
|
Object.keys(city).forEach((code) => {
|
||||||
const option = makeOption(city[code], code, getCityChildren());
|
const option = makeOption(city[code], code, getCityChildren());
|
||||||
cityMap.set(code.slice(0, 4), option);
|
cityMap.set(code.slice(0, 4), option);
|
||||||
@ -68,9 +92,7 @@ export function formatDataForCascade(
|
|||||||
const options = Array.from(provinceMap.values()) as PickerOption[];
|
const options = Array.from(provinceMap.values()) as PickerOption[];
|
||||||
|
|
||||||
if (placeholder.length) {
|
if (placeholder.length) {
|
||||||
const county = showCounty
|
const county = showCounty ? [makeOption(placeholder[2])] : undefined;
|
||||||
? [makeOption(placeholder[2], EMPTY_CODE)]
|
|
||||||
: undefined;
|
|
||||||
const city = showCity
|
const city = showCity
|
||||||
? [makeOption(placeholder[1], EMPTY_CODE, county)]
|
? [makeOption(placeholder[1], EMPTY_CODE, county)]
|
||||||
: undefined;
|
: undefined;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user