1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341 |
- import React, { useRef, useState } from 'react';
- import Tree from '../index';
- import AutoSizer from '../autoSizer';
- import { Button, ButtonGroup, Input, Popover, Toast } from '../../index';
- import BigTree from './BigData';
- import testData from './data';
- import { cloneDeep, difference, isEqual } from 'lodash';
- import { IconEdit, IconMapPin, IconMore } from '@douyinfe/semi-icons';
- const TreeNode = Tree.TreeNode;
- export default {
- title: 'Tree'
- }
- const treeChildren = [
- {
- label: '北京',
- value: 'beijing',
- key: 'beijing',
- },
- {
- label: '上海',
- value: 'shanghai',
- key: 'shanghai',
- },
- ];
- const treeData1 = [
- {
- label: '亚洲',
- value: 'yazhou',
- key: 'yazhou',
- children: [
- {
- label: '中国',
- value: 'zhongguo',
- key: 'zhongguo',
- disabled: true,
- children: treeChildren,
- },
- {
- 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: treeChildren,
- },
- {
- label: '日本',
- key: 'riben',
- children: [
- {
- label: '东京',
- key: 'dongjing',
- },
- {
- label: '大阪',
- key: 'daban',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- key: 'beimeizhou',
- children: [
- {
- label: '美国',
- key: 'meiguo',
- },
- {
- label: '加拿大',
- key: 'jianada',
- },
- ],
- },
- ];
- const treeDataWithIcon = [
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- icon: <IconMapPin style={{ color: 'var(--semi-color-text-2)' }} />,
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- icon: <IconMapPin style={{ color: 'var(--semi-color-text-2)' }} />,
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- icon: <IconMapPin style={{ color: 'var(--semi-color-text-2)' }} />,
- },
- ],
- },
- ];
- let opts = {
- content: 'Hi, Bytedance dance dance',
- duration: 3,
- };
- const treeDataWithNode = [
- {
- label: (
- <span>
- <span style={{ marginRight: 30 }}>亚洲</span>
- <Button
- style={{ zIndex: 2 }}
- onClick={e => {
- Toast.info(opts);
- e.stopPropagation();
- }}
- >
- Display Toast
- </Button>
- </span>
- ),
- 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',
- },
- ],
- },
- {
- label: '北美洲',
- value: 'beimeizhou',
- key: 'beimeizhou',
- children: [
- {
- label: '美国',
- value: 'meiguo',
- key: 'meiguo',
- },
- {
- label: '加拿大',
- value: 'jianada',
- key: 'jianada',
- },
- ],
- },
- ];
- const treeJson = {
- Node1: {
- Child1: '0-0-1',
- Child2: '0-0-2',
- },
- Node2: '0-1',
- };
- const benchmarkSet = size => {
- console.time('benchmarkSet');
- var set = new Set();
- for (var i = 0; i < size; i++) set.add(i);
- for (var i = 0; i < size; i++) set.has(i);
- console.timeEnd('benchmarkSet');
- };
- const benchmarkArr = size => {
- console.time('benchmarkArr');
- var arr = [];
- for (var i = 0; i < size; i++) arr.push(i);
- for (var i = 0; i < size; i++) arr.indexOf(i);
- console.timeEnd('benchmarkArr');
- };
- export const BenchMark = () => {
- const size = 100000;
- benchmarkSet(size);
- benchmarkArr(size);
- return <div>check console please</div>;
- };
- BenchMark.story = {
- name: 'bench mark',
- };
- export const SimpleTree = () => (
- <Tree
- treeData={treeData1}
- // onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
- // onSelect={(e, bool, node) => console.log('select', e, bool, node)}
- // onChange={e => console.log('change', e)}
- onContextMenu={(e, node) => console.log(e.currentTarget, node)}
- onDoubleClick={(e, node) => console.log(e, node)}
- motion={true}
- />
- );
- SimpleTree.story = {
- name: 'simple tree',
- };
- export const MultipleTree = () => (
- <Tree
- treeData={treeData1}
- multiple
- labelInValue
- // onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
- // onSelect={(e, bool) => console.log('select', e, bool)}
- onContextMenu={(e, node) => console.log(e, node)}
- onDoubleClick={(e, node) => console.log(e, node)}
- onChange={e => console.log('change', e)}
- />
- );
- MultipleTree.story = {
- name: 'multiple tree',
- };
- export const SearchableTree = () => (
- <Tree
- treeData={treeData1}
- filterTreeNode
- treeNodeFilterProp="value"
- multiple
- searchStyle={{ width: '300px' }}
- />
- );
- SearchableTree.story = {
- name: 'searchable tree',
- };
- export const DisabledTree = () => (
- <Tree treeData={treeData1} defaultValue={['dongjing', 'daban']} multiple disabled />
- );
- DisabledTree.story = {
- name: 'disabled tree',
- };
- export const DefaultTree = () => (
- <div>
- <Tree treeData={treeData1} defaultValue={['zhongguo']} />
- <br />
- <Tree
- treeData={treeData1}
- multiple
- defaultValue={['shanghai', 'dongjing', 'beijing', 'daban']}
- onChange={e => console.log(e)}
- onSelect={e => console.log(e)}
- />
- </div>
- );
- DefaultTree.story = {
- name: 'default tree',
- };
- export const ExpandAction = () => (
- <div>
- <Tree treeData={treeData1} defaultValue={['zhongguo']} />
- <br />
- <Tree
- treeData={treeData1}
- //multiple
- defaultValue={['shanghai', 'dongjing', 'beijing', 'daban']}
- expandAction="click"
- onDoubleClick={e => console.log(e.detail)}
- />
- <br />
- <Tree
- treeData={treeData1}
- multiple
- defaultValue={['shanghai', 'dongjing', 'beijing', 'daban']}
- expandAction="doubleClick"
- />
- </div>
- );
- ExpandAction.story = {
- name: 'expandAction',
- };
- class Demo extends React.Component {
- constructor() {
- super();
- this.state = {
- value: 'shanghai',
- };
- }
- onChange(value) {
- this.setState({ value });
- }
- render() {
- return (
- <Tree
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData1}
- 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', 'beijing', 'zhongguo'],
- };
- }
- onChange(value) {
- console.log(value);
- this.setState({ value });
- }
- render() {
- console.log(this.state.value);
- return (
- <Tree
- style={{ width: 300 }}
- multiple
- dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
- treeData={treeData1}
- value={this.state.value}
- placeholder="Please select"
- onChange={e => this.onChange(e)}
- />
- );
- }
- }
- export const ControlledComponentMultiple = () => <Demo2 />;
- ControlledComponentMultiple.story = {
- name: 'controlled Component multiple',
- };
- export const JsonTree = () => (
- <div>
- <Tree
- treeDataSimpleJson={treeJson}
- onChange={e => console.log('change', e)}
- onSelect={e => console.log('select', e)}
- />
- <br />
- <Tree
- treeDataSimpleJson={treeJson}
- multiple
- onChange={e => console.log(e)}
- onSelect={e => console.log(e)}
- />
- </div>
- );
- JsonTree.story = {
- name: 'json tree',
- };
- export const IconTree = () => (
- <div>
- <Tree treeData={treeDataWithIcon} />
- <br />
- <Tree treeData={treeDataWithIcon} multiple blockNode />
- </div>
- );
- IconTree.story = {
- name: 'icon tree',
- };
- export const DirectoryTree = () => <Tree treeData={treeData1} directory multiple blockNode />;
- DirectoryTree.story = {
- name: 'directory tree',
- };
- const button = (
- <ButtonGroup size="small" theme="borderless">
- <Button
- onClick={e => {
- Toast.info(opts);
- e.stopPropagation();
- }}
- >
- 提示
- </Button>
- <Button>点击</Button>
- </ButtonGroup>
- );
- const style = {
- display: 'flex',
- justifyContent: 'space-between',
- alignItems: 'center',
- };
- const treeDataWithNode2 = [
- {
- label: (
- <div style={style}>
- <span>亚洲</span>
- <ButtonGroup size="small" theme="borderless">
- {button}
- </ButtonGroup>
- </div>
- ),
- value: 'yazhou',
- key: 'yazhou',
- children: [
- {
- label: (
- <div style={style}>
- <span>中国</span>
- {button}
- </div>
- ),
- value: 'zhongguo',
- key: 'zhongguo',
- children: [
- {
- label: (
- <div style={style}>
- <span>test</span>
- {button}
- </div>
- ),
- value: 'test',
- key: 'test',
- },
- ],
- },
- {
- label: (
- <div style={style}>
- <span>日本</span>
- {button}
- </div>
- ),
- value: 'riben',
- key: 'riben',
- },
- ],
- },
- ];
- export const TreeLabelUsingNode = () => (
- <div>
- <Tree treeData={treeDataWithNode2} blockNode defaultExpandAll />
- </div>
- );
- TreeLabelUsingNode.story = {
- name: 'tree label using node',
- };
- const treeDataTest = [
- {
- value: '一级标签1',
- label: '一级标签1',
- id: 1,
- key: '1',
- },
- ];
- class TagSideSheet2 extends React.Component {
- constructor() {
- super();
- this.state = {
- tagList: [],
- visibles: false,
- };
- this.onVisible = this.onVisible.bind(this);
- this.renderLabel = this.renderLabel.bind(this);
- this.transLabel = this.transLabel.bind(this);
- }
- componentDidMount() {
- let tagList = [...treeDataTest];
- this.setState({
- tagList,
- });
- }
- onVisible(visibles) {
- this.setState({
- visibles,
- });
- }
- renderLabel(item) {
- const { visibles } = this.state;
- console.log('rendering label', visibles);
- return (
- <Popover trigger="custom" position="bottomLeft" visible={visibles} content={'测试popover'}>
- <Button
- icon={<IconEdit />}
- onClick={e => {
- e.stopPropagation();
- this.onVisible(!visibles);
- }}
- >
- {item.label}
- </Button>
- </Popover>
- );
- }
- transLabel(list) {
- // list = cloneDeep(list);
- list.forEach(item => {
- item.label = this.renderLabel(item);
- // item.key += Math.random().toString().slice(0, 5);
- });
- return list;
- }
- render() {
- const { tagList = [] } = this.state;
- const transformedTags = this.transLabel(cloneDeep(tagList));
- console.log('transformedTags', transformedTags, treeDataTest);
- return <Tree treeData={transformedTags} />;
- }
- }
- export const TreeLabelUsingPopover = () => <TagSideSheet2 />;
- TreeLabelUsingPopover.story = {
- name: 'tree label using popover',
- };
- export const DefaultExpandKeysTree = () => (
- <>
- <Tree treeData={treeData1} defaultExpandedKeys={['zhongguo', 'beimeizhou']} blockNode />
- <Tree treeData={treeData1} defaultExpandAll blockNode />
- <Tree treeData={treeData1} defaultExpandAll multiple blockNode />
- </>
- );
- DefaultExpandKeysTree.story = {
- name: 'defaultExpandKeys tree',
- };
- export const LabelInValueTree = () => (
- <>
- <Tree treeData={treeData1} labelInValue onChange={e => console.log(e)} />
- <Tree treeData={treeData1} labelInValue onChange={e => console.log(e)} multiple />
- <Tree treeData={treeDataWithIcon} labelInValue onChange={e => console.log(e)} multiple />
- </>
- );
- LabelInValueTree.story = {
- name: 'labelInValue tree',
- };
- class Test extends React.Component {
- constructor() {
- super();
- this.state = {
- loading: false,
- };
- }
- componentDidMount() {
- setTimeout(() => {
- console.log('set loading');
- this.setState({ loading: true });
- }, 5000);
- }
- render() {
- const treeDataWithNode2 = [
- {
- label: (
- <div style={style}>
- <span>亚洲</span>
- <ButtonGroup size="small" theme="borderless">
- {button}
- </ButtonGroup>
- </div>
- ),
- value: 'yazhou',
- key: 'yazhou',
- children: [
- {
- label: (
- <div style={style}>
- <span>中国</span>
- {button}
- </div>
- ),
- value: 'zhongguo',
- key: 'zhongguo',
- children: [
- {
- label: (
- <div style={style}>
- <span>test</span>
- {button}
- </div>
- ),
- value: 'test',
- key: 'test',
- },
- ],
- },
- {
- label: (
- <div style={style}>
- <span>日本</span>
- {button}
- </div>
- ),
- value: 'riben',
- key: 'riben',
- },
- ],
- },
- ];
- return <Tree treeData={treeDataWithNode2} />;
- }
- }
- export const SetStateAfter5S = () => <Test />;
- SetStateAfter5S.story = {
- name: 'setState after 5s',
- };
- class DemoExpandedKeys extends React.Component {
- constructor() {
- super();
- this.state = {
- expand: ['zhongguo', 'beimeizhou'],
- };
- }
- onExpand(expand) {
- console.log(expand);
- this.setState({ expand });
- }
- render() {
- return (
- <Tree
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData1}
- expandedKeys={this.state.expand}
- placeholder="Please select"
- onExpand={(e, rest) => this.onExpand(e, rest)}
- />
- );
- }
- }
- class DemoExpandeKeysMulti extends React.Component {
- constructor() {
- super();
- this.state = {
- expand: ['zhongguo'],
- };
- }
- onExpand(expand) {
- console.log(expand);
- this.setState({ expand });
- }
- render() {
- return (
- <Tree
- style={{ width: 300 }}
- multiple
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData1}
- expandedKeys={this.state.expand}
- placeholder="Please select"
- onExpand={e => this.onExpand(e)}
- />
- );
- }
- }
- export const ExpandedKeys = () => (
- <>
- <DemoExpandedKeys />
- <br />
- <DemoExpandeKeysMulti />
- </>
- );
- ExpandedKeys.story = {
- name: 'expandedKeys',
- };
- class DmExpandedKeys extends React.Component {
- constructor() {
- super();
- this.state = {
- treeData: [
- {
- key: '0',
- label: 'item-0',
- value: '0',
- },
- ],
- };
- this.add = this.add.bind(this);
- }
- add() {
- let itemLength = Math.floor(Math.random() * 5) + 1;
- let treeData = new Array(itemLength).fill(0).map((v, i) => {
- let length = Math.floor(Math.random() * 3);
- let children = new Array(length).fill(0).map((cv, ci) => {
- let child = {
- key: `${i}-${ci}`,
- label: `Leaf-${i}-${ci}`,
- value: `${i}-${ci}`,
- };
- return child;
- });
- let item = {
- key: `${i}`,
- label: `Item-${i}`,
- value: `${i}`,
- children,
- };
- return item;
- });
- this.setState({ treeData });
- }
- render() {
- const { treeData } = this.state;
- return (
- <>
- <Tree treeData={this.state.treeData} expandedKeys={['0', '1']} />
- <br />
- <Button onClick={this.add}>动态改变数据</Button>
- </>
- );
- }
- }
- export const DynamicExpandKeys = () => (
- <>
- <DmExpandedKeys />
- </>
- );
- DynamicExpandKeys.story = {
- name: 'dynamic expandKeys',
- };
- class DmSelectedKeys extends React.Component {
- constructor() {
- super();
- this.state = {
- treeData: [
- {
- key: '0',
- label: 'item-0',
- value: '0',
- },
- ],
- };
- this.add = this.add.bind(this);
- }
- add() {
- let itemLength = Math.floor(Math.random() * 5) + 1;
- let treeData = new Array(itemLength).fill(0).map((v, i) => {
- let length = Math.floor(Math.random() * 3);
- let children = new Array(length).fill(0).map((cv, ci) => {
- let child = {
- key: `${i}-${ci}`,
- label: `Leaf-${i}-${ci}`,
- value: `${i}-${ci}`,
- };
- return child;
- });
- let item = {
- key: `${i}`,
- label: `Item-${i}`,
- value: `${i}`,
- children,
- };
- return item;
- });
- this.setState({ treeData });
- }
- render() {
- const { treeData } = this.state;
- return (
- <>
- <Tree treeData={treeData} value={['0-0']} onChange={e => console.log(e)} />
- <br />
- <Button onClick={this.add}>动态改变数据</Button>
- </>
- );
- }
- }
- export const DynamicSelectedKey = () => (
- <>
- <DmSelectedKeys />
- </>
- );
- DynamicSelectedKey.story = {
- name: 'dynamic selectedKey',
- };
- export const LargeAmountOfData = () => (
- <>
- <BigTree />
- </>
- );
- LargeAmountOfData.story = {
- name: 'large amount of data',
- };
- export const Autosizer = () => (
- <div
- style={{
- boxSizing: 'border-box',
- height: 400,
- width: 200,
- }}
- >
- <div
- style={{
- boxSizing: 'border-box',
- height: '50%',
- width: 200,
- }}
- >
- <AutoSizer
- // defaultHeight={defaultHeight}
- // defaultWidth={defaultWidth}
- >
- {({ height, width }) => (
- <div
- style={{
- width: width,
- height: height,
- }}
- >
- {`width:${width}, height:${height}`}
- </div>
- )}
- </AutoSizer>
- </div>
- </div>
- );
- Autosizer.story = {
- name: 'autosizer',
- };
- const MotionCustomLabelDemo = () => {
- 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',
- },
- ];
- const [hoverKey, setHoverKey] = useState();
- const cusLabel = (list = []) => {
- const recusive = (list = []) => {
- if (!list.length) {
- return;
- }
- list.forEach(item => {
- const { type, label, key } = item;
- item.label = (
- <div onMouseEnter={() => setHoverKey(key)}>
- {label}
- {hoverKey === key ? <IconMore /> : null}
- </div>
- );
- recusive(item.children);
- });
- };
- recusive(list);
- return list;
- };
- return <Tree treeData={cusLabel(treeData)} defaultExpandAll />;
- };
- export const MotionCustomLabel = () => <MotionCustomLabelDemo />;
- MotionCustomLabel.story = {
- name: 'motion custom label',
- };
- 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(['riben']);
- setExpandedKeys(['riben']);
- setAutoExpandParent(true);
- }}
- >
- Update
- </Button>
- <Tree
- onExpand={onExpand}
- expandedKeys={expandedKeys}
- autoExpandParent={autoExpandParent}
- onChange={onSelect}
- value={selectedKeys}
- treeData={treeData1}
- multiple
- />
- </div>
- );
- };
- export const AutoExpandParent = () => (
- <>
- <AutoParentDemo />
- </>
- );
- AutoExpandParent.story = {
- name: 'autoExpandParent',
- };
- const findDescendantKeys = node => {
- let res = [node.key];
- const findChild = item => {
- if (!item) return;
- const { children } = item;
- if (children && children.length) {
- children.forEach(child => {
- res.push(child.key);
- findChild(child);
- });
- }
- };
- findChild(node);
- return res;
- };
- class DyTreeWithExpandControlled extends React.Component {
- constructor() {
- super();
- this.state = {
- treeData: [
- {
- key: '0',
- label: 'item-0',
- value: '0',
- },
- ],
- expandedKeys: [],
- autoExpandParent: false,
- inputValue: '',
- collapsedKeys: new Set([]),
- };
- }
- componentDidUpdate(prevProps, prevState) {
- if (
- !isEqual(prevState.treeData, this.state.treeData) ||
- !isEqual(prevState.inputValue, this.state.inputValue)
- ) {
- const { treeData, inputValue, collapsedKeys } = this.state;
- let filteredKeys = [];
- const findFilteredKey = arr => {
- arr.forEach(item => {
- if (item.label.indexOf(inputValue) > -1) {
- filteredKeys.push(item.key);
- }
- if (item.children) {
- findFilteredKey(item.children);
- }
- });
- };
- findFilteredKey(treeData);
- const expanded = difference(filteredKeys, [...collapsedKeys]);
- this.setState({
- expandedKeys: expanded,
- autoExpandParent: true,
- });
- }
- }
- add = () => {
- let itemLength = Math.floor(Math.random() * 5) + 1;
- let treeData = new Array(itemLength).fill(0).map((v, i) => {
- let length = Math.floor(Math.random() * 3) + 1;
- let children = new Array(length).fill(0).map((cv, ci) => {
- let child = {
- key: `${i}-${ci}`,
- label: `Leaf-${i}-${ci}`,
- value: `${i}-${ci}`,
- };
- return child;
- });
- let item = {
- key: `${i}`,
- label: `Item-${i}`,
- value: `${i}`,
- children,
- };
- return item;
- });
- this.setState({ treeData });
- };
- search = val => {
- this.setState({ inputValue: val });
- };
- onExpand = (keys, { expanded, node }) => {
- let collapsed = this.state.collapsedKeys;
- let desKeys = findDescendantKeys(node);
- if (!expanded) {
- desKeys.forEach(key => collapsed.add(key));
- } else {
- desKeys.forEach(key => collapsed.has(key) && collapsed.delete(key));
- }
- this.setState({
- expandedKeys: keys,
- autoExpandParent: false,
- collapsedKeys: collapsed,
- });
- };
- render() {
- const { treeData, expandedKeys, autoExpandParent } = this.state;
- return (
- <div>
- <Tree
- treeData={treeData}
- filterTreeNode
- autoExpandParent={autoExpandParent}
- expandedKeys={expandedKeys}
- onSearch={this.search}
- onExpand={this.onExpand}
- />
- <br />
- <Button onClick={this.add} style={{ margin: 20 }}>
- 动态改变数据
- </Button>
- </div>
- );
- }
- }
- export const DynamicTreeDataWithSearchValueAndControlledExpand = () => (
- <DyTreeWithExpandControlled />
- );
- DynamicTreeDataWithSearchValueAndControlledExpand.story = {
- name: 'dynamic treeData with searchValue and controlled expand',
- };
- const CusSearchRender = () => {
- const [inputValue, setInputValue] = useState('');
- const ref = useRef();
- const setValue = value => {
- setInputValue(value);
- ref.current.search(value);
- };
- return (
- <Tree
- ref={ref}
- treeData={treeData1}
- filterTreeNode
- showFilteredOnly
- searchRender={({ prefix, placeholder }) => (
- <Input
- prefix={prefix}
- placeholder={placeholder}
- value={inputValue}
- onChange={value => setValue(value)}
- />
- )}
- />
- );
- };
- export const _CusSearchRender = () => (
- <>
- <CusSearchRender />
- </>
- );
- _CusSearchRender.story = {
- name: 'CusSearchRender',
- };
- const RefSearch = () => {
- const ref = useRef();
- const treeData = [
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- {
- label: 'North America',
- value: 'North America',
- key: '1',
- children: [
- {
- label: 'United States',
- value: 'United States',
- key: '1-0',
- },
- {
- label: 'Canada',
- value: 'Canada',
- key: '1-1',
- },
- ],
- },
- ];
- return (
- <div>
- <Input aria-label='filter tree' onChange={v => ref.current.search(v)} />
- <Tree
- treeData={treeData}
- defaultValue="Shanghai"
- blockNode={false}
- ref={ref}
- filterTreeNode
- searchRender={() => null}
- />
- </div>
- );
- };
- export const _RefSearch = () => (
- <>
- <RefSearch />
- </>
- );
- _RefSearch.story = {
- name: 'RefSearch',
- };
- const initTreeDate = [
- {
- label: 'Expand to load',
- key: '0',
- },
- {
- label: 'Expand to load',
- key: '1',
- },
- {
- label: 'Tree Node',
- key: '2',
- isLeaf: true,
- },
- ];
- function updateTreeData(list, key, children) {
- return list.map(node => {
- if (node.key === key) {
- return { ...node, children };
- }
- if (node.children) {
- return { ...node, children: updateTreeData(node.children, key, children) };
- }
- return node;
- });
- }
- const LoadingTreeDemo = () => {
- const [treeData, setTreeData] = useState(initTreeDate);
- function onLoadData({ key, children }) {
- return new Promise(resolve => {
- if (children) {
- resolve();
- return;
- }
- setTimeout(() => {
- setTreeData(origin =>
- updateTreeData(origin, key, [
- {
- label: 'Child Node',
- key: `${key}-0`,
- },
- {
- label: 'Child Node',
- key: `${key}-1`,
- },
- ])
- );
- resolve();
- }, 1000);
- });
- }
- return <Tree loadData={onLoadData} treeData={cloneDeep(treeData)} />;
- };
- export const Loading = () => (
- <>
- <LoadingTreeDemo />
- </>
- );
- Loading.story = {
- name: 'loading',
- };
- const LoadingWithSearch = () => {
- const [treeData, setTreeData] = useState(initTreeDate);
- function onLoadData({ key, children }) {
- return new Promise(resolve => {
- if (children) {
- resolve();
- return;
- }
- setTimeout(() => {
- setTreeData(origin =>
- updateTreeData(origin, key, [
- {
- label: 'Child Node',
- key: `${key}-0`,
- },
- {
- label: 'Child Node',
- key: `${key}-1`,
- },
- ])
- );
- resolve();
- }, 1000);
- });
- }
- return <Tree loadData={onLoadData} treeData={cloneDeep(treeData)} filterTreeNode />;
- };
- export const _LoadingWithSearch = () => (
- <>
- <LoadingWithSearch />
- </>
- );
- _LoadingWithSearch.story = {
- name: 'Loading with search',
- };
- const DisabledStrictly = () => {
- return (
- <>
- <span> disable shanghai(checked), China </span>
- <Tree
- treeData={[
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- disabled: true,
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- disabled: true,
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- ]}
- defaultValue="Shanghai"
- multiple
- defaultExpandAll
- disableStrictly
- />
- <br />
- <span> disable shanghai(checked), beijing(checked) </span>
- <Tree
- treeData={[
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- disabled: true,
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- disabled: true,
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- ]}
- defaultValue={['Shanghai', 'Beijing']}
- multiple
- defaultExpandAll
- disableStrictly
- />
- <span> disable shanghai(checked) </span>
- <Tree
- treeData={[
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- disabled: true,
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- ]}
- defaultValue={['Shanghai']}
- multiple
- defaultExpandAll
- disableStrictly
- />
- <br />
- <span> disable shanghai </span>
- <Tree
- treeData={[
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Chengdu',
- value: 'Chengdu',
- key: '0-0-2',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- disabled: true,
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- ]}
- multiple
- defaultExpandAll
- disableStrictly
- />
- <span> disable China(checked) - Shanghai </span>
- <Tree
- treeData={[
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- disabled: true,
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- disabled: true,
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- ]}
- defaultValue="China"
- multiple
- defaultExpandAll
- disableStrictly
- />
- <span> disable China </span>
- <Tree
- treeData={[
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- disabled: true,
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- ]}
- multiple
- defaultExpandAll
- disableStrictly
- />
- </>
- );
- };
- export const DisableStrictly = () => <DisabledStrictly />;
- DisableStrictly.story = {
- name: 'disableStrictly',
- };
- const ActionTree = () => {
- let initData = [
- {
- label: 'Asia',
- value: 'Asia',
- key: 'asia',
- children: [
- {
- label: 0,
- value: `${0}`,
- key: `${0}`,
- },
- {
- label: 1,
- value: `${1}`,
- key: `${1}`,
- },
- {
- label: 2,
- value: `${2}`,
- key: `${2}`,
- },
- {
- label: 3,
- value: `${3}`,
- key: `${3}`,
- },
- {
- label: 4,
- value: `${4}`,
- key: `${4}`,
- },
- ],
- },
- ];
- const [data, setData] = useState(initData);
- const remove = key => {
- let ind = data[0].children.findIndex(item => item.key === key);
- if (ind >= 0) {
- let items = cloneDeep(data);
- items[0].children.splice(ind, 1);
- setData(items);
- }
- };
- return (
- <Tree
- treeData={cloneDeep(data)}
- renderLabel={(label, data) => (
- <div>
- {label}
- <Button onClick={() => remove(data.key)}>remove</Button>
- </div>
- )}
- />
- );
- };
- export const DeleteOrAddChildNode = () => <ActionTree />;
- DeleteOrAddChildNode.story = {
- name: 'Delete or Add Child Node',
- };
- const MutipleHLTree = () => {
- const [selected, setSelected] = useState(new Set());
- const [selectedThroughParent, setSelectedThroughParent] = useState(new Set());
- const findDescendantKeys = node => {
- let res = [node.key];
- const findChild = item => {
- if (!item) return;
- const { children } = item;
- if (children && children.length) {
- children.forEach(child => {
- res.push(child.key);
- findChild(child);
- });
- }
- };
- findChild(node);
- return res;
- };
- const handleSelect = (key, bool, node) => {
- setSelected(new Set([key]));
- const descendantKeys = findDescendantKeys(node);
- setSelectedThroughParent(new Set(descendantKeys));
- };
- const renderLabel = ({ className, data, onClick, expandIcon }) => {
- const { label, icon, key } = data;
- const isLeaf = !(data.children && data.children.length);
- const style = {
- backgroundColor: selected.has(key)
- ? 'rgba(var(--semi-blue-0), 1)'
- : selectedThroughParent.has(key)
- ? 'rgba(var(--semi-blue-0), .5)'
- : 'transparent',
- };
- return (
- <li className={className} role="treeitem" onClick={onClick} style={style}>
- {isLeaf ? null : expandIcon}
- {icon ? icon : null}
- <span>{label}</span>
- </li>
- );
- };
- const treeStyle = {
- width: 260,
- height: 420,
- border: '1px solid var(--semi-color-border)',
- };
- return (
- <Tree
- treeData={treeData1}
- renderFullLabel={renderLabel}
- onSelect={handleSelect}
- style={treeStyle}
- />
- );
- };
- export const RenderOuterLable = () => <MutipleHLTree />;
- RenderOuterLable.story = {
- name: 'renderOuterLable',
- };
- export const TreeWithoutValueProps = () => (
- <Tree
- treeData={treeDataWithoutValue}
- value="meiguo"
- defaultExpandAll
- onChange={(...args) => console.log(args)}
- />
- );
- TreeWithoutValueProps.story = {
- name: 'tree without value props',
- };
- const DnDTree = () => {
- const initialData = [
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0',
- },
- ],
- },
- ],
- },
- {
- label: 'North America',
- value: 'North America',
- key: '1',
- children: [
- {
- label: 'United States',
- value: 'United States',
- key: '1-0',
- },
- {
- label: 'Canada',
- value: 'Canada',
- key: '1-1',
- },
- ],
- },
- {
- label: 'Europe',
- value: 'Europe',
- key: '2',
- },
- ];
- const [treeData, setTreeData] = useState(initialData);
- // const [expandedKeys, setExpandedKeys] = useState(['zhongguo']);
- function onDragEnter(info) {
- console.log(info);
- // if in controlled expandedKeys mode
- // setExpandedKeys(info.expandedKeys)
- }
- function onDrop(info) {
- const { dropToGap, node, dragNode } = info;
- const dropKey = node.key;
- const dragKey = dragNode.key;
- const dropPos = node.pos.split('-');
- const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
- const data = [...treeData];
- const loop = (data, key, callback) => {
- data.forEach((item, ind, arr) => {
- if (item.key === key) return callback(item, ind, arr);
- if (item.children) return loop(item.children, key, callback);
- });
- };
- let dragObj;
- loop(data, dragKey, (item, ind, arr) => {
- arr.splice(ind, 1);
- dragObj = item;
- });
- if (!dropToGap) {
- // inset into the dropPosition
- loop(data, dropKey, (item, ind, arr) => {
- item.children = item.children || [];
- item.children.push(dragObj);
- });
- } else if (dropPosition === 1 && node.children && node.expanded) {
- // has children && expanded and drop into the node bottom gap
- // insert to the top 这里我们添加在头部,可以是任意位置
- loop(data, dropKey, item => {
- item.children = item.children || [];
- item.children.unshift(dragObj);
- });
- } else {
- let dropNodeInd;
- let dropNodePosArr;
- loop(data, dropKey, (item, ind, arr) => {
- dropNodePosArr = arr;
- dropNodeInd = ind;
- });
- if (dropPosition === -1) {
- // insert to top
- dropNodePosArr.splice(dropNodeInd, 0, dragObj);
- } else {
- // insert to bottom
- dropNodePosArr.splice(dropNodeInd + 1, 0, dragObj);
- }
- }
- setTreeData(data);
- }
- return (
- <Tree
- treeData={treeData}
- draggable
- //expandedKeys={expandedKeys}
- onDragEnter={onDragEnter}
- onDrop={onDrop}
- />
- );
- };
- export const DraggableTree = () => <DnDTree />;
- DraggableTree.story = {
- name: 'draggable Tree',
- };
- const TestTree = () => {
- return (
- <Tree
- treeData={testData}
- // motion={false}
- style={{ height: '100%' }}
- filterTreeNode
- expandAction="click"
- showFilteredOnly
- />
- );
- };
- export const Draggable = () => <TestTree />;
- Draggable.story = {
- name: 'draggable',
- };
- export const RenderFullLabelWithDraggable = () => {
- const [selected, setSelected] = useState(new Set());
- const [selectedThroughParent, setSelectedThroughParent] = useState(new Set());
- const defaultTreeData = [
- {
- label: '黑色固定按钮',
- key: 'fix-btn-0',
- },
- {
- label: '模块',
- key: 'module-0',
- children: [
- {
- label: '可自由摆放的组件',
- key: 'free-compo-0',
- },
- {
- label: '分栏容器',
- key: 'split-col-0',
- children: [
- {
- label: '按钮组件',
- key: 'btn-0',
- },
- {
- label: '按钮组件',
- key: 'btn-1',
- },
- ],
- },
- ],
- },
- {
- label: '模块',
- key: 'module-1',
- children: [
- {
- label: '自定义组件',
- key: 'cus-0',
- },
- ],
- },
- ];
- const [treeData, setTreeData] = useState(defaultTreeData);
- const onDrop = info => {
- const { dropToGap, node, dragNode } = info;
- const dropKey = node.key;
- const dragKey = dragNode.key;
- const dropPos = node.pos.split('-');
- const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
- const data = [...treeData];
- const loop = (data, key, callback) => {
- data.forEach((item, ind, arr) => {
- if (item.key === key) return callback(item, ind, arr);
- if (item.children) return loop(item.children, key, callback);
- });
- };
- let dragObj;
- loop(data, dragKey, (item, ind, arr) => {
- arr.splice(ind, 1);
- dragObj = item;
- });
- if (!dropToGap) {
- loop(data, dropKey, (item, ind, arr) => {
- item.children = item.children || [];
- item.children.push(dragObj);
- });
- } else if (dropPosition === 1 && node.children && node.expanded) {
- loop(data, dropKey, item => {
- item.children = item.children || [];
- item.children.unshift(dragObj);
- });
- } else {
- let dropNodeInd;
- let dropNodePosArr;
- loop(data, dropKey, (item, ind, arr) => {
- dropNodePosArr = arr;
- dropNodeInd = ind;
- });
- if (dropPosition === -1) {
- dropNodePosArr.splice(dropNodeInd, 0, dragObj);
- } else {
- dropNodePosArr.splice(dropNodeInd + 1, 0, dragObj);
- }
- }
- setTreeData(data);
- };
- const findDescendantKeys = node => {
- let res = [node.key];
- const findChild = item => {
- if (!item) return;
- const { children } = item;
- if (children && children.length) {
- children.forEach(child => {
- res.push(child.key);
- findChild(child);
- });
- }
- };
- findChild(node);
- return res;
- };
- const handleSelect = (key, bool, node) => {
- setSelected(new Set([key]));
- const descendantKeys = findDescendantKeys(node);
- setSelectedThroughParent(new Set(descendantKeys));
- };
- const renderLabel = ({ className, data, onClick, expandIcon }) => {
- const { label, icon, key } = data;
- const isLeaf = !(data.children && data.children.length);
- const style = {
- backgroundColor: selected.has(key)
- ? 'rgba(var(--semi-blue-0), 1)'
- : selectedThroughParent.has(key)
- ? 'rgba(var(--semi-blue-0), .5)'
- : 'transparent',
- };
- return (
- <li className={className} role="treeitem" onClick={onClick} style={style}>
- {isLeaf ? <span style={{ width: 24 }}></span> : expandIcon}
- {icon ? icon : null}
- <span>{label}</span>
- </li>
- );
- };
- const treeStyle = {
- height: 420,
- border: '1px solid var(--semi-color-border)',
- };
- return (
- <Tree
- treeData={treeData}
- draggable
- onDrop={onDrop}
- renderFullLabel={renderLabel}
- onSelect={handleSelect}
- style={treeStyle}
- defaultExpandAll
- />
- );
- };
- RenderFullLabelWithDraggable.story = {
- name: 'renderFullLabel with draggable',
- };
- export const CheckRelationDemo = () => {
- const treeData = [
- {
- label: 'Asia',
- value: 'Asia',
- key: '0',
- children: [
- {
- label: 'China',
- value: 'China',
- key: '0-0',
- children: [
- {
- label: 'Beijing',
- value: 'Beijing',
- key: '0-0-0',
- },
- {
- label: 'Shanghai',
- value: 'Shanghai',
- key: '0-0-1',
- },
- {
- label: 'Chengdu',
- value: 'Chengdu',
- key: '0-0-2',
- },
- ],
- },
- {
- label: 'Japan',
- value: 'Japan',
- key: '0-1',
- children: [
- {
- label: 'Osaka',
- value: 'Osaka',
- key: '0-1-0'
- }
- ]
- },
- ],
- },
- {
- label: 'North America',
- value: 'North America',
- key: '1',
- children: [
- {
- label: 'United States',
- value: 'United States',
- key: '1-0'
- },
- {
- label: 'Canada',
- value: 'Canada',
- key: '1-1'
- }
- ]
- }
- ];
- const [value, setValue] = useState('China');
- const [value2, setValue2] = useState();
- const [value3, setValue3] = useState();
- const style = {
- width: 260,
- height: 420,
- border: '1px solid var(--semi-color-border)'
- };
- const handleChange = value => {
- console.log(value);
- setValue(value);
- };
- const handleChange2 = value => {
- console.log(value);
- setValue2(value);
- };
- const handleChange3 = value => {
- console.log(value);
- setValue3(value);
- };
- return (
- <>
- <div>checkRelation='unRelated'</div>
- <Tree
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 中国节点为 disabled</div>
- <Tree
- treeData={treeData1}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
- <Tree
- treeData={treeData1}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- disableStrictly
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + defaultValue 为 China</div>
- <Tree
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- defaultValue='China'
- />
- <br /><br />
- <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
- <Tree
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- value={value}
- onChange={handleChange}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
- <Tree
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- value={value2}
- onChangeWithObject
- onChange={handleChange2}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
- <Tree
- leafOnly
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- value={value3}
- onChange={handleChange3}
- />
- <br /><br />
- <div>checkRelation='unRelated' + onSelect </div>
- <Tree
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- onSelect={(value,status,node)=>console.log('select', value, status, node)}
- />
- </>
- );
- };
|