123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167 |
- import React, { useState } from 'react';
- import { Icon, Button, Form, Popover, Tag } from '../../index';
- import TreeSelect from '../index';
- import { flattenDeep } from 'lodash';
- import CustomTrigger from './CustomTrigger';
- import { IconCreditCard } from '@douyinfe/semi-icons';
- const TreeNode = TreeSelect.TreeNode;
- export default {
- title: 'TreeSelect',
- parameters: {
- chromatic: { disableSnapshot: true },
- },
- }
- const treeData1 = [
- {
- label: 'Node1',
- value: '0-0',
- key: '0-0',
- children: [
- {
- label: 'Child Node1',
- value: '0-0-1',
- key: '0-0-1',
- },
- {
- label: 'Child Node2',
- value: '0-0-2',
- key: '0-0-2',
- },
- ],
- },
- {
- label: 'Node2',
- value: '0-1',
- key: '0-1',
- },
- ];
- const treeData2 = [
- {
- label: '亚洲',
- value: 'yazhou',
- key: 'yazhou',
- children: [
- {
- label: '中国',
- value: 'zhongguo',
- key: 'zhongguo',
- children: [
- {
- label: '北京',
- value: 'beijing',
- key: 'beijing',
- },
- {
- label: '上海',
- value: 'shanghai',
- key: 'shanghai',
- },
- ],
- },
- // {
- // label: '日本',
- // value: 'riben',
- // key: 'riben',
- // children: [
- // {
- // label: '东京',
- // value: 'dongjing',
- // key: 'dongjing'
- // },
- // {
- // label: '大阪',
- // value: 'daban',
- // key: 'daban'
- // }
- // ]
- // },
- ],
- },
- {
- label: '北美洲',
- value: 'beimeizhou',
- key: 'beimeizhou',
- children: [
- {
- label: '美国',
- value: 'meiguo',
- key: 'meiguo',
- },
- {
- label: '加拿大',
- value: 'jianada',
- key: 'jianada',
- },
- ],
- },
- ];
- const treeDataWithoutValue = [
- {
- label: '亚洲',
- key: 'yazhou',
- children: [
- {
- label: '中国',
- key: 'zhongguo',
- disabled: true,
- children: [
- {
- label: '北京',
- key: 'beijing',
- },
- {
- label: '上海',
- key: 'shanghai',
- },
- ],
- },
- {
- label: '日本',
- key: 'riben',
- children: [
- {
- label: '东京',
- key: 'dongjing',
- },
- {
- label: '大阪',
- key: 'daban',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- key: 'beimeizhou',
- children: [
- {
- label: '美国',
- key: 'meiguo',
- },
- {
- label: '加拿大',
- key: 'jianada',
- },
- ],
- },
- ];
- class SimpleTree extends React.Component {
- render() {
- return (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
- treeData={treeData1}
- placeholder="Please select"
- onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
- onSelect={(e, bool) => console.log('select', e, bool)}
- onChange={(e, node) => console.log('change', e, node)}
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="Please select"
- />
- </div>
- );
- }
- }
- export const _TreeSelect = () => {
- const treeData = [
- {
- label: '亚洲',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: '中国',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: '北京',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- ];
- return (
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="请选择"
- />
- );
- };
- _TreeSelect.story = {
- name: 'tree select',
- };
- export const Searchable = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- searchAutoFocus
- placeholder="searchAutoFocus"
- />
- </div>
- );
- Searchable.story = {
- name: 'searchable',
- };
- export const SearchPosition = () => (
- <>
- <TreeSelect
- searchPosition="trigger"
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- placeholder="单选"
- />
- <br />
- <br />
- <TreeSelect
- searchPosition="trigger"
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- maxTagCount={2}
- placeholder="多选"
- />
- <br />
- <br />
- <TreeSelect
- searchPosition="trigger"
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- maxTagCount={2}
- placeholder="searchAutoFocus"
- searchAutoFocus
- />
- <br />
- <br />
- <TreeSelect
- searchPosition="trigger"
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- maxTagCount={1}
- placeholder="maxTagCount=1"
- />
- </>
- );
- SearchPosition.story = {
- name: 'searchPosition',
- };
- export const PrefixSuffixInsetLabel = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- prefix={<IconCreditCard />}
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- prefix={<span>1234</span>}
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- suffix="RMB"
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- insetLabel="blablabla"
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- insetLabel={<span>1234</span>}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="Please select"
- />
- </div>
- );
- PrefixSuffixInsetLabel.story = {
- name: 'prefix suffix insetLabel',
- };
- PrefixSuffixInsetLabel.parameters = {
- chromatic: { disableSnapshot: false },
- }
- export const ValidateStatus = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- validateStatus="warning"
- placeholder="Please select"
- onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
- onSelect={(e, bool) => console.log('select', e, bool)}
- onChange={e => console.log('change', e)}
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- validateStatus="error"
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- // multiple
- placeholder="Please select"
- />
- </div>
- );
- ValidateStatus.story = {
- name: 'validate status',
- };
- ValidateStatus.parameters = {
- chromatic: { disableSnapshot: false },
- }
- export const Multiple = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- placeholder="Please select"
- onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
- onSelect={(e, bool) => console.log('select', e, bool)}
- onChange={e => console.log('change', e)}
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- placeholder="Please select"
- />
- </div>
- );
- Multiple.story = {
- name: 'multiple',
- };
- export const MaxTagCount = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- maxTagCount={2}
- placeholder="Please select"
- />
- {/* <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- placeholder="Please select"
- /> */}
- </div>
- );
- MaxTagCount.story = {
- name: 'maxTagCount',
- };
- export const MultipleSearchable = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- </div>
- );
- MultipleSearchable.story = {
- name: 'multiple searchable',
- };
- export const DefaultValues = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- defaultValue={'shanghai'}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- // multiple
- filterTreeNode
- treeNodeFilterProp="value"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- defaultValue={'shanghai'}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- multiple
- treeNodeFilterProp="value"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- defaultValue={['shanghai', 'daban', 'dongjing']}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- multiple
- treeNodeFilterProp="value"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- defaultValue={['meiguo', 'dongjing']}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- multiple
- treeNodeFilterProp="value"
- />
- </div>
- );
- DefaultValues.story = {
- name: 'default values',
- };
- export const Disabled = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- defaultValue={'shanghai'}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- disabled
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- defaultValue={'shanghai'}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- disabled
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- <br />
- <br />
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- disabled
- treeNodeFilterProp="value"
- placeholder="Please select"
- />
- </div>
- );
- Disabled.story = {
- name: 'disabled',
- };
- Disabled.parameters = {
- chromatic: { disableSnapshot: false },
- }
- export const OptionLabelProp = () => (
- <>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- treeNodeFilterProp="value"
- treeNodeLabelProp="value"
- placeholder="Please select"
- />
- </>
- );
- OptionLabelProp.story = {
- name: 'optionLabelProp',
- };
- export const ValueInArray = () => (
- <>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- filterTreeNode
- valueInArray
- onChange={(...args) => console.log(args)}
- treeNodeFilterProp="value"
- treeNodeLabelProp="value"
- placeholder="Please select"
- />
- </>
- );
- ValueInArray.story = {
- name: 'valueInArray',
- };
- export const OnBlurOnFocus = () => (
- <>
- <div>single</div>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- onBlur={(...args) => console.log('blur', args)}
- onFocus={(...args) => console.log('focus', args)}
- placeholder="Please select"
- />
- <div>multiple</div>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- onBlur={(...args) => console.log('blur', args)}
- onFocus={(...args) => console.log('focus', args)}
- placeholder="Please select"
- />
- </>
- );
- OnBlurOnFocus.story = {
- name: 'onBlur/onFocus',
- };
- export const LeafOnly = () => (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- // value={this.state.value}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- multiple
- leafOnly
- placeholder="Please select"
- onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
- onSelect={(e, bool) => console.log('select', e, bool)}
- onChange={e => console.log('change', e)}
- />
- </div>
- );
- LeafOnly.story = {
- name: 'leafOnly',
- };
- class Demo extends React.Component {
- constructor() {
- super();
- this.state = {
- value: 'shanghai',
- };
- }
- onChange(value) {
- this.setState({ value });
- }
- render() {
- return (
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- value={this.state.value}
- placeholder="Please select"
- onChange={e => this.onChange(e)}
- />
- );
- }
- }
- export const ControlledComponentSingle = () => <Demo />;
- ControlledComponentSingle.story = {
- name: 'controlled Component single',
- };
- class Demo2 extends React.Component {
- constructor() {
- super();
- this.state = {
- value: ['Shanghai'],
- };
- }
- onChange(value) {
- this.setState({ value });
- }
- render() {
- console.log(this.state.value);
- const treeData = [
- {
- label: '亚洲',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: '中国',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: '北京',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- ];
- return (
- <TreeSelect
- style={{ width: 300 }}
- multiple
- dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
- treeData={treeData}
- value={this.state.value}
- placeholder="Please select"
- onChange={e => this.onChange(e)}
- />
- );
- }
- }
- export const ControlledComponentMultiple = () => <Demo2 />;
- ControlledComponentMultiple.story = {
- name: 'controlled Component multiple',
- };
- class Demo3 extends React.Component {
- constructor() {
- super();
- this.state = {
- value: ['Shanghai'],
- };
- }
- // 获取最底层值
- getDeepChildrensByNode = node => {
- return flattenDeep(
- node.map(item => {
- if (item.children) {
- return this.getDeepChildrensByNode(item.children);
- }
- return item.value;
- })
- );
- };
- onChange(value, node) {
- console.log('onchange', value);
- value = this.getDeepChildrensByNode(node);
- console.log('modifiled', value);
- this.setState({ value });
- }
- render() {
- const treeData = [
- {
- label: '亚洲',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: '中国',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: '北京',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- ];
- return (
- <TreeSelect
- style={{ width: 300 }}
- multiple
- dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
- treeData={treeData}
- value={this.state.value}
- placeholder="Please select"
- onChange={(e, node) => this.onChange(e, node)}
- />
- );
- }
- }
- export const ControlledComponentMultipleValueModified = () => <Demo3 />;
- ControlledComponentMultipleValueModified.story = {
- name: 'controlled Component multiple value modified',
- };
- class ConvertDemo extends React.Component {
- constructor(props) {
- super(props);
- this.formApi = null;
- }
- handleChange = val => {
- let finalVal = val;
- let firstClassOption = ['Asia', 'North America'];
- // 在这里去做你的value替换逻辑
- console.log('originVal:' + val);
- if (val.length === 1) {
- // do nothing
- } else {
- if (val.every(item => firstClassOption.includes(item))) {
- finalVal = val[val.length - 1];
- }
- }
- console.log('finalVal:' + finalVal);
- return finalVal;
- };
- render() {
- const treeData = [
- {
- label: '亚洲',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: '中国',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: '北京',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- ];
- return (
- <Form getFormApi={this.getFormApi}>
- <Form.TreeSelect
- field="tree"
- label="节点(TreeSelect)"
- placeholder="请选择服务节点"
- treeData={treeData}
- convert={this.handleChange}
- filterTreeNode
- multiple
- ></Form.TreeSelect>
- </Form>
- );
- }
- }
- export const _ConvertDemo = () => <ConvertDemo />;
- _ConvertDemo.story = {
- name: 'convert demo',
- };
- export const TreeselectDefaultOpenInPopover = () => (
- <Popover
- content={
- <TreeSelect
- style={{ width: 200 }}
- dropdownStyle={{ width: 200 }}
- treeData={treeData1}
- defaultOpen
- // value={this.state.value}
- // placeholder="Please select"
- // onChange={(e, node) => this.onChange(e, node)}
- />
- }
- >
- <Button style={{ marginLeft: 150 }}>悬停此处</Button>
- </Popover>
- );
- TreeselectDefaultOpenInPopover.story = {
- name: 'treeselect defaultOpen in popover',
- };
- export const CustomTriggerDemo = () => <CustomTrigger />;
- CustomTriggerDemo.story = { name: 'custom trigger' };
- const AutoParentDemo = () => {
- const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
- const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
- const [autoExpandParent, setAutoExpandParent] = useState(true);
- const onExpand = expandedKeys => {
- console.log('onExpand', expandedKeys);
- // if not set autoExpandParent to false, if children expanded, parent can not collapse.
- // or, you can remove all expanded children keys.
- setExpandedKeys(expandedKeys);
- setAutoExpandParent(false);
- };
- const onSelect = (selectedKeys, info) => {
- console.log('onSelect:', info);
- setSelectedKeys(selectedKeys);
- };
- return (
- <div>
- <Button
- onClick={() => {
- setSelectedKeys(['beijing']);
- setExpandedKeys(['beijing']);
- setAutoExpandParent(true);
- }}
- >
- Update
- </Button>
- <TreeSelect
- onExpand={onExpand}
- expandedKeys={expandedKeys}
- autoExpandParent={autoExpandParent}
- onChange={onSelect}
- value={selectedKeys}
- treeData={treeData2}
- // multiple
- />
- </div>
- );
- };
- export const AutoExpandParent = () => (
- <>
- <AutoParentDemo />
- </>
- );
- AutoExpandParent.story = {
- name: 'autoExpandParent',
- };
- export const TreeWithoutValueProps = () => (
- <TreeSelect
- treeData={treeDataWithoutValue}
- value="beijing"
- defaultExpandAll
- onChange={(...args) => console.log(args)}
- />
- );
- TreeWithoutValueProps.story = {
- name: 'tree without value props',
- };
- export const TreeSelectRenderSelectedItem = () => {
- const treeData = [
- {
- label: '亚洲',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: '中国',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: '北京',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- {
- label: '南美洲',
- value: 'South America',
- key: '2',
- },
- {
- label: '南极洲',
- value: 'Antarctica',
- key: '3',
- },
- ];
- return (
- <>
- <h4>单选</h4>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- renderSelectedItem={item => `${item.value}-${item.label}`}
- />
- <h4>多选</h4>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- multiple
- placeholder="请选择"
- renderSelectedItem={(item, { index, onClose }) => ({
- content: `${item.value}-${item.label}`,
- isRenderInTag: true,
- })}
- />
- <h4>多选 + isRenderInTag=false</h4>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- multiple
- maxTagCount={2}
- placeholder="请选择"
- renderSelectedItem={(item, { index, onClose }) => ({
- content: (
- <Tag key={index} color="white">
- {item.value}
- </Tag>
- ),
- isRenderInTag: false,
- })}
- />
- </>
- );
- };
- TreeSelectRenderSelectedItem.story = {
- name: 'treeSelect renderSelectedItem',
- };
- const DisableStrictlyDemo = () => {
- const [value, setValue] = useState(['Shanghai']);
- const treeData = [
- {
- label: '亚洲',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: '中国',
- value: 'China',
- key: '0-0',
- disabled: true,
- children: [
- {
- label: '北京',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- {
- label: '日本',
- value: 'Japan',
- key: '0-1',
- },
- ],
- },
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- ];
- return (
- <div>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- disableStrictly
- multiple
- searchPosition="trigger"
- filterTreeNode
- value={value}
- onChange={value => setValue(value)}
- />
- </div>
- );
- };
- export const DisabledStrictly = () => (
- <>
- <DisableStrictlyDemo />
- </>
- );
- DisabledStrictly.story = {
- name: 'disabledStrictly',
- };
|