| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687 |
- import React, { useState, useMemo, useRef, useCallback, useEffect } from 'react';
- import { Icon, Input, Button, Form, Popover, Tag, Typography, CheckboxGroup, TagInput, Switch, Tree } from '../../index';
- import TreeSelect from '../index';
- import { flattenDeep } from 'lodash';
- import CustomTrigger from './CustomTrigger';
- import { IconCreditCard, IconChevronDown, IconClose } from '@douyinfe/semi-icons';
- import copy from 'fast-copy';
- 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',
- },
- ],
- },
- ];
- const specialTreeData = [
- {
- label1: '亚洲',
- // value1: 'Yazhou',
- key1: 'yazhou',
- children1: [
- {
- label1: '中国',
- // value1: 'Zhongguo',
- key1: 'zhongguo',
- disabled1: true,
- children1: [
- {
- label1: '北京',
- // value1: 'Beijing',
- key1: 'beijing',
- },
- {
- label1: '上海',
- // value1: 'Shanghai',
- key1: 'shanghai',
- },
- ],
- },
- {
- label1: '日本',
- // value1: 'Riben',
- key1: 'riben',
- children1: [
- {
- label1: '东京',
- // value1: 'Dongjing',
- key1: 'dongjing',
- },
- {
- label1: '大阪',
- // value1: 'Daban',
- key1: 'daban',
- },
- ],
- },
- ],
- },
- {
- label1: '北美洲',
- // value1: 'Beimeizhou',
- key1: 'beimeizhou',
- children1: [
- {
- label1: '美国',
- // value1: 'Meiguo',
- key1: 'meiguo',
- },
- {
- label1: '加拿大',
- // value1: 'Jianada',
- key1: 'jianada',
- },
- ],
- },
- ];
- const treeDataEn = [
- {
- 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'
- }
- ]
- }
- ];
- 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"
- />
- </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 = treeDataEn;
- 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>
- );
- };
- export const LongLabel = () => {
- const treeData = [
- {
- label: '这是一个超长的中文测试用标题这是一个超长的中文测试用标题这是一个超长的中文测试用标题这是一个超长的中文测试用标题',
- value: 'v1',
- key: '0',
- },
- {
- label: 'ThisISAVeryLongTestSentenceThisISAVeryLongTestSentenceThisISAVeryLongTestSentence',
- value: 'v2',
- key: '1',
- }
- ];
- return (
- <>
- <p>单选</p>
- <TreeSelect
- defaultValue='v1'
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- />
- <p>单选,可搜索, searchPosition='trigger'</p>
- <TreeSelect
- filterTreeNode
- searchPosition='trigger'
- defaultValue='v1'
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- />
- <p>单选,可搜索, searchPosition='dropDown'</p>
- <TreeSelect
- filterTreeNode
- defaultValue='v1'
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- />
- <p>单选</p>
- <TreeSelect
- defaultValue='v2'
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- />
- <p>单选,可搜索, searchPosition='trigger'</p>
- <TreeSelect
- filterTreeNode
- searchPosition='trigger'
- defaultValue='v2'
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- />
- <p>单选,可搜索, searchPosition='dropDown'</p>
- <TreeSelect
- filterTreeNode
- defaultValue='v2'
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- placeholder="请选择"
- />
- </>
- );
- }
- export const UnRelatedAndAsyncLoad = () => {
- const initialData = [
- {
- label: 'Expand to load0',
- value: '0',
- key: '0',
- },
- {
- label: 'Expand to load1',
- value: '1',
- key: '1',
- },
- {
- label: 'Leaf Node',
- value: '2',
- key: '2',
- isLeaf: true,
- },
- ];
- const [treeData, setTreeData] = useState(initialData);
- 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}-0`,
- key: `${key}-0`,
- },
- {
- label: `Child Node${key}-1`,
- key: `${key}-1`,
- },
- ]),
- );
- resolve();
- }, 1000);
- });
- }
- return (
- <>
- <span>issue 1852: checkRelation='unRelated', 异步加载数据</span>
- <TreeSelect
- checkRelation='unRelated'
- defaultValue={['0']}
- multiple
- defaultOpen
- loadData={onLoadData}
- treeData={[...treeData]}
- />
- </>
- );
- };
- const constructLargeData = () => {
- const newArray = (new Array(10)).fill(0).map((item, m) => {
- const parent = {
- key: `key-${m}`,
- label: `node-${m}`,
- children: []
- }
- new Array(100).fill(0).map((item, n) => {
- const children = {
- key: `key-${m}-${n}`,
- label: `value-${m}-${n}`,
- children: []
- }
- new Array(10).fill(0).map((item, o) => {
- const grandChildren = {
- key: `key-${m}-${n}-${o}`,
- label: `value-${m}-${n}-${o}`,
- }
- children.children.push(grandChildren);
- });
- parent.children.push(children);
- });
- return parent;
- });
- return newArray;
- }
- export const ChangeTreeData = () => {
- const [sign, setSign] = useState(true);
- const treeData1 = useMemo(() => {
- return constructLargeData();
- }, []);
- const treeData2 = useMemo(() => {
- return constructLargeData();
- }, []);
- const onButtonClick = useCallback(() => {
- setSign((sign) => {
- return !sign;
- })
- }, []);
- return <>
- <Button onClick={onButtonClick}>点击修改TreeData</Button>
- <br/><br/>
- <TreeSelect
- treeData={sign ? treeData1 : treeData2}
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- placeholder="请选择"
- />
- </>
- }
- export const KeyMaps = () => {
- const [withObject, setWithObject] = useState(false);
- const [value1, setValue1] = useState(undefined);
- const [value2, setValue2] = useState(undefined);
- const [expandKeys, setExpandedKeys] = useState(["yazhou", 'zhongguo']);
-
- const switchChange = useCallback((checked) => {
- setWithObject(checked);
- setValue1(undefined);
- setValue2(undefined);
- }, []);
- const onSingleChange = useCallback((value) => {
- console.log('onSingleChange', value);
- setValue1(value);
- }, []);
- const onMultipleChange = useCallback((value) => {
- console.log('onMultipleChange', value);
- setValue2(value);
- }, []);
- const normalChange = useCallback((value) => {
- console.log('onChange', value);
- }, []);
- const normalExpand = useCallback((expandedKeys, {expanded, node}) => {
- console.log('onExpanded', expandedKeys, expanded, copy(node));
- }, []);
- const keyMaps = useMemo(() => {
- return {
- // value: 'value1',
- key: 'key1',
- label: 'label1',
- children: 'children1',
- disabled: 'disabled1'
- };
- }, []);
- const regularTreeProps = useMemo(() => ({
- keyMaps: keyMaps,
- treeData: specialTreeData,
- style: { width: 300 },
- dropdownStyle: { maxHeight: 400, overflow: 'auto' },
- onChange: normalChange,
- onExpand: normalExpand,
- onChangeWithObject: withObject,
- }), [withObject]);
- const defaultSelectedObj = {
- label1: '北京',
- // value1: 'Beijing',
- key1: 'beijing',
- };
- return (
- <>
- <span>onChangeWithObject</span><Switch checked={withObject} onChange={switchChange} />
- <div key={String(withObject)} style={{ marginTop: 10 }}>
- <div> Single select</div>
- <TreeSelect
- {...regularTreeProps}
- defaultValue={withObject ? defaultSelectedObj : 'beijing'}
- />
- <div> Single select, onSearch, filterTreeNode, treeNodeFilterProp</div>
- <TreeSelect
- {...regularTreeProps}
- filterTreeNode={(inputValue, treeNodeString, data)=> {
- console.log("filterTreeNode", inputValue, treeNodeString, data);
- return treeNodeString.includes(inputValue);
- }}
- treeNodeFilterProp={"key1"}
- onSearch={(input, filteredExpandedKeys) => {
- console.log('onSearch', input, filteredExpandedKeys);
- }}
- />
- <div>Single select, controlled</div>
- <TreeSelect
- {...regularTreeProps}
- value={value1}
- onChange={onSingleChange}
- />
- <div> Multiple select</div>
- <TreeSelect
- {...regularTreeProps}
- multiple
- defaultValue={withObject ? [defaultSelectedObj] : ['beijing']}
- />
- <div> Multiple select, controlled</div>
- <TreeSelect
- {...regularTreeProps}
- value={value2}
- multiple
- onChange={onMultipleChange}
- />
- <div> Multiple select, disableStrictly</div>
- <TreeSelect
- {...regularTreeProps}
- multiple
- disableStrictly
- />
- <div> Multiple, 展开受控</div>
- <TreeSelect
- {...regularTreeProps}
- multiple
- expandedKeys={expandKeys}
- onExpand={(expandedKeys, {expanded, node}) => {
- console.log('onExpanded', expandedKeys, expanded, copy(node));
- setExpandedKeys(expandedKeys);
- }}
- />
- </div>
- </>
- );
- }
- export const Issue1542 = () => {
- const [expandedKeys, setExpandedKeys] = useState([]);
- const treeData = treeDataEn;
- const onExpand = useCallback((expandedKeys) => {
- setExpandedKeys(expandedKeys);
- }, [expandedKeys]);
- const onSearch = useCallback((inputValue, filteredExpandedKeys) => {
- const set = new Set([...filteredExpandedKeys, ...expandedKeys]);
- setExpandedKeys(Array.from(set));
- }, [setExpandedKeys]);
- return (
- <>
- <TreeSelect
- // multiple
- style={{ width: 300 }}
- dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
- treeData={treeData}
- filterTreeNode
- searchPosition='trigger'
- showFilteredOnly
- expandedKeys={expandedKeys}
- onExpand={onExpand}
- onSearch={onSearch}
- />
- </>
- );
- };
|