1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275 |
- import React, { useState, useMemo, useRef, useCallback } from 'react';
- import { Icon, Input, Button, Form, Popover, Tag, Typography, CheckboxGroup, TagInput } from '../../index';
- import TreeSelect from '../index';
- import { flattenDeep } from 'lodash';
- import CustomTrigger from './CustomTrigger';
- import { IconCreditCard, IconChevronDown, IconClose } from '@douyinfe/semi-icons';
- import { setFocusToPreviousMenuItem } from '@douyinfe/semi-foundation/utils/a11y';
- const TreeNode = TreeSelect.TreeNode;
- const { Title } = Typography;
- 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 treeData3 = [
- {
- 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: 'Child Node3',
- value: '0-0-3',
- key: '0-0-3',
- },
- {
- label: 'Child Node4',
- value: '0-0-4',
- key: '0-0-4',
- },
- {
- label: 'Child Node5',
- value: '0-0-5',
- key: '0-0-5',
- },
- {
- label: 'Child Node6',
- value: '0-0-6',
- key: '0-0-6',
- },
- {
- label: 'Child Node7',
- value: '0-0-7',
- key: '0-0-7',
- },
- {
- label: 'Child Node8',
- value: '0-0-8',
- key: '0-0-8',
- },
- {
- label: 'Child Node9',
- value: '0-0-9',
- key: '0-0-9',
- },
- {
- label: 'Child Node10',
- value: '0-0-10',
- key: '0-0-10',
- },
- ],
- },
- {
- label: 'Node2',
- value: '0-1',
- key: '0-1',
- },
- ];
- 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',
- },
- ],
- },
- ];
- export const TreeSelectWrapper = () => (
- <div>
- <div>github issue 750 修改测试用例</div>
- <CheckboxGroup>
- <TreeSelect
- showClear={true}
- expandAll
- style={{width: 400}}
- treeData={[
- {
- key: '1',
- label: '所有节点',
- value: '1',
- children: [
- { key: '20006251', label: 'Semi', value: '[email protected]' },
- { key: '20006248', label: 'Design', value: '[email protected]' },
- {
- key: '20006205',
- label: 'React',
- value: '[email protected]',
- },
- ],
- },
- ]}
- multiple
- filterTreeNode
- showFilteredOnly={true}
- leafOnly
- />
- </CheckboxGroup>
- </div>
- );
- TreeSelectWrapper.story = {
- name: 'treeSelect wrapper',
- };
- 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}
- defaultOpen
- defaultExpandAll
- motion={false}
- multiple
- placeholder="Please select"
- />
- <span id={'invisible-span'} style={{ width: 10, height: 10, position: 'fixed', top: 0, right: 0 }} />
- </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"
- />
- <div>single, filterTreeNode, searchPosition=dropdown</div>
- <TreeSelect
- filterTreeNode
- 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, filterTreeNode, searchPosition=dropdown</div>
- <TreeSelect
- filterTreeNode
- 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"
- />
- <div>single, filterTreeNode, searchPosition=trigger</div>
- <TreeSelect
- searchPosition="trigger"
- filterTreeNode
- 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, filterTreeNode, searchPosition=trigger</div>
- <TreeSelect
- searchPosition="trigger"
- filterTreeNode
- 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',
- };
- 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: 300,
- };
- const dropdownStyle = {
- maxHeight: 400,
- overflow: 'auto'
- };
- 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>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + maxTagCount=2</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- maxTagCount={2}
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- maxTagCount={2}
- filterTreeNode
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索 + searchBox in trigger</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- maxTagCount={2}
- filterTreeNode
- checkRelation='unRelated'
- searchPosition='trigger'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 中国节点为 disabled</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeDataWithoutValue}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeDataWithoutValue}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- disableStrictly
- style={style}
- />
- <br /><br />
- <div>checkRelation='unRelated' + defaultValue 为 China</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- defaultValue='China'
- />
- <br /><br />
- <div>checkRelation='unRelated' + defaultValue 为 China + 开启搜索</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- filterTreeNode
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- defaultValue='China'
- />
- <br /><br />
- <div>多选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- filterTreeNode
- showClear
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- searchPosition='trigger'
- defaultValue={['China', 'Japan']}
- />
- <br /><br />
- <div>单选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- filterTreeNode
- showClear
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- searchPosition='trigger'
- defaultValue='China'
- />
- <br /><br />
- <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- value={value}
- onChange={handleChange}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- value={value2}
- onChangeWithObject
- onChange={handleChange2}
- />
- <br /><br />
- <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- leafOnly
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- value={value3}
- onChange={handleChange3}
- />
- <br /><br />
- <div>checkRelation='unRelated' + onSelect </div>
- <TreeSelect
- dropdownStyle={dropdownStyle}
- treeData={treeData}
- multiple
- checkRelation='unRelated'
- defaultExpandAll
- style={style}
- onSelect={(value,status,node)=>console.log('select', value, status, node)}
- />
- </>
- );
- };
- export const SearchableAndExpandedKeys = () => {
- const [expandedKeys1, setExpandedKeys1] = useState([]);
- const [expandedKeys2, setExpandedKeys2] = useState([]);
- const [expandedKeys3, setExpandedKeys3] = useState([]);
- return (
- <>
- <Title heading={6}>expandedKeys 受控</Title>
- <TreeSelect
- style={{ width: 300, marginBottom: 30 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- expandedKeys={expandedKeys1}
- defaultValue='beijing'
- onExpand={v => {
- console.log('onExpand value: ', v);
- setExpandedKeys1(v);
- }}
- />
- <Title heading={6}>expandedKeys 受控 + 开启搜索</Title>
- <TreeSelect
- style={{ width: 300, marginBottom: 30 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- defaultValue='beijing'
- expandedKeys={expandedKeys2}
- onExpand={v => {
- console.log('onExpand value: ', v);
- setExpandedKeys2(v);
- }}
- />
- <Title heading={6}>expandedKeys 受控 + 开启搜索 + 搜索时更新 expandedKeys</Title>
- <TreeSelect
- style={{ width: 300, marginBottom: 30 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- expandedKeys={expandedKeys3}
- defaultValue='beijing'
- onExpand={v => {
- console.log('onExpand value: ', v);
- setExpandedKeys3(v)
- }}
- onSearch={(input, filterExpandedKeys) => {
- console.log('onExpand filterExpandedKeys: ', filterExpandedKeys);
- setExpandedKeys3(filterExpandedKeys);
- }}
- />
- </>
- )
- }
- export const loadData = () => {
- const initialData = [
- {
- label: 'Expand to load',
- value: '0',
- key: '0',
- },
- {
- label: 'Expand to load',
- value: '1',
- key: '1',
- },
- {
- label: 'Leaf Node',
- value: '2',
- key: '2',
- isLeaf: true,
- },
- ];
- const [treeData, setTreeData] = useState(initialData);
- const [loadedKeys, setLoadedKeys] = useState(['2']);
- 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;
- });
- }
- 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 (
- <TreeSelect
- loadData={onLoadData}
- filterTreeNode
- treeData={treeData}
- style={{ width: 300 }}
- placeholder="请选择"
- />
- );
- }
- export const loadDataAndLoadedkeys = () => {
- const initialData = [
- {
- label: 'Expand to load',
- value: '0',
- key: '0',
- },
- {
- label: 'Expand to load',
- value: '1',
- key: '1',
- },
- {
- label: 'Leaf Node',
- value: '2',
- key: '2',
- isLeaf: true,
- },
- ];
- const [treeData, setTreeData] = useState(initialData);
- const [loadedKeys, setLoadedKeys] = useState(['2']);
- 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;
- });
- }
- function updateLoadedKeys(key) {
- if(!loadedKeys.includes(key)){
- setLoadedKeys([...loadedKeys, key]);
- console.log('[...loadedKeys, key]', [...loadedKeys, key]);
- }
- }
- 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`,
- },
- ]),
- );
- // updateLoadedKeys(key);
- resolve();
- }, 1000);
- });
- }
- return (
- <TreeSelect
- loadData={onLoadData}
- filterTreeNode
- // loadedKeys={loadedKeys}
- treeData={treeData}
- style={{ width: 300 }}
- placeholder="请选择"
- />
- );
- }
- export const size = () => {
- const props = {
- style: { width: 300 },
- dropdownStyle: { maxHeight: 400, overflow: 'auto' },
- defaultValue: ['0-0-1', '0-0-2', '0-0-3', '0-0-4', '0-0-5', '0-0-6', '0-0-7'],
- treeData: treeData3,
- multiple: true,
- filterTreeNode: true,
- searchPosition: "trigger"
- };
- return (<>
- <TreeSelect {...props} size={'small'} placeholder={'small'} />
- <br/><br/>
- <TreeSelect {...props} size={'default'} placeholder={'default'} />
- <br/><br/>
- <TreeSelect {...props} size={'large'} placeholder={'large'} />
- </>);
- }
- export const valueNotInTreeDataIssue = () => {
- const treeData = [
- {
- key: "test",
- label: "测试标签",
- children: [
- {
- key: "test_2",
- label: "测试二级标签"
- },
- {
- key: "jzr_test",
- label: "之睿测试"
- }
- ]
- },
-
- {
- key: "create",
- label: "创作构思",
- children: [
- {
- key: "material",
- label: "素材积累"
- },
- {
- key: "lens_script",
- label: "分镜脚本"
- }
- ]
- }
- ];
- const treeDataWithValue = [
- {
- value: "test",
- key: "0",
- label: "测试标签",
- children: [
- {
- value: "test_2",
- key: "0-1",
- label: "测试二级标签"
- },
- {
- value: "jzr_test",
- key: "0-2",
- label: "之睿测试"
- }
- ]
- },
- {
- value: "create",
- key: "1",
- label: "创作构思",
- children: [
- {
- value: "material",
- key: "1-1",
- label: "素材积累"
- },
- {
- value: "lens_script",
- key: "1-2",
- label: "分镜脚本"
- }
- ]
- }
- ];
- const commonProps = useMemo(() => {
- return {
- multiple: true,
- style: { width: 300 },
- dropdownStyle: { maxHeight: 400, overflow: 'auto' },
- onChange: (value) => {
- console.log('onChange', value);
- },
- onSelect: (value) => {
- console.log('onSelect', value);
- },
- };
- }, []);
-
- return (
- <>
- <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,无 value</p>
- <p>checkRelation='related'</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={["test_2", 0]}
- treeData={treeData}
- {...commonProps}
- />
- <p>checkRelation='unRelated'</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={["test_2", "fish"]}
- checkRelation='unRelated'
- treeData={treeData}
- {...commonProps}
- />
- <p>onChangeWithObject, checkRelation='related'</p>
- <TreeSelect
- defaultExpandAll
- onChangeWithObject
- defaultValue={[
- {
- key: "test_2",
- label: "测试二级标签"
- },
- {
- key: "fish",
- label: "鱼"
- }
- ]}
- treeData={treeData}
- {...commonProps}
- />
- <p>onChangeWithObject, checkRelation='unRelated'</p>
- <TreeSelect
- defaultExpandAll
- onChangeWithObject
- defaultValue={[
- {
- key: "test_2",
- label: "测试二级标签"
- },
- {
- key: "fish",
- label: "鱼"
- }
- ]}
- treeData={treeData}
- {...commonProps}
- />
- <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,有 value</p>
- <p>checkRelation='related'</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={["test", "fish"]}
- treeData={treeDataWithValue}
- {...commonProps}
- />
- <p>checkRelation='unRelated'</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={["test", "fish"]}
- checkRelation='unRelated'
- treeData={treeDataWithValue}
- {...commonProps}
- />
- <p>onChangeWithObject, checkRelation='unRelated'</p>
- <TreeSelect
- defaultExpandAll
- onChangeWithObject
- defaultValue={[
- {
- value: "test_2",
- key: "0-1",
- label: "测试二级标签"
- },
- {
- key: "fish",
- value: "Fish",
- label: "鱼"
- }
- ]}
- treeData={treeDataWithValue}
- {...commonProps}
- />
- <p>onChangeWithObject, checkRelation='unRelated'</p>
- <TreeSelect
- defaultExpandAll
- onChangeWithObject
- defaultValue={[
- {
- value: "test_2",
- key: "0-1",
- label: "测试二级标签"
- },
- {
- key: "fish",
- value: "Fish",
- label: "鱼"
- }
- ]}
- treeData={treeDataWithValue}
- {...commonProps}
- />
- <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,无 value</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={"fish"}
- treeData={treeData}
- {...commonProps}
- multiple={false}
- />
- <p>onChangeWithObject</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={{
- key: "fish",
- value: "Fish",
- label: "鱼"
- }}
- treeData={treeData}
- {...commonProps}
- multiple={false}
- onChangeWithObject
- />
- <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,有 value</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={"fish"}
- treeData={treeDataWithValue}
- {...commonProps}
- multiple={false}
- />
- <p>onChangeWithObject</p>
- <TreeSelect
- defaultExpandAll
- defaultValue={{
- key: "fish",
- value: "Fish",
- label: "鱼"
- }}
- treeData={treeDataWithValue}
- {...commonProps}
- multiple={false}
- onChangeWithObject
- />
- </>
- );
- };
- class ValueTypeIsNumber extends React.Component {
- constructor() {
- super();
- this.state = {
- value: 1
- };
- }
- onChange(value) {
- console.log('onChange', value);
- this.setState({ value });
- }
- render() {
- const treeData = [
- {
- label: '北美洲',
- value: 'North America',
- key: '1',
- },
- {
- 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',
- },
- ],
- },
- ],
- },
-
- ];
- return (
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- value={this.state.value}
- placeholder="请选择"
- multiple
- onChange={e => this.onChange(e)}
- />
- );
- }
- }
- export const valueIsNumber = () => <ValueTypeIsNumber />
- export const searchPositionInTriggerAndVirtualize = () => {
- return (
- <>
- <TreeSelect
- searchPosition="trigger"
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- placeholder="单选"
- virtualize={{
- itemSize: 28,
- // dropDown height 300 minus search box height minus padding 8 * 2
- // or if you set dropdown height, it will fill 100% of rest space
- height: 236
- }}
- />
- </>
- );
- };
- export const clickTriggerToHide = () => (
- <>
- <p>clickTriggerToHide 未设置,默认为 true</p>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="单选"
- />
- <p>clickTriggerToHide 设置为 false</p>
- <TreeSelect
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- placeholder="单选"
- clickTriggerToHide={false}
- />
- </>
- );
- export const triggerRenderAddMethod = () => {
- const treeData = useMemo(() => [
- {
- label: '亚洲',
- value: '亚洲',
- key: '0',
- children: [
- {
- label: '中国',
- value: '中国',
- key: '0-0',
- children: [
- {
- label: '北京',
- value: '北京',
- key: '0-0-0',
- },
- {
- label: '上海',
- value: '上海',
- key: '0-0-1',
- },
- ],
- },
- ],
- },
- {
- label: '北美洲',
- value: '北美洲',
- key: '1',
- }
- ], []);
- const onValueChange = useCallback((value) => {
- console.log('onChange', value);
- });
- const closeIcon = useCallback((value, onClear) => {
- return value && value.length ? <IconClose onClick={onClear} /> : <IconChevronDown />;
- }, []);
- const renderTagItem = useCallback((item, onRemove) => (
- <Tag closable key={item.key} onClose={() => { onRemove(item.key); }}>{item.label}</Tag>
- ), []);
- const renderTrigger1 = useCallback((props) => {
- const { value, placeholder, onClear } = props;
- return (
- <Button theme={'light'} icon={closeIcon(value, onClear)} iconPosition={'right'}>
- {value && value.length ? value.map(item => item.label).join(',') : placeholder}
- </Button>
- );
- }, []);
- const renderTrigger2 = useCallback((props) => {
- const { value, onSearch, onRemove, onClear } = props;
- return (
- <div style={{ border: '1px solid grey', width: 'fit-content', padding: 5, borderRadius: 5 }}>
- {value && value.length > 0 &&
- <div style={{ width: 'fit-content', minWidth: 10, padding: 5 }}>
- {value.map(item => renderTagItem(item, onRemove))}
- </div>
- }
- <Input style={{ width: 200 }} onChange={onSearch} />
- {closeIcon(value, onClear)}
- </div>
- );
- }, []);
- const renderTrigger3 = useCallback((props) => {
- const { value, onSearch, onRemove, inputValue } = props;
- const tagInputValue = value.map(item => item.key);
- const renderTagInMultiple = (key) => {
- const label = value.find(item => item.key === key).label;
- const onCloseTag = (value, e, tagKey) => {
- onRemove(tagKey);
- }
- return <Tag style={{ marginLeft: 2 }} tagKey={key} key={key} onClose={onCloseTag} closable>{label}</Tag>
- }
- return (
- <TagInput
- inputValue={inputValue}
- value={tagInputValue}
- onInputChange={onSearch}
- renderTagItem={renderTagInMultiple}
- />
- )
- }, []);
- return (
- <>
- <TreeSelect
- triggerRender={renderTrigger1}
- multiple
- treeData={treeData}
- placeholder='Custom Trigger'
- onChange={onValueChange}
- style={{ width: 300 }}
- />
- <br />
- <TreeSelect
- triggerRender={renderTrigger2}
- filterTreeNode
- searchPosition="trigger"
- multiple
- treeData={treeData}
- placeholder='Custom Trigger'
- onChange={onValueChange}
- style={{ width: 300 }}
- />
- <br />
- <TreeSelect
- triggerRender={renderTrigger3}
- filterTreeNode
- searchPosition="trigger"
- multiple
- treeData={treeData}
- placeholder='Custom Trigger'
- onChange={onValueChange}
- style={{ width: 300 }}
- />
- </>
- );
- }
- export const AutoSearchFocusPlusPreventScroll = () => {
- return (
- <div>
- <div style={{height: '100vh' }}>我是一个高度和视窗高度一致的div。
- <br />由于 TreeSelect 设置了 searchAutoFocus 以及 preventScroll,
- <br /> 符合预期的情况是在没有滚动屏幕情况下,你不会看到 TreeSelect 的 trigger
- </div>
- <TreeSelect
- searchAutoFocus
- searchPosition="trigger"
- preventScroll={true}
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData2}
- filterTreeNode
- placeholder="单选"
- />
- </div>
- );
- };
|