Skip to content

Cascader ^0.7.0

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

📌 Platform Compatibility

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

🏯 Basic Usage

html
<template>
    <hy-cell>
        <hy-cell-item
            title="Select Region"
            :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: 'Gansu Province',
            children: [
                {
                    value: 'jinchang',
                    label: 'Jinchang City',
                    children: [
                        { value: 'jinchuan', label: 'Jinchuan District' },
                        { value: 'yongchang', label: 'Yongchang County' },
                    ],
                },
            ],
        },
    ]);

    const onConfirm = (params) => {
        console.log('Confirmed selection:', params);
        showCascader.value = false;
    };
</script>

With Input Box

html
<hy-cascader
    v-model="selectedValue"
    :options="options"
    has-input
    title="Please select an address"
    placeholder="Please select a region"
></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: 'Beijing',
        areas: [{ code: '100101', name: 'Chaoyang District' }],
    },
]);

Asynchronous Loading

html
<hy-cascader
    v-model="selectedValue"
    v-model:show="showCascader"
    :lazy-load="lazyLoad"
    title="Async loading example"
></hy-cascader>
ts
const lazyLoad = (option: any, tabIndex: number, resolve: (children: any[]) => void) => {
    // Simulate asynchronous loading
    setTimeout(() => {
        const children = [
            { value: '1', label: 'Option 1' },
            { value: '2', label: 'Option 2', isLeaf: true },
        ];
        resolve(children);
    }, 800);
};

API

Cascader Props

ParameterDescriptionTypeDefault Value
modelValueCurrently selected valueCascaderValue{ value: [], label: [] }
showWhether to show the cascader popupbooleanfalse
optionsCascader data sourceCascaderOption[]-
showToolbarWhether to show the top toolbarbooleantrue
titleTop titlestring-
placeholderPlaceholder text for the input boxstringPlease select
closeOnClickOverlayWhether to close when the overlay is clickedbooleanfalse
zIndexz-index value of the popup layernumber10076
hasInputWhether to show the input boxbooleanfalse
inputInput box configurationHyInputProps-
separatorSeparator used for multiple selectionstring/
valueKeyKey corresponding to the option valuestringvalue
labelKeyKey corresponding to the option labelstringlabel
childrenKeyKey corresponding to the option childrenstringchildren
lazyLoadCallback function for asynchronously loading child nodes; when provided, async loading mode is enabledCascaderLazyLoad-
isLeafKeyKey in the option object that identifies a leaf nodestringisLeaf

Events

Event NameDescriptionCallback Parameters
closeTriggered when the popup closes-
cancelTriggered when the selection is cancelled-
confirmTriggered when the selection is confirmedCascaderEmitValue
changeTriggered when the value changesCascaderEmitValue
update:showTriggered when the popup show state changesboolean
update:modelValueTriggered when the value changesCascaderValue

Slots

Slot NameDescriptionAccepted Values
defaultDefault slot for customizing the input box 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;
01:10