Skip to content

Cascader ^0.7.0 ​

The cascader is used for selecting data with multiple levels, supporting both static data and lazy loading modes.

📌Platform Differences ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

🏯Basic Usage ​

html
<template>
    <hy-cell>
        <hy-cell-item
            title="选择地区"
            :value="selectedValue.label.join(' / ')"
            @click="showCascader = true"
        ></hy-cell-item>
    </hy-cell>
    <hy-cascader
        v-model="selectedValue"
        v-model:show="showCascader"
        :options="options"
        @confirm="onConfirm"
    ></hy-cascader>
</template>

<script setup lang="ts">
    import { ref } from 'vue';

    const showCascader = ref(false);
    const selectedValue = ref({ value: [], label: [] });

    const options = ref([
        {
            value: 'gansu',
            label: '甘肃省',
            children: [
                {
                    value: 'jinchang',
                    label: '金昌市',
                    children: [
                        { value: 'jinchuan', label: '金川区' },
                        { value: 'yongchang', label: '永昌县' },
                    ],
                },
            ],
        },
    ]);

    const onConfirm = (params) => {
        console.log('确认选择:', params);
        showCascader.value = false;
    };
</script>

With Input Field ​

html
<hy-cascader
    v-model="selectedValue"
    :options="options"
    has-input
    title="请选择地址"
    placeholder="请选择地区"
></hy-cascader>

Custom Key Names ​

html
<hy-cascader
    v-model="selectedValue"
    :options="options"
    labelKey="name"
    valueKey="code"
    childrenKey="areas"
></hy-cascader>
ts
const options = ref([
    {
        code: '1001',
        name: '北京市',
        areas: [{ code: '100101', name: '朝阳区' }],
    },
]);

Lazy Loading ​

html
<hy-cascader
    v-model="selectedValue"
    v-model:show="showCascader"
    :lazy-load="lazyLoad"
    title="异步加载示例"
></hy-cascader>
ts
const lazyLoad = (option: any, tabIndex: number, resolve: (children: any[]) => void) => {
    // 模拟异步加载
    setTimeout(() => {
        const children = [
            { value: '1', label: '选项1' },
            { value: '2', label: '选项2', isLeaf: true },
        ];
        resolve(children);
    }, 800);
};

API ​

Cascader Props ​

PropDescriptionTypeDefault
modelValueCurrently selected valueCascaderValue{ value: [], label: [] }
showWhether to show the cascader popupbooleanfalse
optionsData source of the cascaderCascaderOption[]-
showToolbarWhether to show the top toolbarbooleantrue
titleTop titlestring-
placeholderPlaceholder text of the input fieldstringPlease select
closeOnClickOverlayWhether to close when the overlay is clickedbooleanfalse
zIndexz-index value of the popup layernumber10076
hasInputWhether to show the input fieldbooleanfalse
inputInput field configurationHyInputProps-
separatorSeparator used for multiple selectionstring/
valueKeyKey that corresponds to the option valuestringvalue
labelKeyKey that corresponds to the option labelstringlabel
childrenKeyKey that corresponds to the option childrenstringchildren
lazyLoadCallback function for asynchronously loading child nodes; providing it enables lazy loading modeCascaderLazyLoad-
isLeafKeyKey in the option object that identifies a leaf nodestringisLeaf

Events ​

Event NameDescriptionCallback Parameters
closeTriggered when the popup is closed-
cancelTriggered when the selection is canceled-
confirmTriggered when the selection is confirmedCascaderEmitValue
changeTriggered when the value changesCascaderEmitValue
update:showTriggered when the popup visibility changesboolean
update:modelValueTriggered when the value changesCascaderValue

Slots ​

Slot NameDescriptionAccepted Value
defaultDefault slot, for customizing input content-

Typings ​

Type Definitions
ts
export interface CascaderOption {
    value: string | number;
    label: string;
    children?: CascaderOption[];
    disabled?: boolean;
    isLeaf?: boolean;
    [key: string]: any;
}

export interface CascaderValue {
    value: (string | number)[];
    label: string[];
}

export interface CascaderEmitValue {
    value: (string | number)[];
    label: string[];
    selectedOptions: CascaderOption[];
}

export type CascaderLazyLoad = (
    option: CascaderOption | null,
    tabIndex: number,
    resolve: (children: CascaderOption[]) => void
) => void;

CSS Variables ​

VariableDescriptionDefault
--hy-cascader-close-sizeClose button size60rpx
--hy-cascader-loading-z-indexLoading overlay z-index10
--hy-cascader-title-font-sizeTitle font sizeFollows --hy-font-size-base (28rpx)
--hy-cascader-title-colorTitle colorFollows --hy-text-color
--hy-cascader-item-font-sizeOption font sizeFollows --hy-font-size-base (28rpx)
--hy-cascader-item-colorOption colorFollows --hy-text-color
--hy-cascader-borderDivider line below the title barFollows --hy-border-line
12:33
battery