Cascader
Cascade selection box.
When To Use#
When you need to select from a set of associated data set. Such as province/city/district, company level, things classification.
When selecting from a large data set, with multi-stage classification separated for easy selection.
Chooses cascade items in one float layer for better user experience.
Examples
Please select
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
ReactDOM.render(
<Cascader options={options} onChange={onChange} placeholder="Please select" />,
mountNode,
);
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
},
],
},
];
class CitySwitcher extends React.Component {
state = {
text: 'Unselect',
};
onChange = (value, selectedOptions) => {
this.setState({
text: selectedOptions.map(o => o.label).join(', '),
});
};
render() {
return (
<span>
{this.state.text}
<Cascader options={options} onChange={this.onChange}>
<a href="#">Change city</a>
</Cascader>
</span>
);
}
}
ReactDOM.render(<CitySwitcher />, mountNode);
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
disabled: true,
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
ReactDOM.render(<Cascader options={options} onChange={onChange} />, mountNode);
import { Cascader } from 'antd';
const options = [
{
label: 'Light',
value: 'light',
children: new Array(20)
.fill(null)
.map((_, index) => ({ label: `Number ${index}`, value: index })),
},
{
label: 'Bamboo',
value: 'bamboo',
children: [
{
label: 'Little',
value: 'little',
children: [
{
label: 'Toy Fish',
value: 'fish',
},
{
label: 'Toy Cards',
value: 'cards',
},
{
label: 'Toy Bird',
value: 'bird',
},
],
},
],
},
];
const App = () => {
const onChange = value => {
console.log(value);
};
return (
<Cascader
style={{ width: '100%' }}
options={options}
onChange={onChange}
multiple
maxTagCount="responsive"
/>
);
};
ReactDOM.render(<App />, mountNode);
4.17.0
Zhejiang / Hangzhou / West Lake (752100)
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
code: 752100,
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
code: 453400,
},
],
},
],
},
];
function handleAreaClick(e, label, option) {
e.stopPropagation();
console.log('clicked', label, option);
}
const displayRender = (labels, selectedOptions) =>
labels.map((label, i) => {
const option = selectedOptions[i];
if (i === labels.length - 1) {
return (
<span key={option.value}>
{label} (<a onClick={e => handleAreaClick(e, label, option)}>{option.code}</a>)
</span>
);
}
return <span key={option.value}>{label} / </span>;
});
ReactDOM.render(
<Cascader
options={options}
defaultValue={['zhejiang', 'hangzhou', 'xihu']}
displayRender={displayRender}
style={{ width: '100%' }}
/>,
mountNode,
);
import { Cascader } from 'antd';
const optionLists = [
{
value: 'zhejiang',
label: 'Zhejiang',
isLeaf: false,
},
{
value: 'jiangsu',
label: 'Jiangsu',
isLeaf: false,
},
];
const LazyOptions = () => {
const [options, setOptions] = React.useState(optionLists);
const onChange = (value, selectedOptions) => {
console.log(value, selectedOptions);
};
const loadData = selectedOptions => {
const targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;
// load options lazily
setTimeout(() => {
targetOption.loading = false;
targetOption.children = [
{
label: `${targetOption.label} Dynamic 1`,
value: 'dynamic1',
},
{
label: `${targetOption.label} Dynamic 2`,
value: 'dynamic2',
},
];
setOptions([...options]);
}, 1000);
};
return <Cascader options={options} loadData={loadData} onChange={onChange} changeOnSelect />;
};
ReactDOM.render(<LazyOptions />, mountNode);
Please select
import { Cascader, Divider } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function dropdownRender(menus) {
return (
<div>
{menus}
<Divider style={{ margin: 0 }} />
<div style={{ padding: 8 }}>The footer is not very short.</div>
</div>
);
}
ReactDOM.render(
<Cascader options={options} dropdownRender={dropdownRender} placeholder="Please select" />,
mountNode,
);
Zhejiang / Hangzhou / West Lake
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
ReactDOM.render(
<Cascader
defaultValue={['zhejiang', 'hangzhou', 'xihu']}
options={options}
onChange={onChange}
/>,
mountNode,
);
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
// Just show the latest item.
function displayRender(label) {
return label[label.length - 1];
}
ReactDOM.render(
<Cascader
options={options}
expandTrigger="hover"
displayRender={displayRender}
onChange={onChange}
/>,
mountNode,
);
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hanzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
ReactDOM.render(<Cascader options={options} onChange={onChange} changeOnSelect />, mountNode);
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
ReactDOM.render(
<>
<Cascader size="large" options={options} onChange={onChange} />
<br />
<br />
<Cascader options={options} onChange={onChange} />
<br />
<br />
<Cascader size="small" options={options} onChange={onChange} />
<br />
<br />
</>,
mountNode,
);
Please select
import { Cascader } from 'antd';
const options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
},
{
value: 'xiasha',
label: 'Xia Sha',
disabled: true,
},
],
},
],
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua men',
},
],
},
],
},
];
function onChange(value, selectedOptions) {
console.log(value, selectedOptions);
}
function filter(inputValue, path) {
return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
}
ReactDOM.render(
<Cascader
options={options}
onChange={onChange}
placeholder="Please select"
showSearch={{ filter }}
onSearch={value => console.log(value)}
/>,
mountNode,
);
Please select
import { Cascader } from 'antd';
const options = [
{
code: 'zhejiang',
name: 'Zhejiang',
items: [
{
code: 'hangzhou',
name: 'Hangzhou',
items: [
{
code: 'xihu',
name: 'West Lake',
},
],
},
],
},
{
code: 'jiangsu',
name: 'Jiangsu',
items: [
{
code: 'nanjing',
name: 'Nanjing',
items: [
{
code: 'zhonghuamen',
name: 'Zhong Hua Men',
},
],
},
],
},
];
function onChange(value) {
console.log(value);
}
ReactDOM.render(
<Cascader
fieldNames={{ label: 'name', value: 'code', children: 'items' }}
options={options}
onChange={onChange}
placeholder="Please select"
/>,
mountNode,
);
API#
<Cascader options={options} onChange={onChange} />
Property | Description | Type | Default | Version |
---|---|---|---|---|
allowClear | Whether allow clear | boolean | true | |
autoFocus | If get focus when component mounted | boolean | false | |
bordered | Whether has border style | boolean | true | |
changeOnSelect | (Work on single select) Change value on each selection if set to true, see above demo for details | boolean | false | |
className | The additional css class | string | - | |
defaultValue | Initial selected value | string[] | number[] | [] | |
disabled | Whether disabled select | boolean | false | |
displayRender | The render function of displaying single selected options. You can use tagRender for multiple mode | (label, selectedOptions) => ReactNode | label => label.join(/ ) | |
dropdownClassName | The additional className of popup overlay | string | - | 4.17.0 |
dropdownRender | Customize dropdown content | (menus: ReactNode) => ReactNode | - | 4.4.0 |
expandIcon | Customize the current item expand icon | ReactNode | - | 4.4.0 |
expandTrigger | expand current item when click or hover, one of click hover | string | click | |
fieldNames | Custom field name for label and value and children | object | { label: label , value: value , children: children } | |
getPopupContainer | Parent Node which the selector should be rendered to. Default to body . When position issues happen, try to modify it into scrollable content and position it relative. example | function(triggerNode) | () => document.body | |
loadData | To load option lazily, and it cannot work with showSearch | (selectedOptions) => void | - | |
maxTagCount | Max tag count to show. responsive will cost render performance | number | responsive | - | 4.17.0 |
maxTagPlaceholder | Placeholder for not showing tags | ReactNode | function(omittedValues) | - | 4.17.0 |
notFoundContent | Specify content to show when no result matches | string | Not Found | |
open | Set visible of cascader popup | boolean | - | 4.17.0 |
options | The data options of cascade | Option[] | - | |
placeholder | The input placeholder | string | Please select | |
placement | Use preset popup align config from builtinPlacements:bottomLeft bottomRight topLeft topRight | string | bottomLeft | 4.17.0 |
showSearch | Whether show search input in single mode | boolean | Object | false | |
size | The input size | large | middle | small | - | |
style | The additional style | CSSProperties | - | |
suffixIcon | The custom suffix icon | ReactNode | - | |
tagRender | Customize tag render when multiple | (props) => ReactNode | - | 4.17.0 |
value | The selected value | string[] | number[] | - | |
onChange | Callback when finishing cascader select | (value, selectedOptions) => void | - | |
onDropdownVisibleChange | Callback when popup shown or hidden | (value) => void | - | 4.17.0 |
multiple | Support multiple or not | boolean | - | 4.17.0 |
searchValue | Set search value,Need work with showSearch | string | - | 4.17.0 |
onSearch | The callback function triggered when input changed | (search: string) => void | - | 4.17.0 |
dropdownMenuColumnStyle | The style of the drop-down menu column | CSSProperties | - | |
loadingIcon | The apparence of lazy loading (now is useless) | ReactNode | - |
showSearch#
Property | Description | Type | Default | Version |
---|---|---|---|---|
filter | The function will receive two arguments, inputValue and option, if the function returns true, the option will be included in the filtered set; Otherwise, it will be excluded | function(inputValue, path): boolean | - | |
limit | Set the count of filtered items | number | false | 50 | |
matchInputWidth | Whether the width of list matches input, (how it looks) | boolean | true | |
render | Used to render filtered options | function(inputValue, path): ReactNode | - | |
sort | Used to sort filtered options | function(a, b, inputValue) | - |
Option#
interface Option {
value: string | number;
label?: React.ReactNode;
disabled?: boolean;
children?: Option[];
}
Methods#
Name | Description | Version |
---|---|---|
blur() | Remove focus | |
focus() | Get focus |