treeSelect.stories.jsx 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  1. import React, { useState, useMemo, useRef, useCallback, useEffect } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Icon, Input, Button, Form, Popover, Tag, Typography, CheckboxGroup, TagInput, Switch } from '../../index';
  4. import TreeSelect from '../index';
  5. import { flattenDeep, without } from 'lodash';
  6. import CustomTrigger from './CustomTrigger';
  7. import { IconCreditCard, IconChevronDown, IconClose, IconPlus, IconMinus } from '@douyinfe/semi-icons';
  8. import copy from 'fast-copy';
  9. const TreeNode = TreeSelect.TreeNode;
  10. const { Title } = Typography;
  11. export default {
  12. title: 'TreeSelect',
  13. parameters: {
  14. chromatic: { disableSnapshot: true },
  15. },
  16. }
  17. const treeData1 = [
  18. {
  19. label: 'Node1',
  20. value: '0-0',
  21. key: '0-0',
  22. children: [
  23. {
  24. label: 'Child Node1',
  25. value: '0-0-1',
  26. key: '0-0-1',
  27. },
  28. {
  29. label: 'Child Node2',
  30. value: '0-0-2',
  31. key: '0-0-2',
  32. },
  33. ],
  34. },
  35. {
  36. label: 'Node2',
  37. value: '0-1',
  38. key: '0-1',
  39. },
  40. ];
  41. const treeData2 = [
  42. {
  43. label: '亚洲',
  44. value: 'yazhou',
  45. key: 'yazhou',
  46. children: [
  47. {
  48. label: '中国',
  49. value: 'zhongguo',
  50. key: 'zhongguo',
  51. children: [
  52. {
  53. label: '北京',
  54. value: 'beijing',
  55. key: 'beijing',
  56. },
  57. {
  58. label: '上海',
  59. value: 'shanghai',
  60. key: 'shanghai',
  61. },
  62. ],
  63. },
  64. // {
  65. // label: '日本',
  66. // value: 'riben',
  67. // key: 'riben',
  68. // children: [
  69. // {
  70. // label: '东京',
  71. // value: 'dongjing',
  72. // key: 'dongjing'
  73. // },
  74. // {
  75. // label: '大阪',
  76. // value: 'daban',
  77. // key: 'daban'
  78. // }
  79. // ]
  80. // },
  81. ],
  82. },
  83. {
  84. label: '北美洲',
  85. value: 'beimeizhou',
  86. key: 'beimeizhou',
  87. children: [
  88. {
  89. label: '美国',
  90. value: 'meiguo',
  91. key: 'meiguo',
  92. },
  93. {
  94. label: '加拿大',
  95. value: 'jianada',
  96. key: 'jianada',
  97. },
  98. ],
  99. },
  100. ];
  101. const treeData3 = [
  102. {
  103. label: 'Node1',
  104. value: '0-0',
  105. key: '0-0',
  106. children: [
  107. {
  108. label: 'Child Node1',
  109. value: '0-0-1',
  110. key: '0-0-1',
  111. },
  112. {
  113. label: 'Child Node2',
  114. value: '0-0-2',
  115. key: '0-0-2',
  116. },
  117. {
  118. label: 'Child Node3',
  119. value: '0-0-3',
  120. key: '0-0-3',
  121. },
  122. {
  123. label: 'Child Node4',
  124. value: '0-0-4',
  125. key: '0-0-4',
  126. },
  127. {
  128. label: 'Child Node5',
  129. value: '0-0-5',
  130. key: '0-0-5',
  131. },
  132. {
  133. label: 'Child Node6',
  134. value: '0-0-6',
  135. key: '0-0-6',
  136. },
  137. {
  138. label: 'Child Node7',
  139. value: '0-0-7',
  140. key: '0-0-7',
  141. },
  142. {
  143. label: 'Child Node8',
  144. value: '0-0-8',
  145. key: '0-0-8',
  146. },
  147. {
  148. label: 'Child Node9',
  149. value: '0-0-9',
  150. key: '0-0-9',
  151. },
  152. {
  153. label: 'Child Node10',
  154. value: '0-0-10',
  155. key: '0-0-10',
  156. },
  157. ],
  158. },
  159. {
  160. label: 'Node2',
  161. value: '0-1',
  162. key: '0-1',
  163. },
  164. ];
  165. const treeDataWithoutValue = [
  166. {
  167. label: '亚洲',
  168. key: 'yazhou',
  169. children: [
  170. {
  171. label: '中国',
  172. key: 'zhongguo',
  173. disabled: true,
  174. children: [
  175. {
  176. label: '北京',
  177. key: 'beijing',
  178. },
  179. {
  180. label: '上海',
  181. key: 'shanghai',
  182. },
  183. ],
  184. },
  185. {
  186. label: '日本',
  187. key: 'riben',
  188. children: [
  189. {
  190. label: '东京',
  191. key: 'dongjing',
  192. },
  193. {
  194. label: '大阪',
  195. key: 'daban',
  196. },
  197. ],
  198. },
  199. ],
  200. },
  201. {
  202. label: '北美洲',
  203. key: 'beimeizhou',
  204. children: [
  205. {
  206. label: '美国',
  207. key: 'meiguo',
  208. },
  209. {
  210. label: '加拿大',
  211. key: 'jianada',
  212. },
  213. ],
  214. },
  215. ];
  216. const specialTreeData = [
  217. {
  218. label1: '亚洲',
  219. // value1: 'Yazhou',
  220. key1: 'yazhou',
  221. children1: [
  222. {
  223. label1: '中国',
  224. // value1: 'Zhongguo',
  225. key1: 'zhongguo',
  226. disabled1: true,
  227. children1: [
  228. {
  229. label1: '北京',
  230. // value1: 'Beijing',
  231. key1: 'beijing',
  232. },
  233. {
  234. label1: '上海',
  235. // value1: 'Shanghai',
  236. key1: 'shanghai',
  237. },
  238. ],
  239. },
  240. {
  241. label1: '日本',
  242. // value1: 'Riben',
  243. key1: 'riben',
  244. children1: [
  245. {
  246. label1: '东京',
  247. // value1: 'Dongjing',
  248. key1: 'dongjing',
  249. },
  250. {
  251. label1: '大阪',
  252. // value1: 'Daban',
  253. key1: 'daban',
  254. },
  255. ],
  256. },
  257. ],
  258. },
  259. {
  260. label1: '北美洲',
  261. // value1: 'Beimeizhou',
  262. key1: 'beimeizhou',
  263. children1: [
  264. {
  265. label1: '美国',
  266. // value1: 'Meiguo',
  267. key1: 'meiguo',
  268. },
  269. {
  270. label1: '加拿大',
  271. // value1: 'Jianada',
  272. key1: 'jianada',
  273. },
  274. ],
  275. },
  276. ];
  277. const treeDataEn = [
  278. {
  279. label: 'Asia',
  280. value: 'Asia',
  281. key: '0',
  282. children: [
  283. {
  284. label: 'China',
  285. value: 'China',
  286. key: '0-0',
  287. children: [
  288. {
  289. label: 'Beijing',
  290. value: 'Beijing',
  291. key: '0-0-0',
  292. },
  293. {
  294. label: 'Shanghai',
  295. value: 'Shanghai',
  296. key: '0-0-1',
  297. },
  298. {
  299. label: 'Chengdu',
  300. value: 'Chengdu',
  301. key: '0-0-2',
  302. },
  303. ],
  304. },
  305. {
  306. label: 'Japan',
  307. value: 'Japan',
  308. key: '0-1',
  309. children: [
  310. {
  311. label: 'Osaka',
  312. value: 'Osaka',
  313. key: '0-1-0'
  314. }
  315. ]
  316. },
  317. ],
  318. },
  319. {
  320. label: 'North America',
  321. value: 'North America',
  322. key: '1',
  323. children: [
  324. {
  325. label: 'United States',
  326. value: 'United States',
  327. key: '1-0'
  328. },
  329. {
  330. label: 'Canada',
  331. value: 'Canada',
  332. key: '1-1'
  333. }
  334. ]
  335. }
  336. ];
  337. export const TreeSelectWrapper = () => (
  338. <div>
  339. <div>github issue 750 修改测试用例</div>
  340. <CheckboxGroup>
  341. <TreeSelect
  342. showClear={true}
  343. expandAll
  344. style={{width: 400}}
  345. treeData={[
  346. {
  347. key: '1',
  348. label: '所有节点',
  349. value: '1',
  350. children: [
  351. { key: '20006251', label: 'Semi', value: '[email protected]' },
  352. { key: '20006248', label: 'Design', value: '[email protected]' },
  353. {
  354. key: '20006205',
  355. label: 'React',
  356. value: '[email protected]',
  357. },
  358. ],
  359. },
  360. ]}
  361. multiple
  362. filterTreeNode
  363. showFilteredOnly={true}
  364. leafOnly
  365. />
  366. </CheckboxGroup>
  367. </div>
  368. );
  369. TreeSelectWrapper.story = {
  370. name: 'treeSelect wrapper',
  371. };
  372. class SimpleTree extends React.Component {
  373. render() {
  374. return (
  375. <div>
  376. <TreeSelect
  377. style={{ width: 300 }}
  378. // value={this.state.value}
  379. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  380. treeData={treeData1}
  381. placeholder="Please select"
  382. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  383. onSelect={(e, bool) => console.log('select', e, bool)}
  384. onChange={(e, node) => console.log('change', e, node)}
  385. />
  386. <br />
  387. <br />
  388. <TreeSelect
  389. style={{ width: 300 }}
  390. // value={this.state.value}
  391. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  392. treeData={treeData2}
  393. placeholder="Please select"
  394. />
  395. </div>
  396. );
  397. }
  398. }
  399. export const _TreeSelect = () => {
  400. const treeData = [
  401. {
  402. label: '亚洲',
  403. value: 'Asia',
  404. key: '0',
  405. children: [
  406. {
  407. label: '中国',
  408. value: 'China',
  409. key: '0-0',
  410. children: [
  411. {
  412. label: '北京',
  413. value: 'Beijing',
  414. key: '0-0-0',
  415. },
  416. {
  417. label: '上海',
  418. value: 'Shanghai',
  419. key: '0-0-1',
  420. },
  421. ],
  422. },
  423. ],
  424. },
  425. {
  426. label: '北美洲',
  427. value: 'North America',
  428. key: '1',
  429. },
  430. ];
  431. return (
  432. <TreeSelect
  433. style={{ width: 300 }}
  434. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  435. treeData={treeData2}
  436. placeholder="请选择"
  437. />
  438. );
  439. };
  440. _TreeSelect.story = {
  441. name: 'tree select',
  442. };
  443. export const Searchable = () => (
  444. <div>
  445. <TreeSelect
  446. style={{ width: 300 }}
  447. // value={this.state.value}
  448. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  449. treeData={treeData2}
  450. filterTreeNode
  451. treeNodeFilterProp="value"
  452. placeholder="Please select"
  453. />
  454. <br />
  455. <br />
  456. <TreeSelect
  457. style={{ width: 300 }}
  458. // value={this.state.value}
  459. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  460. treeData={treeData2}
  461. filterTreeNode
  462. placeholder="Please select"
  463. />
  464. <br />
  465. <br />
  466. <TreeSelect
  467. style={{ width: 300 }}
  468. // value={this.state.value}
  469. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  470. treeData={treeData2}
  471. filterTreeNode
  472. searchAutoFocus
  473. placeholder="searchAutoFocus"
  474. />
  475. </div>
  476. );
  477. Searchable.story = {
  478. name: 'searchable',
  479. };
  480. export const SearchPosition = () => (
  481. <div style={{ background: 'var(--semi-color-bg-0)'}}>
  482. <TreeSelect
  483. searchPosition="trigger"
  484. style={{ width: 300 }}
  485. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  486. treeData={treeData2}
  487. filterTreeNode
  488. placeholder="单选"
  489. />
  490. <br />
  491. <br />
  492. <TreeSelect
  493. searchPosition="trigger"
  494. style={{ width: 300 }}
  495. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  496. treeData={treeData2}
  497. multiple
  498. filterTreeNode
  499. maxTagCount={2}
  500. placeholder="多选"
  501. />
  502. <br />
  503. <br />
  504. <TreeSelect
  505. searchPosition="trigger"
  506. style={{ width: 300 }}
  507. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  508. treeData={treeData2}
  509. multiple
  510. filterTreeNode
  511. maxTagCount={2}
  512. placeholder="searchAutoFocus"
  513. searchAutoFocus
  514. />
  515. <br />
  516. <br />
  517. <TreeSelect
  518. searchPosition="trigger"
  519. style={{ width: 300 }}
  520. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  521. treeData={treeData2}
  522. multiple
  523. filterTreeNode
  524. maxTagCount={1}
  525. placeholder="maxTagCount=1"
  526. />
  527. </div>
  528. );
  529. SearchPosition.story = {
  530. name: 'searchPosition',
  531. };
  532. export const PrefixSuffixInsetLabel = () => (
  533. <div>
  534. <TreeSelect
  535. style={{ width: 300 }}
  536. // value={this.state.value}
  537. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  538. treeData={treeData2}
  539. filterTreeNode
  540. prefix={<IconCreditCard />}
  541. treeNodeFilterProp="value"
  542. placeholder="Please select"
  543. />
  544. <br />
  545. <br />
  546. <TreeSelect
  547. style={{ width: 300 }}
  548. // value={this.state.value}
  549. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  550. treeData={treeData2}
  551. filterTreeNode
  552. prefix={<span>1234</span>}
  553. treeNodeFilterProp="value"
  554. placeholder="Please select"
  555. />
  556. <br />
  557. <br />
  558. <TreeSelect
  559. style={{ width: 300 }}
  560. suffix="RMB"
  561. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  562. treeData={treeData2}
  563. placeholder="Please select"
  564. />
  565. <br />
  566. <br />
  567. <TreeSelect
  568. style={{ width: 300 }}
  569. insetLabel="blablabla"
  570. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  571. treeData={treeData2}
  572. placeholder="Please select"
  573. />
  574. <br />
  575. <br />
  576. <TreeSelect
  577. style={{ width: 300 }}
  578. insetLabel={<span>1234</span>}
  579. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  580. treeData={treeData2}
  581. placeholder="Please select"
  582. />
  583. </div>
  584. );
  585. PrefixSuffixInsetLabel.story = {
  586. name: 'prefix suffix insetLabel',
  587. };
  588. PrefixSuffixInsetLabel.parameters = {
  589. chromatic: { disableSnapshot: false },
  590. }
  591. export const ValidateStatus = () => (
  592. <div>
  593. <TreeSelect
  594. style={{ width: 300 }}
  595. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  596. treeData={treeData2}
  597. multiple
  598. validateStatus="warning"
  599. placeholder="Please select"
  600. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  601. onSelect={(e, bool) => console.log('select', e, bool)}
  602. onChange={e => console.log('change', e)}
  603. />
  604. <br />
  605. <br />
  606. <TreeSelect
  607. style={{ width: 300 }}
  608. validateStatus="error"
  609. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  610. treeData={treeData2}
  611. // multiple
  612. placeholder="Please select"
  613. />
  614. </div>
  615. );
  616. ValidateStatus.story = {
  617. name: 'validate status',
  618. };
  619. ValidateStatus.parameters = {
  620. chromatic: { disableSnapshot: false },
  621. }
  622. export const Multiple = () => (
  623. <div>
  624. <TreeSelect
  625. style={{ width: 300 }}
  626. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  627. treeData={treeData2}
  628. multiple
  629. placeholder="Please select"
  630. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  631. onSelect={(e, bool) => console.log('select', e, bool)}
  632. onChange={e => console.log('change', e)}
  633. />
  634. <br />
  635. <br />
  636. <TreeSelect
  637. style={{ width: 300 }}
  638. // value={this.state.value}
  639. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  640. treeData={treeData2}
  641. defaultOpen
  642. defaultExpandAll
  643. motion={false}
  644. multiple
  645. placeholder="Please select"
  646. />
  647. <span id={'invisible-span'} style={{ width: 10, height: 10, position: 'fixed', top: 0, right: 0 }} />
  648. </div>
  649. );
  650. Multiple.story = {
  651. name: 'multiple',
  652. };
  653. export const MaxTagCount = () => (
  654. <div>
  655. <TreeSelect
  656. style={{ width: 300 }}
  657. // value={this.state.value}
  658. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  659. treeData={treeData2}
  660. multiple
  661. maxTagCount={2}
  662. placeholder="Please select"
  663. />
  664. {/* <br />
  665. <br />
  666. <TreeSelect
  667. style={{ width: 300 }}
  668. // value={this.state.value}
  669. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  670. treeData={treeData2}
  671. multiple
  672. placeholder="Please select"
  673. /> */}
  674. </div>
  675. );
  676. MaxTagCount.story = {
  677. name: 'maxTagCount',
  678. };
  679. export const MultipleSearchable = () => (
  680. <div>
  681. <TreeSelect
  682. style={{ width: 300 }}
  683. // value={this.state.value}
  684. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  685. treeData={treeData2}
  686. multiple
  687. filterTreeNode
  688. treeNodeFilterProp="value"
  689. placeholder="Please select"
  690. />
  691. </div>
  692. );
  693. MultipleSearchable.story = {
  694. name: 'multiple searchable',
  695. };
  696. export const DefaultValues = () => (
  697. <div>
  698. <TreeSelect
  699. style={{ width: 300 }}
  700. defaultValue={'shanghai'}
  701. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  702. treeData={treeData2}
  703. // multiple
  704. filterTreeNode
  705. treeNodeFilterProp="value"
  706. />
  707. <br />
  708. <br />
  709. <TreeSelect
  710. style={{ width: 300 }}
  711. defaultValue={'shanghai'}
  712. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  713. treeData={treeData2}
  714. filterTreeNode
  715. multiple
  716. treeNodeFilterProp="value"
  717. />
  718. <br />
  719. <br />
  720. <TreeSelect
  721. style={{ width: 300 }}
  722. defaultValue={['shanghai', 'daban', 'dongjing']}
  723. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  724. treeData={treeData2}
  725. filterTreeNode
  726. multiple
  727. treeNodeFilterProp="value"
  728. />
  729. <br />
  730. <br />
  731. <TreeSelect
  732. style={{ width: 300 }}
  733. defaultValue={['meiguo', 'dongjing']}
  734. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  735. treeData={treeData2}
  736. filterTreeNode
  737. multiple
  738. treeNodeFilterProp="value"
  739. />
  740. </div>
  741. );
  742. DefaultValues.story = {
  743. name: 'default values',
  744. };
  745. export const Disabled = () => (
  746. <div>
  747. <TreeSelect
  748. style={{ width: 300 }}
  749. defaultValue={'shanghai'}
  750. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  751. treeData={treeData2}
  752. multiple
  753. filterTreeNode
  754. disabled
  755. treeNodeFilterProp="value"
  756. placeholder="Please select"
  757. />
  758. <br />
  759. <br />
  760. <TreeSelect
  761. style={{ width: 300 }}
  762. defaultValue={'shanghai'}
  763. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  764. treeData={treeData2}
  765. filterTreeNode
  766. disabled
  767. treeNodeFilterProp="value"
  768. placeholder="Please select"
  769. />
  770. <br />
  771. <br />
  772. <TreeSelect
  773. style={{ width: 300 }}
  774. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  775. treeData={treeData2}
  776. multiple
  777. filterTreeNode
  778. disabled
  779. treeNodeFilterProp="value"
  780. placeholder="Please select"
  781. />
  782. </div>
  783. );
  784. Disabled.story = {
  785. name: 'disabled',
  786. };
  787. Disabled.parameters = {
  788. chromatic: { disableSnapshot: false },
  789. }
  790. export const OptionLabelProp = () => (
  791. <>
  792. <TreeSelect
  793. style={{ width: 300 }}
  794. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  795. treeData={treeData2}
  796. multiple
  797. filterTreeNode
  798. treeNodeFilterProp="value"
  799. treeNodeLabelProp="value"
  800. placeholder="Please select"
  801. />
  802. </>
  803. );
  804. OptionLabelProp.story = {
  805. name: 'optionLabelProp',
  806. };
  807. export const ValueInArray = () => (
  808. <>
  809. <TreeSelect
  810. style={{ width: 300 }}
  811. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  812. treeData={treeData2}
  813. multiple
  814. filterTreeNode
  815. valueInArray
  816. onChange={(...args) => console.log(args)}
  817. treeNodeFilterProp="value"
  818. treeNodeLabelProp="value"
  819. placeholder="Please select"
  820. />
  821. </>
  822. );
  823. ValueInArray.story = {
  824. name: 'valueInArray',
  825. };
  826. export const OnBlurOnFocus = () => (
  827. <>
  828. <div>single</div>
  829. <TreeSelect
  830. style={{ width: 300 }}
  831. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  832. treeData={treeData2}
  833. onBlur={(...args) => console.log('blur', args)}
  834. onFocus={(...args) => console.log('focus', args)}
  835. placeholder="Please select"
  836. />
  837. <div>multiple</div>
  838. <TreeSelect
  839. style={{ width: 300 }}
  840. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  841. treeData={treeData2}
  842. multiple
  843. onBlur={(...args) => console.log('blur', args)}
  844. onFocus={(...args) => console.log('focus', args)}
  845. placeholder="Please select"
  846. />
  847. <div>single, filterTreeNode, searchPosition=dropdown</div>
  848. <TreeSelect
  849. filterTreeNode
  850. style={{ width: 300 }}
  851. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  852. treeData={treeData2}
  853. onBlur={(...args) => console.log('blur', args)}
  854. onFocus={(...args) => console.log('focus', args)}
  855. placeholder="Please select"
  856. />
  857. <div>multiple, filterTreeNode, searchPosition=dropdown</div>
  858. <TreeSelect
  859. filterTreeNode
  860. style={{ width: 300 }}
  861. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  862. treeData={treeData2}
  863. multiple
  864. onBlur={(...args) => console.log('blur', args)}
  865. onFocus={(...args) => console.log('focus', args)}
  866. placeholder="Please select"
  867. />
  868. <div>single, filterTreeNode, searchPosition=trigger</div>
  869. <TreeSelect
  870. searchPosition="trigger"
  871. filterTreeNode
  872. style={{ width: 300 }}
  873. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  874. treeData={treeData2}
  875. onBlur={(...args) => console.log('blur', args)}
  876. onFocus={(...args) => console.log('focus', args)}
  877. placeholder="Please select"
  878. />
  879. <div>multiple, filterTreeNode, searchPosition=trigger</div>
  880. <TreeSelect
  881. searchPosition="trigger"
  882. filterTreeNode
  883. style={{ width: 300 }}
  884. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  885. treeData={treeData2}
  886. multiple
  887. onBlur={(...args) => console.log('blur', args)}
  888. onFocus={(...args) => console.log('focus', args)}
  889. placeholder="Please select"
  890. />
  891. </>
  892. );
  893. OnBlurOnFocus.story = {
  894. name: 'onBlur/onFocus',
  895. };
  896. export const LeafOnly = () => (
  897. <div>
  898. <TreeSelect
  899. style={{ width: 300 }}
  900. // value={this.state.value}
  901. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  902. treeData={treeData2}
  903. multiple
  904. leafOnly
  905. placeholder="Please select"
  906. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  907. onSelect={(e, bool) => console.log('select', e, bool)}
  908. onChange={e => console.log('change', e)}
  909. />
  910. </div>
  911. );
  912. LeafOnly.story = {
  913. name: 'leafOnly',
  914. };
  915. class Demo extends React.Component {
  916. constructor() {
  917. super();
  918. this.state = {
  919. value: 'shanghai',
  920. };
  921. }
  922. onChange(value) {
  923. this.setState({ value });
  924. }
  925. render() {
  926. return (
  927. <TreeSelect
  928. style={{ width: 300 }}
  929. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  930. treeData={treeData2}
  931. value={this.state.value}
  932. placeholder="Please select"
  933. onChange={e => this.onChange(e)}
  934. />
  935. );
  936. }
  937. }
  938. export const ControlledComponentSingle = () => <Demo />;
  939. ControlledComponentSingle.story = {
  940. name: 'controlled Component single',
  941. };
  942. class Demo2 extends React.Component {
  943. constructor() {
  944. super();
  945. this.state = {
  946. value: ['Shanghai'],
  947. };
  948. }
  949. onChange(value) {
  950. this.setState({ value });
  951. }
  952. render() {
  953. console.log(this.state.value);
  954. const treeData = [
  955. {
  956. label: '亚洲',
  957. value: 'Asia',
  958. key: '0',
  959. children: [
  960. {
  961. label: '中国',
  962. value: 'China',
  963. key: '0-0',
  964. children: [
  965. {
  966. label: '北京',
  967. value: 'Beijing',
  968. key: '0-0-0',
  969. },
  970. {
  971. label: '上海',
  972. value: 'Shanghai',
  973. key: '0-0-1',
  974. },
  975. ],
  976. },
  977. ],
  978. },
  979. {
  980. label: '北美洲',
  981. value: 'North America',
  982. key: '1',
  983. },
  984. ];
  985. return (
  986. <TreeSelect
  987. style={{ width: 300 }}
  988. multiple
  989. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  990. treeData={treeData}
  991. value={this.state.value}
  992. placeholder="Please select"
  993. onChange={e => this.onChange(e)}
  994. />
  995. );
  996. }
  997. }
  998. export const ControlledComponentMultiple = () => <Demo2 />;
  999. ControlledComponentMultiple.story = {
  1000. name: 'controlled Component multiple',
  1001. };
  1002. class Demo3 extends React.Component {
  1003. constructor() {
  1004. super();
  1005. this.state = {
  1006. value: ['Shanghai'],
  1007. };
  1008. }
  1009. // 获取最底层值
  1010. getDeepChildrensByNode = node => {
  1011. return flattenDeep(
  1012. node.map(item => {
  1013. if (item.children) {
  1014. return this.getDeepChildrensByNode(item.children);
  1015. }
  1016. return item.value;
  1017. })
  1018. );
  1019. };
  1020. onChange(value, node) {
  1021. console.log('onchange', value);
  1022. value = this.getDeepChildrensByNode(node);
  1023. console.log('modifiled', value);
  1024. this.setState({ value });
  1025. }
  1026. render() {
  1027. const treeData = [
  1028. {
  1029. label: '亚洲',
  1030. value: 'Asia',
  1031. key: '0',
  1032. children: [
  1033. {
  1034. label: '中国',
  1035. value: 'China',
  1036. key: '0-0',
  1037. children: [
  1038. {
  1039. label: '北京',
  1040. value: 'Beijing',
  1041. key: '0-0-0',
  1042. },
  1043. {
  1044. label: '上海',
  1045. value: 'Shanghai',
  1046. key: '0-0-1',
  1047. },
  1048. ],
  1049. },
  1050. ],
  1051. },
  1052. {
  1053. label: '北美洲',
  1054. value: 'North America',
  1055. key: '1',
  1056. },
  1057. ];
  1058. return (
  1059. <TreeSelect
  1060. style={{ width: 300 }}
  1061. multiple
  1062. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  1063. treeData={treeData}
  1064. value={this.state.value}
  1065. placeholder="Please select"
  1066. onChange={(e, node) => this.onChange(e, node)}
  1067. />
  1068. );
  1069. }
  1070. }
  1071. export const ControlledComponentMultipleValueModified = () => <Demo3 />;
  1072. ControlledComponentMultipleValueModified.story = {
  1073. name: 'controlled Component multiple value modified',
  1074. };
  1075. class ConvertDemo extends React.Component {
  1076. constructor(props) {
  1077. super(props);
  1078. this.formApi = null;
  1079. }
  1080. handleChange = val => {
  1081. let finalVal = val;
  1082. let firstClassOption = ['Asia', 'North America'];
  1083. // 在这里去做你的value替换逻辑
  1084. console.log('originVal:' + val);
  1085. if (val.length === 1) {
  1086. // do nothing
  1087. } else {
  1088. if (val.every(item => firstClassOption.includes(item))) {
  1089. finalVal = val[val.length - 1];
  1090. }
  1091. }
  1092. console.log('finalVal:' + finalVal);
  1093. return finalVal;
  1094. };
  1095. render() {
  1096. const treeData = [
  1097. {
  1098. label: '亚洲',
  1099. value: 'Asia',
  1100. key: '0',
  1101. children: [
  1102. {
  1103. label: '中国',
  1104. value: 'China',
  1105. key: '0-0',
  1106. children: [
  1107. {
  1108. label: '北京',
  1109. value: 'Beijing',
  1110. key: '0-0-0',
  1111. },
  1112. {
  1113. label: '上海',
  1114. value: 'Shanghai',
  1115. key: '0-0-1',
  1116. },
  1117. ],
  1118. },
  1119. ],
  1120. },
  1121. {
  1122. label: '北美洲',
  1123. value: 'North America',
  1124. key: '1',
  1125. },
  1126. ];
  1127. return (
  1128. <Form getFormApi={this.getFormApi}>
  1129. <Form.TreeSelect
  1130. field="tree"
  1131. label="节点(TreeSelect)"
  1132. placeholder="请选择服务节点"
  1133. treeData={treeData}
  1134. convert={this.handleChange}
  1135. filterTreeNode
  1136. multiple
  1137. ></Form.TreeSelect>
  1138. </Form>
  1139. );
  1140. }
  1141. }
  1142. export const _ConvertDemo = () => <ConvertDemo />;
  1143. _ConvertDemo.story = {
  1144. name: 'convert demo',
  1145. };
  1146. export const TreeselectDefaultOpenInPopover = () => (
  1147. <Popover
  1148. content={
  1149. <TreeSelect
  1150. style={{ width: 200 }}
  1151. dropdownStyle={{ width: 200 }}
  1152. treeData={treeData1}
  1153. defaultOpen
  1154. // value={this.state.value}
  1155. // placeholder="Please select"
  1156. // onChange={(e, node) => this.onChange(e, node)}
  1157. />
  1158. }
  1159. >
  1160. <Button style={{ marginLeft: 150 }}>悬停此处</Button>
  1161. </Popover>
  1162. );
  1163. TreeselectDefaultOpenInPopover.story = {
  1164. name: 'treeselect defaultOpen in popover',
  1165. };
  1166. export const CustomTriggerDemo = () => <CustomTrigger />;
  1167. CustomTriggerDemo.story = { name: 'custom trigger' };
  1168. const AutoParentDemo = () => {
  1169. const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
  1170. const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
  1171. const [autoExpandParent, setAutoExpandParent] = useState(true);
  1172. const onExpand = expandedKeys => {
  1173. console.log('onExpand', expandedKeys);
  1174. // if not set autoExpandParent to false, if children expanded, parent can not collapse.
  1175. // or, you can remove all expanded children keys.
  1176. setExpandedKeys(expandedKeys);
  1177. setAutoExpandParent(false);
  1178. };
  1179. const onSelect = (selectedKeys, info) => {
  1180. console.log('onSelect:', info);
  1181. setSelectedKeys(selectedKeys);
  1182. };
  1183. return (
  1184. <div>
  1185. <Button
  1186. onClick={() => {
  1187. setSelectedKeys(['beijing']);
  1188. setExpandedKeys(['beijing']);
  1189. setAutoExpandParent(true);
  1190. }}
  1191. >
  1192. Update
  1193. </Button>
  1194. <TreeSelect
  1195. onExpand={onExpand}
  1196. expandedKeys={expandedKeys}
  1197. autoExpandParent={autoExpandParent}
  1198. onChange={onSelect}
  1199. value={selectedKeys}
  1200. treeData={treeData2}
  1201. // multiple
  1202. />
  1203. </div>
  1204. );
  1205. };
  1206. export const AutoExpandParent = () => (
  1207. <>
  1208. <AutoParentDemo />
  1209. </>
  1210. );
  1211. AutoExpandParent.story = {
  1212. name: 'autoExpandParent',
  1213. };
  1214. export const TreeWithoutValueProps = () => (
  1215. <TreeSelect
  1216. treeData={treeDataWithoutValue}
  1217. value="beijing"
  1218. defaultExpandAll
  1219. onChange={(...args) => console.log(args)}
  1220. />
  1221. );
  1222. TreeWithoutValueProps.story = {
  1223. name: 'tree without value props',
  1224. };
  1225. export const TreeSelectRenderSelectedItem = () => {
  1226. const treeData = [
  1227. {
  1228. label: '亚洲',
  1229. value: 'Asia',
  1230. key: '0',
  1231. children: [
  1232. {
  1233. label: '中国',
  1234. value: 'China',
  1235. key: '0-0',
  1236. children: [
  1237. {
  1238. label: '北京',
  1239. value: 'Beijing',
  1240. key: '0-0-0',
  1241. },
  1242. {
  1243. label: '上海',
  1244. value: 'Shanghai',
  1245. key: '0-0-1',
  1246. },
  1247. ],
  1248. },
  1249. ],
  1250. },
  1251. {
  1252. label: '北美洲',
  1253. value: 'North America',
  1254. key: '1',
  1255. },
  1256. {
  1257. label: '南美洲',
  1258. value: 'South America',
  1259. key: '2',
  1260. },
  1261. {
  1262. label: '南极洲',
  1263. value: 'Antarctica',
  1264. key: '3',
  1265. },
  1266. ];
  1267. return (
  1268. <>
  1269. <h4>单选</h4>
  1270. <TreeSelect
  1271. style={{ width: 300 }}
  1272. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1273. treeData={treeData}
  1274. placeholder="请选择"
  1275. renderSelectedItem={item => `${item.value}-${item.label}`}
  1276. />
  1277. <h4>多选</h4>
  1278. <TreeSelect
  1279. style={{ width: 300 }}
  1280. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1281. treeData={treeData}
  1282. multiple
  1283. placeholder="请选择"
  1284. renderSelectedItem={(item, { index, onClose }) => ({
  1285. content: `${item.value}-${item.label}`,
  1286. isRenderInTag: true,
  1287. })}
  1288. />
  1289. <h4>多选 + isRenderInTag=false</h4>
  1290. <TreeSelect
  1291. style={{ width: 300 }}
  1292. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1293. treeData={treeData}
  1294. multiple
  1295. maxTagCount={2}
  1296. placeholder="请选择"
  1297. renderSelectedItem={(item, { index, onClose }) => ({
  1298. content: (
  1299. <Tag key={index} color="white">
  1300. {item.value}
  1301. </Tag>
  1302. ),
  1303. isRenderInTag: false,
  1304. })}
  1305. />
  1306. </>
  1307. );
  1308. };
  1309. TreeSelectRenderSelectedItem.story = {
  1310. name: 'treeSelect renderSelectedItem',
  1311. };
  1312. const DisableStrictlyDemo = () => {
  1313. const [value, setValue] = useState(['Shanghai']);
  1314. const treeData = [
  1315. {
  1316. label: '亚洲',
  1317. value: 'Asia',
  1318. key: '0',
  1319. children: [
  1320. {
  1321. label: '中国',
  1322. value: 'China',
  1323. key: '0-0',
  1324. disabled: true,
  1325. children: [
  1326. {
  1327. label: '北京',
  1328. value: 'Beijing',
  1329. key: '0-0-0',
  1330. },
  1331. {
  1332. label: '上海',
  1333. value: 'Shanghai',
  1334. key: '0-0-1',
  1335. },
  1336. ],
  1337. },
  1338. {
  1339. label: '日本',
  1340. value: 'Japan',
  1341. key: '0-1',
  1342. },
  1343. ],
  1344. },
  1345. {
  1346. label: '北美洲',
  1347. value: 'North America',
  1348. key: '1',
  1349. },
  1350. ];
  1351. return (
  1352. <div>
  1353. <TreeSelect
  1354. style={{ width: 300 }}
  1355. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1356. treeData={treeData}
  1357. disableStrictly
  1358. multiple
  1359. searchPosition="trigger"
  1360. filterTreeNode
  1361. value={value}
  1362. onChange={value => setValue(value)}
  1363. />
  1364. </div>
  1365. );
  1366. };
  1367. export const DisabledStrictly = () => (
  1368. <>
  1369. <DisableStrictlyDemo />
  1370. </>
  1371. );
  1372. DisabledStrictly.story = {
  1373. name: 'disabledStrictly',
  1374. };
  1375. export const CheckRelationDemo = () => {
  1376. const treeData = treeDataEn;
  1377. const [value, setValue] = useState('China');
  1378. const [value2, setValue2] = useState();
  1379. const [value3, setValue3] = useState();
  1380. const style = {
  1381. width: 300,
  1382. };
  1383. const dropdownStyle = {
  1384. maxHeight: 400,
  1385. overflow: 'auto'
  1386. };
  1387. const handleChange = value => {
  1388. console.log(value);
  1389. setValue(value);
  1390. };
  1391. const handleChange2 = value => {
  1392. console.log(value);
  1393. setValue2(value);
  1394. };
  1395. const handleChange3 = value => {
  1396. console.log(value);
  1397. setValue3(value);
  1398. };
  1399. return (
  1400. <>
  1401. <div>checkRelation='unRelated'</div>
  1402. <TreeSelect
  1403. dropdownStyle={dropdownStyle}
  1404. treeData={treeData}
  1405. multiple
  1406. checkRelation='unRelated'
  1407. defaultExpandAll
  1408. style={style}
  1409. />
  1410. <br /><br />
  1411. <div>checkRelation='unRelated' + maxTagCount=2</div>
  1412. <TreeSelect
  1413. dropdownStyle={dropdownStyle}
  1414. treeData={treeData}
  1415. multiple
  1416. maxTagCount={2}
  1417. checkRelation='unRelated'
  1418. defaultExpandAll
  1419. style={style}
  1420. />
  1421. <br /><br />
  1422. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索</div>
  1423. <TreeSelect
  1424. dropdownStyle={dropdownStyle}
  1425. treeData={treeData}
  1426. multiple
  1427. maxTagCount={2}
  1428. filterTreeNode
  1429. checkRelation='unRelated'
  1430. defaultExpandAll
  1431. style={style}
  1432. />
  1433. <br /><br />
  1434. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索 + searchBox in trigger</div>
  1435. <TreeSelect
  1436. dropdownStyle={dropdownStyle}
  1437. treeData={treeData}
  1438. multiple
  1439. maxTagCount={2}
  1440. filterTreeNode
  1441. checkRelation='unRelated'
  1442. searchPosition='trigger'
  1443. defaultExpandAll
  1444. style={style}
  1445. />
  1446. <br /><br />
  1447. <div>checkRelation='unRelated' + 中国节点为 disabled</div>
  1448. <TreeSelect
  1449. dropdownStyle={dropdownStyle}
  1450. treeData={treeDataWithoutValue}
  1451. multiple
  1452. checkRelation='unRelated'
  1453. defaultExpandAll
  1454. style={style}
  1455. />
  1456. <br /><br />
  1457. <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
  1458. <TreeSelect
  1459. dropdownStyle={dropdownStyle}
  1460. treeData={treeDataWithoutValue}
  1461. multiple
  1462. checkRelation='unRelated'
  1463. defaultExpandAll
  1464. disableStrictly
  1465. style={style}
  1466. />
  1467. <br /><br />
  1468. <div>checkRelation='unRelated' + defaultValue 为 China</div>
  1469. <TreeSelect
  1470. dropdownStyle={dropdownStyle}
  1471. treeData={treeData}
  1472. multiple
  1473. checkRelation='unRelated'
  1474. defaultExpandAll
  1475. style={style}
  1476. defaultValue='China'
  1477. />
  1478. <br /><br />
  1479. <div>checkRelation='unRelated' + defaultValue 为 China + 开启搜索</div>
  1480. <TreeSelect
  1481. dropdownStyle={dropdownStyle}
  1482. treeData={treeData}
  1483. multiple
  1484. filterTreeNode
  1485. checkRelation='unRelated'
  1486. defaultExpandAll
  1487. style={style}
  1488. defaultValue='China'
  1489. />
  1490. <br /><br />
  1491. <div>多选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1492. <TreeSelect
  1493. dropdownStyle={dropdownStyle}
  1494. treeData={treeData}
  1495. multiple
  1496. filterTreeNode
  1497. showClear
  1498. checkRelation='unRelated'
  1499. defaultExpandAll
  1500. style={style}
  1501. searchPosition='trigger'
  1502. defaultValue={['China', 'Japan']}
  1503. />
  1504. <br /><br />
  1505. <div>单选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1506. <TreeSelect
  1507. dropdownStyle={dropdownStyle}
  1508. treeData={treeData}
  1509. filterTreeNode
  1510. showClear
  1511. checkRelation='unRelated'
  1512. defaultExpandAll
  1513. style={style}
  1514. searchPosition='trigger'
  1515. defaultValue='China'
  1516. />
  1517. <br /><br />
  1518. <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
  1519. <TreeSelect
  1520. dropdownStyle={dropdownStyle}
  1521. treeData={treeData}
  1522. multiple
  1523. checkRelation='unRelated'
  1524. defaultExpandAll
  1525. style={style}
  1526. value={value}
  1527. onChange={handleChange}
  1528. />
  1529. <br /><br />
  1530. <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
  1531. <TreeSelect
  1532. dropdownStyle={dropdownStyle}
  1533. treeData={treeData}
  1534. multiple
  1535. checkRelation='unRelated'
  1536. defaultExpandAll
  1537. style={style}
  1538. value={value2}
  1539. onChangeWithObject
  1540. onChange={handleChange2}
  1541. />
  1542. <br /><br />
  1543. <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
  1544. <TreeSelect
  1545. dropdownStyle={dropdownStyle}
  1546. leafOnly
  1547. treeData={treeData}
  1548. multiple
  1549. checkRelation='unRelated'
  1550. defaultExpandAll
  1551. style={style}
  1552. value={value3}
  1553. onChange={handleChange3}
  1554. />
  1555. <br /><br />
  1556. <div>checkRelation='unRelated' + onSelect </div>
  1557. <TreeSelect
  1558. dropdownStyle={dropdownStyle}
  1559. treeData={treeData}
  1560. multiple
  1561. checkRelation='unRelated'
  1562. defaultExpandAll
  1563. style={style}
  1564. onSelect={(value,status,node)=>console.log('select', value, status, node)}
  1565. />
  1566. </>
  1567. );
  1568. };
  1569. export const SearchableAndExpandedKeys = () => {
  1570. const [expandedKeys1, setExpandedKeys1] = useState([]);
  1571. const [expandedKeys2, setExpandedKeys2] = useState([]);
  1572. const [expandedKeys3, setExpandedKeys3] = useState([]);
  1573. return (
  1574. <>
  1575. <Title heading={6}>expandedKeys 受控</Title>
  1576. <TreeSelect
  1577. style={{ width: 300, marginBottom: 30 }}
  1578. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1579. treeData={treeData2}
  1580. expandedKeys={expandedKeys1}
  1581. defaultValue='beijing'
  1582. onExpand={v => {
  1583. console.log('onExpand value: ', v);
  1584. setExpandedKeys1(v);
  1585. }}
  1586. />
  1587. <Title heading={6}>expandedKeys 受控 + 开启搜索</Title>
  1588. <TreeSelect
  1589. style={{ width: 300, marginBottom: 30 }}
  1590. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1591. treeData={treeData2}
  1592. filterTreeNode
  1593. defaultValue='beijing'
  1594. expandedKeys={expandedKeys2}
  1595. onExpand={v => {
  1596. console.log('onExpand value: ', v);
  1597. setExpandedKeys2(v);
  1598. }}
  1599. />
  1600. <Title heading={6}>expandedKeys 受控 + 开启搜索 + 搜索时更新 expandedKeys</Title>
  1601. <TreeSelect
  1602. style={{ width: 300, marginBottom: 30 }}
  1603. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1604. treeData={treeData2}
  1605. filterTreeNode
  1606. expandedKeys={expandedKeys3}
  1607. defaultValue='beijing'
  1608. onExpand={v => {
  1609. console.log('onExpand value: ', v);
  1610. setExpandedKeys3(v)
  1611. }}
  1612. onSearch={(input, filterExpandedKeys) => {
  1613. console.log('onExpand filterExpandedKeys: ', filterExpandedKeys);
  1614. setExpandedKeys3(filterExpandedKeys);
  1615. }}
  1616. />
  1617. </>
  1618. )
  1619. }
  1620. export const loadData = () => {
  1621. const initialData = [
  1622. {
  1623. label: 'Expand to load',
  1624. value: '0',
  1625. key: '0',
  1626. },
  1627. {
  1628. label: 'Expand to load',
  1629. value: '1',
  1630. key: '1',
  1631. },
  1632. {
  1633. label: 'Leaf Node',
  1634. value: '2',
  1635. key: '2',
  1636. isLeaf: true,
  1637. },
  1638. ];
  1639. const [treeData, setTreeData] = useState(initialData);
  1640. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1641. function updateTreeData(list, key, children) {
  1642. return list.map(node => {
  1643. if (node.key === key) {
  1644. return { ...node, children };
  1645. }
  1646. if (node.children) {
  1647. return { ...node, children: updateTreeData(node.children, key, children) };
  1648. }
  1649. return node;
  1650. });
  1651. }
  1652. function onLoadData({ key, children }) {
  1653. return new Promise(resolve => {
  1654. if (children) {
  1655. resolve();
  1656. return;
  1657. }
  1658. setTimeout(() => {
  1659. setTreeData(origin =>
  1660. updateTreeData(origin, key, [
  1661. {
  1662. label: 'Child Node',
  1663. key: `${key}-0`,
  1664. },
  1665. {
  1666. label: 'Child Node',
  1667. key: `${key}-1`,
  1668. },
  1669. ]),
  1670. );
  1671. resolve();
  1672. }, 1000);
  1673. });
  1674. }
  1675. return (
  1676. <TreeSelect
  1677. loadData={onLoadData}
  1678. filterTreeNode
  1679. treeData={treeData}
  1680. style={{ width: 300 }}
  1681. placeholder="请选择"
  1682. />
  1683. );
  1684. }
  1685. export const loadDataAndLoadedkeys = () => {
  1686. const initialData = [
  1687. {
  1688. label: 'Expand to load',
  1689. value: '0',
  1690. key: '0',
  1691. },
  1692. {
  1693. label: 'Expand to load',
  1694. value: '1',
  1695. key: '1',
  1696. },
  1697. {
  1698. label: 'Leaf Node',
  1699. value: '2',
  1700. key: '2',
  1701. isLeaf: true,
  1702. },
  1703. ];
  1704. const [treeData, setTreeData] = useState(initialData);
  1705. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1706. function updateTreeData(list, key, children) {
  1707. return list.map(node => {
  1708. if (node.key === key) {
  1709. return { ...node, children };
  1710. }
  1711. if (node.children) {
  1712. return { ...node, children: updateTreeData(node.children, key, children) };
  1713. }
  1714. return node;
  1715. });
  1716. }
  1717. function updateLoadedKeys(key) {
  1718. if(!loadedKeys.includes(key)){
  1719. setLoadedKeys([...loadedKeys, key]);
  1720. console.log('[...loadedKeys, key]', [...loadedKeys, key]);
  1721. }
  1722. }
  1723. function onLoadData({ key, children }) {
  1724. return new Promise(resolve => {
  1725. if (children) {
  1726. resolve();
  1727. return;
  1728. }
  1729. setTimeout(() => {
  1730. setTreeData(origin =>
  1731. updateTreeData(origin, key, [
  1732. {
  1733. label: 'Child Node',
  1734. key: `${key}-0`,
  1735. },
  1736. {
  1737. label: 'Child Node',
  1738. key: `${key}-1`,
  1739. },
  1740. ]),
  1741. );
  1742. // updateLoadedKeys(key);
  1743. resolve();
  1744. }, 1000);
  1745. });
  1746. }
  1747. return (
  1748. <TreeSelect
  1749. loadData={onLoadData}
  1750. filterTreeNode
  1751. // loadedKeys={loadedKeys}
  1752. treeData={treeData}
  1753. style={{ width: 300 }}
  1754. placeholder="请选择"
  1755. />
  1756. );
  1757. }
  1758. export const size = () => {
  1759. const props = {
  1760. style: { width: 300 },
  1761. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  1762. defaultValue: ['0-0-1', '0-0-2', '0-0-3', '0-0-4', '0-0-5', '0-0-6', '0-0-7'],
  1763. treeData: treeData3,
  1764. multiple: true,
  1765. filterTreeNode: true,
  1766. searchPosition: "trigger"
  1767. };
  1768. return (<>
  1769. <TreeSelect {...props} size={'small'} placeholder={'small'} />
  1770. <br/><br/>
  1771. <TreeSelect {...props} size={'default'} placeholder={'default'} />
  1772. <br/><br/>
  1773. <TreeSelect {...props} size={'large'} placeholder={'large'} />
  1774. </>);
  1775. }
  1776. export const valueNotInTreeDataIssue = () => {
  1777. const treeData = [
  1778. {
  1779. key: "test",
  1780. label: "测试标签",
  1781. children: [
  1782. {
  1783. key: "test_2",
  1784. label: "测试二级标签"
  1785. },
  1786. {
  1787. key: "jzr_test",
  1788. label: "之睿测试"
  1789. }
  1790. ]
  1791. },
  1792. {
  1793. key: "create",
  1794. label: "创作构思",
  1795. children: [
  1796. {
  1797. key: "material",
  1798. label: "素材积累"
  1799. },
  1800. {
  1801. key: "lens_script",
  1802. label: "分镜脚本"
  1803. }
  1804. ]
  1805. }
  1806. ];
  1807. const treeDataWithValue = [
  1808. {
  1809. value: "test",
  1810. key: "0",
  1811. label: "测试标签",
  1812. children: [
  1813. {
  1814. value: "test_2",
  1815. key: "0-1",
  1816. label: "测试二级标签"
  1817. },
  1818. {
  1819. value: "jzr_test",
  1820. key: "0-2",
  1821. label: "之睿测试"
  1822. }
  1823. ]
  1824. },
  1825. {
  1826. value: "create",
  1827. key: "1",
  1828. label: "创作构思",
  1829. children: [
  1830. {
  1831. value: "material",
  1832. key: "1-1",
  1833. label: "素材积累"
  1834. },
  1835. {
  1836. value: "lens_script",
  1837. key: "1-2",
  1838. label: "分镜脚本"
  1839. }
  1840. ]
  1841. }
  1842. ];
  1843. const commonProps = useMemo(() => {
  1844. return {
  1845. multiple: true,
  1846. style: { width: 300 },
  1847. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  1848. onChange: (value) => {
  1849. console.log('onChange', value);
  1850. },
  1851. onSelect: (value) => {
  1852. console.log('onSelect', value);
  1853. },
  1854. };
  1855. }, []);
  1856. return (
  1857. <>
  1858. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,无 value</p>
  1859. <p>checkRelation='related'</p>
  1860. <TreeSelect
  1861. defaultExpandAll
  1862. defaultValue={["test_2", 0]}
  1863. treeData={treeData}
  1864. {...commonProps}
  1865. />
  1866. <p>checkRelation='unRelated'</p>
  1867. <TreeSelect
  1868. defaultExpandAll
  1869. defaultValue={["test_2", "fish"]}
  1870. checkRelation='unRelated'
  1871. treeData={treeData}
  1872. {...commonProps}
  1873. />
  1874. <p>onChangeWithObject, checkRelation='related'</p>
  1875. <TreeSelect
  1876. defaultExpandAll
  1877. onChangeWithObject
  1878. defaultValue={[
  1879. {
  1880. key: "test_2",
  1881. label: "测试二级标签"
  1882. },
  1883. {
  1884. key: "fish",
  1885. label: "鱼"
  1886. }
  1887. ]}
  1888. treeData={treeData}
  1889. {...commonProps}
  1890. />
  1891. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1892. <TreeSelect
  1893. defaultExpandAll
  1894. onChangeWithObject
  1895. defaultValue={[
  1896. {
  1897. key: "test_2",
  1898. label: "测试二级标签"
  1899. },
  1900. {
  1901. key: "fish",
  1902. label: "鱼"
  1903. }
  1904. ]}
  1905. treeData={treeData}
  1906. {...commonProps}
  1907. />
  1908. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,有 value</p>
  1909. <p>checkRelation='related'</p>
  1910. <TreeSelect
  1911. defaultExpandAll
  1912. defaultValue={["test", "fish"]}
  1913. treeData={treeDataWithValue}
  1914. {...commonProps}
  1915. />
  1916. <p>checkRelation='unRelated'</p>
  1917. <TreeSelect
  1918. defaultExpandAll
  1919. defaultValue={["test", "fish"]}
  1920. checkRelation='unRelated'
  1921. treeData={treeDataWithValue}
  1922. {...commonProps}
  1923. />
  1924. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1925. <TreeSelect
  1926. defaultExpandAll
  1927. onChangeWithObject
  1928. defaultValue={[
  1929. {
  1930. value: "test_2",
  1931. key: "0-1",
  1932. label: "测试二级标签"
  1933. },
  1934. {
  1935. key: "fish",
  1936. value: "Fish",
  1937. label: "鱼"
  1938. }
  1939. ]}
  1940. treeData={treeDataWithValue}
  1941. {...commonProps}
  1942. />
  1943. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1944. <TreeSelect
  1945. defaultExpandAll
  1946. onChangeWithObject
  1947. defaultValue={[
  1948. {
  1949. value: "test_2",
  1950. key: "0-1",
  1951. label: "测试二级标签"
  1952. },
  1953. {
  1954. key: "fish",
  1955. value: "Fish",
  1956. label: "鱼"
  1957. }
  1958. ]}
  1959. treeData={treeDataWithValue}
  1960. {...commonProps}
  1961. />
  1962. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,无 value</p>
  1963. <TreeSelect
  1964. defaultExpandAll
  1965. defaultValue={"fish"}
  1966. treeData={treeData}
  1967. {...commonProps}
  1968. multiple={false}
  1969. />
  1970. <p>onChangeWithObject</p>
  1971. <TreeSelect
  1972. defaultExpandAll
  1973. defaultValue={{
  1974. key: "fish",
  1975. value: "Fish",
  1976. label: "鱼"
  1977. }}
  1978. treeData={treeData}
  1979. {...commonProps}
  1980. multiple={false}
  1981. onChangeWithObject
  1982. />
  1983. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,有 value</p>
  1984. <TreeSelect
  1985. defaultExpandAll
  1986. defaultValue={"fish"}
  1987. treeData={treeDataWithValue}
  1988. {...commonProps}
  1989. multiple={false}
  1990. />
  1991. <p>onChangeWithObject</p>
  1992. <TreeSelect
  1993. defaultExpandAll
  1994. defaultValue={{
  1995. key: "fish",
  1996. value: "Fish",
  1997. label: "鱼"
  1998. }}
  1999. treeData={treeDataWithValue}
  2000. {...commonProps}
  2001. multiple={false}
  2002. onChangeWithObject
  2003. />
  2004. </>
  2005. );
  2006. };
  2007. class ValueTypeIsNumber extends React.Component {
  2008. constructor() {
  2009. super();
  2010. this.state = {
  2011. value: 1
  2012. };
  2013. }
  2014. onChange(value) {
  2015. console.log('onChange', value);
  2016. this.setState({ value });
  2017. }
  2018. render() {
  2019. const treeData = [
  2020. {
  2021. label: '北美洲',
  2022. value: 'North America',
  2023. key: '1',
  2024. },
  2025. {
  2026. label: '亚洲',
  2027. value: 'Asia',
  2028. key: '0',
  2029. children: [
  2030. {
  2031. label: '中国',
  2032. value: 'China',
  2033. key: '0-0',
  2034. children: [
  2035. {
  2036. label: '北京',
  2037. value: 'Beijing',
  2038. key: '0-0-0',
  2039. },
  2040. {
  2041. label: '上海',
  2042. value: 'Shanghai',
  2043. key: '0-0-1',
  2044. },
  2045. ],
  2046. },
  2047. ],
  2048. },
  2049. ];
  2050. return (
  2051. <TreeSelect
  2052. style={{ width: 300 }}
  2053. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2054. treeData={treeData}
  2055. value={this.state.value}
  2056. placeholder="请选择"
  2057. multiple
  2058. onChange={e => this.onChange(e)}
  2059. />
  2060. );
  2061. }
  2062. }
  2063. export const valueIsNumber = () => <ValueTypeIsNumber />
  2064. export const searchPositionInTriggerAndVirtualize = () => {
  2065. return (
  2066. <>
  2067. <TreeSelect
  2068. searchPosition="trigger"
  2069. style={{ width: 300 }}
  2070. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2071. treeData={treeData2}
  2072. filterTreeNode
  2073. placeholder="单选"
  2074. virtualize={{
  2075. itemSize: 28,
  2076. // dropDown height 300 minus search box height minus padding 8 * 2
  2077. // or if you set dropdown height, it will fill 100% of rest space
  2078. height: 236
  2079. }}
  2080. />
  2081. </>
  2082. );
  2083. };
  2084. export const clickTriggerToHide = () => (
  2085. <>
  2086. <p>clickTriggerToHide 未设置,默认为 true</p>
  2087. <TreeSelect
  2088. style={{ width: 300 }}
  2089. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2090. treeData={treeData2}
  2091. placeholder="单选"
  2092. />
  2093. <p>clickTriggerToHide 设置为 false</p>
  2094. <TreeSelect
  2095. style={{ width: 300 }}
  2096. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2097. treeData={treeData2}
  2098. placeholder="单选"
  2099. clickTriggerToHide={false}
  2100. />
  2101. </>
  2102. );
  2103. export const triggerRenderAddMethod = () => {
  2104. const treeData = useMemo(() => [
  2105. {
  2106. label: '亚洲',
  2107. value: '亚洲',
  2108. key: '0',
  2109. children: [
  2110. {
  2111. label: '中国',
  2112. value: '中国',
  2113. key: '0-0',
  2114. children: [
  2115. {
  2116. label: '北京',
  2117. value: '北京',
  2118. key: '0-0-0',
  2119. },
  2120. {
  2121. label: '上海',
  2122. value: '上海',
  2123. key: '0-0-1',
  2124. },
  2125. ],
  2126. },
  2127. ],
  2128. },
  2129. {
  2130. label: '北美洲',
  2131. value: '北美洲',
  2132. key: '1',
  2133. }
  2134. ], []);
  2135. const onValueChange = useCallback((value) => {
  2136. console.log('onChange', value);
  2137. });
  2138. const closeIcon = useCallback((value, onClear) => {
  2139. return value && value.length ? <IconClose onClick={onClear} /> : <IconChevronDown />;
  2140. }, []);
  2141. const renderTagItem = useCallback((item, onRemove) => (
  2142. <Tag closable key={item.key} onClose={() => { onRemove(item.key); }}>{item.label}</Tag>
  2143. ), []);
  2144. const renderTrigger1 = useCallback((props) => {
  2145. const { value, placeholder, onClear } = props;
  2146. return (
  2147. <Button theme={'light'} icon={closeIcon(value, onClear)} iconPosition={'right'}>
  2148. {value && value.length ? value.map(item => item.label).join(',') : placeholder}
  2149. </Button>
  2150. );
  2151. }, []);
  2152. const renderTrigger2 = useCallback((props) => {
  2153. const { value, onSearch, onRemove, onClear } = props;
  2154. return (
  2155. <div style={{ border: '1px solid grey', width: 'fit-content', padding: 5, borderRadius: 5 }}>
  2156. {value && value.length > 0 &&
  2157. <div style={{ width: 'fit-content', minWidth: 10, padding: 5 }}>
  2158. {value.map(item => renderTagItem(item, onRemove))}
  2159. </div>
  2160. }
  2161. <Input style={{ width: 200 }} onChange={onSearch} />
  2162. {closeIcon(value, onClear)}
  2163. </div>
  2164. );
  2165. }, []);
  2166. const renderTrigger3 = useCallback((props) => {
  2167. const { value, onSearch, onRemove, inputValue } = props;
  2168. const tagInputValue = value.map(item => item.key);
  2169. const renderTagInMultiple = (key) => {
  2170. const label = value.find(item => item.key === key).label;
  2171. const onCloseTag = (value, e, tagKey) => {
  2172. onRemove(tagKey);
  2173. }
  2174. return <Tag style={{ marginLeft: 2 }} tagKey={key} key={key} onClose={onCloseTag} closable>{label}</Tag>
  2175. }
  2176. return (
  2177. <TagInput
  2178. inputValue={inputValue}
  2179. value={tagInputValue}
  2180. onInputChange={onSearch}
  2181. renderTagItem={renderTagInMultiple}
  2182. />
  2183. )
  2184. }, []);
  2185. return (
  2186. <>
  2187. <TreeSelect
  2188. triggerRender={renderTrigger1}
  2189. treeData={treeData}
  2190. placeholder='Single, Custom Trigger'
  2191. onChange={onValueChange}
  2192. style={{ width: 300 }}
  2193. />
  2194. <br />
  2195. <TreeSelect
  2196. triggerRender={renderTrigger1}
  2197. multiple
  2198. treeData={treeData}
  2199. placeholder='Multiple, custom Trigger'
  2200. onChange={onValueChange}
  2201. style={{ width: 300 }}
  2202. />
  2203. <br />
  2204. <TreeSelect
  2205. triggerRender={renderTrigger2}
  2206. filterTreeNode
  2207. searchPosition="trigger"
  2208. multiple
  2209. treeData={treeData}
  2210. placeholder='Custom Trigger'
  2211. onChange={onValueChange}
  2212. style={{ width: 300 }}
  2213. />
  2214. <br />
  2215. <TreeSelect
  2216. defaultExpandAll
  2217. triggerRender={renderTrigger3}
  2218. filterTreeNode
  2219. searchPosition="trigger"
  2220. multiple
  2221. treeData={treeData}
  2222. placeholder='Custom Trigger'
  2223. onChange={onValueChange}
  2224. style={{ width: 300 }}
  2225. />
  2226. <br />
  2227. <TreeSelect
  2228. defaultExpandAll
  2229. checkRelation={'unRelated'}
  2230. triggerRender={renderTrigger3}
  2231. filterTreeNode
  2232. searchPosition="trigger"
  2233. multiple
  2234. treeData={treeData}
  2235. placeholder='multiple, checkRelation = unRelated'
  2236. onChange={onValueChange}
  2237. style={{ width: 300 }}
  2238. />
  2239. </>
  2240. );
  2241. }
  2242. export const AutoSearchFocusPlusPreventScroll = () => {
  2243. return (
  2244. <div>
  2245. <div style={{height: '100vh' }}>我是一个高度和视窗高度一致的div。
  2246. <br />由于 TreeSelect 设置了 searchAutoFocus 以及 preventScroll,
  2247. <br /> 符合预期的情况是在没有滚动屏幕情况下,你不会看到 TreeSelect 的 trigger
  2248. </div>
  2249. <TreeSelect
  2250. searchAutoFocus
  2251. searchPosition="trigger"
  2252. preventScroll={true}
  2253. style={{ width: 300 }}
  2254. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2255. treeData={treeData2}
  2256. filterTreeNode
  2257. placeholder="单选"
  2258. />
  2259. </div>
  2260. );
  2261. };
  2262. export const LongLabel = () => {
  2263. const treeData = [
  2264. {
  2265. label: '这是一个超长的中文测试用标题这是一个超长的中文测试用标题这是一个超长的中文测试用标题这是一个超长的中文测试用标题',
  2266. value: 'v1',
  2267. key: '0',
  2268. },
  2269. {
  2270. label: 'ThisISAVeryLongTestSentenceThisISAVeryLongTestSentenceThisISAVeryLongTestSentence',
  2271. value: 'v2',
  2272. key: '1',
  2273. }
  2274. ];
  2275. return (
  2276. <>
  2277. <p>单选</p>
  2278. <TreeSelect
  2279. defaultValue='v1'
  2280. style={{ width: 300 }}
  2281. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2282. treeData={treeData}
  2283. placeholder="请选择"
  2284. />
  2285. <p>单选,可搜索, searchPosition='trigger'</p>
  2286. <TreeSelect
  2287. filterTreeNode
  2288. searchPosition='trigger'
  2289. defaultValue='v1'
  2290. style={{ width: 300 }}
  2291. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2292. treeData={treeData}
  2293. placeholder="请选择"
  2294. />
  2295. <p>单选,可搜索, searchPosition='dropDown'</p>
  2296. <TreeSelect
  2297. filterTreeNode
  2298. defaultValue='v1'
  2299. style={{ width: 300 }}
  2300. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2301. treeData={treeData}
  2302. placeholder="请选择"
  2303. />
  2304. <p>单选</p>
  2305. <TreeSelect
  2306. defaultValue='v2'
  2307. style={{ width: 300 }}
  2308. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2309. treeData={treeData}
  2310. placeholder="请选择"
  2311. />
  2312. <p>单选,可搜索, searchPosition='trigger'</p>
  2313. <TreeSelect
  2314. filterTreeNode
  2315. searchPosition='trigger'
  2316. defaultValue='v2'
  2317. style={{ width: 300 }}
  2318. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2319. treeData={treeData}
  2320. placeholder="请选择"
  2321. />
  2322. <p>单选,可搜索, searchPosition='dropDown'</p>
  2323. <TreeSelect
  2324. filterTreeNode
  2325. defaultValue='v2'
  2326. style={{ width: 300 }}
  2327. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2328. treeData={treeData}
  2329. placeholder="请选择"
  2330. />
  2331. </>
  2332. );
  2333. }
  2334. export const UnRelatedAndAsyncLoad = () => {
  2335. const initialData = [
  2336. {
  2337. label: 'Expand to load0',
  2338. value: '0',
  2339. key: '0',
  2340. },
  2341. {
  2342. label: 'Expand to load1',
  2343. value: '1',
  2344. key: '1',
  2345. },
  2346. {
  2347. label: 'Leaf Node',
  2348. value: '2',
  2349. key: '2',
  2350. isLeaf: true,
  2351. },
  2352. ];
  2353. const [treeData, setTreeData] = useState(initialData);
  2354. function updateTreeData(list, key, children) {
  2355. return list.map(node => {
  2356. if (node.key === key) {
  2357. return { ...node, children };
  2358. }
  2359. if (node.children) {
  2360. return { ...node, children: updateTreeData(node.children, key, children) };
  2361. }
  2362. return node;
  2363. });
  2364. }
  2365. function onLoadData({ key, children }) {
  2366. return new Promise(resolve => {
  2367. if (children) {
  2368. resolve();
  2369. return;
  2370. }
  2371. setTimeout(() => {
  2372. setTreeData(origin =>
  2373. updateTreeData(origin, key, [
  2374. {
  2375. label: `Child Node${key}-0`,
  2376. key: `${key}-0`,
  2377. },
  2378. {
  2379. label: `Child Node${key}-1`,
  2380. key: `${key}-1`,
  2381. },
  2382. ]),
  2383. );
  2384. resolve();
  2385. }, 1000);
  2386. });
  2387. }
  2388. return (
  2389. <>
  2390. <span>issue 1852: checkRelation='unRelated', 异步加载数据</span>
  2391. <TreeSelect
  2392. checkRelation='unRelated'
  2393. defaultValue={['0']}
  2394. multiple
  2395. defaultOpen
  2396. loadData={onLoadData}
  2397. treeData={[...treeData]}
  2398. />
  2399. </>
  2400. );
  2401. };
  2402. const constructLargeData = () => {
  2403. const newArray = (new Array(10)).fill(0).map((item, m) => {
  2404. const parent = {
  2405. key: `key-${m}`,
  2406. label: `node-${m}`,
  2407. children: []
  2408. }
  2409. new Array(100).fill(0).map((item, n) => {
  2410. const children = {
  2411. key: `key-${m}-${n}`,
  2412. label: `value-${m}-${n}`,
  2413. children: []
  2414. }
  2415. new Array(10).fill(0).map((item, o) => {
  2416. const grandChildren = {
  2417. key: `key-${m}-${n}-${o}`,
  2418. label: `value-${m}-${n}-${o}`,
  2419. }
  2420. children.children.push(grandChildren);
  2421. });
  2422. parent.children.push(children);
  2423. });
  2424. return parent;
  2425. });
  2426. return newArray;
  2427. }
  2428. export const ChangeTreeData = () => {
  2429. const [sign, setSign] = useState(true);
  2430. const treeData1 = useMemo(() => {
  2431. return constructLargeData();
  2432. }, []);
  2433. const treeData2 = useMemo(() => {
  2434. return constructLargeData();
  2435. }, []);
  2436. const onButtonClick = useCallback(() => {
  2437. setSign((sign) => {
  2438. return !sign;
  2439. })
  2440. }, []);
  2441. return <>
  2442. <Button onClick={onButtonClick}>点击修改TreeData</Button>
  2443. <br/><br/>
  2444. <TreeSelect
  2445. treeData={sign ? treeData1 : treeData2}
  2446. style={{ width: 300 }}
  2447. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2448. placeholder="请选择"
  2449. />
  2450. </>
  2451. }
  2452. export const KeyMaps = () => {
  2453. const [withObject, setWithObject] = useState(false);
  2454. const [value1, setValue1] = useState(undefined);
  2455. const [value2, setValue2] = useState(undefined);
  2456. const [expandKeys, setExpandedKeys] = useState(["yazhou", 'zhongguo']);
  2457. const switchChange = useCallback((checked) => {
  2458. setWithObject(checked);
  2459. setValue1(undefined);
  2460. setValue2(undefined);
  2461. }, []);
  2462. const onSingleChange = useCallback((value) => {
  2463. console.log('onSingleChange', value);
  2464. setValue1(value);
  2465. }, []);
  2466. const onMultipleChange = useCallback((value) => {
  2467. console.log('onMultipleChange', value);
  2468. setValue2(value);
  2469. }, []);
  2470. const normalChange = useCallback((value) => {
  2471. console.log('onChange', value);
  2472. }, []);
  2473. const normalExpand = useCallback((expandedKeys, {expanded, node}) => {
  2474. console.log('onExpanded', expandedKeys, expanded, copy(node));
  2475. }, []);
  2476. const keyMaps = useMemo(() => {
  2477. return {
  2478. // value: 'value1',
  2479. key: 'key1',
  2480. label: 'label1',
  2481. children: 'children1',
  2482. disabled: 'disabled1'
  2483. };
  2484. }, []);
  2485. const regularTreeProps = useMemo(() => ({
  2486. keyMaps: keyMaps,
  2487. treeData: specialTreeData,
  2488. style: { width: 300 },
  2489. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  2490. onChange: normalChange,
  2491. onExpand: normalExpand,
  2492. onChangeWithObject: withObject,
  2493. }), [withObject]);
  2494. const defaultSelectedObj = {
  2495. label1: '北京',
  2496. // value1: 'Beijing',
  2497. key1: 'beijing',
  2498. };
  2499. return (
  2500. <>
  2501. <span>onChangeWithObject</span><Switch checked={withObject} onChange={switchChange} />
  2502. <div key={String(withObject)} style={{ marginTop: 10 }}>
  2503. <div> Single select</div>
  2504. <TreeSelect
  2505. {...regularTreeProps}
  2506. defaultValue={withObject ? defaultSelectedObj : 'beijing'}
  2507. />
  2508. <div> Single select, onSearch, filterTreeNode, treeNodeFilterProp</div>
  2509. <TreeSelect
  2510. {...regularTreeProps}
  2511. filterTreeNode={(inputValue, treeNodeString, data)=> {
  2512. console.log("filterTreeNode", inputValue, treeNodeString, data);
  2513. return treeNodeString.includes(inputValue);
  2514. }}
  2515. treeNodeFilterProp={"key1"}
  2516. onSearch={(input, filteredExpandedKeys) => {
  2517. console.log('onSearch', input, filteredExpandedKeys);
  2518. }}
  2519. />
  2520. <div>Single select, controlled</div>
  2521. <TreeSelect
  2522. {...regularTreeProps}
  2523. value={value1}
  2524. onChange={onSingleChange}
  2525. />
  2526. <div> Multiple select</div>
  2527. <TreeSelect
  2528. {...regularTreeProps}
  2529. multiple
  2530. defaultValue={withObject ? [defaultSelectedObj] : ['beijing']}
  2531. />
  2532. <div> Multiple select, controlled</div>
  2533. <TreeSelect
  2534. {...regularTreeProps}
  2535. value={value2}
  2536. multiple
  2537. onChange={onMultipleChange}
  2538. />
  2539. <div> Multiple select, disableStrictly</div>
  2540. <TreeSelect
  2541. {...regularTreeProps}
  2542. multiple
  2543. disableStrictly
  2544. />
  2545. <div> Multiple, 展开受控</div>
  2546. <TreeSelect
  2547. {...regularTreeProps}
  2548. multiple
  2549. expandedKeys={expandKeys}
  2550. onExpand={(expandedKeys, {expanded, node}) => {
  2551. console.log('onExpanded', expandedKeys, expanded, copy(node));
  2552. setExpandedKeys(expandedKeys);
  2553. }}
  2554. />
  2555. </div>
  2556. </>
  2557. );
  2558. }
  2559. export const Issue1542 = () => {
  2560. const [expandedKeys, setExpandedKeys] = useState([]);
  2561. const treeData = treeDataEn;
  2562. const onExpand = useCallback((expandedKeys) => {
  2563. setExpandedKeys(expandedKeys);
  2564. }, [expandedKeys]);
  2565. const onSearch = useCallback((inputValue, filteredExpandedKeys) => {
  2566. const set = new Set([...filteredExpandedKeys, ...expandedKeys]);
  2567. setExpandedKeys(Array.from(set));
  2568. }, [setExpandedKeys]);
  2569. return (
  2570. <>
  2571. <TreeSelect
  2572. // multiple
  2573. style={{ width: 300 }}
  2574. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2575. treeData={treeData}
  2576. filterTreeNode
  2577. searchPosition='trigger'
  2578. showFilteredOnly
  2579. expandedKeys={expandedKeys}
  2580. onExpand={onExpand}
  2581. onSearch={onSearch}
  2582. />
  2583. </>
  2584. );
  2585. };
  2586. class WebComponentWrapper extends HTMLElement {
  2587. constructor() {
  2588. super();
  2589. this.attachShadow({ mode: 'open' });
  2590. }
  2591. connectedCallback() {
  2592. ReactDOM.render(<_TreeSelect />, this.shadowRoot);
  2593. }
  2594. }
  2595. customElements.define('my-web-component', WebComponentWrapper);
  2596. export const WebCompTestOutside = () => {
  2597. return (
  2598. <my-web-component></my-web-component>
  2599. );
  2600. };
  2601. export const CustomSelectAll = () => {
  2602. const [value, setValue] = useState([]);
  2603. const [filteredNodes, setFilteredNodes] = useState([])
  2604. const treeData = treeDataEn;
  2605. const onSearch = useCallback((inputValue, filteredExpandedKeys, _filteredNodes) => {
  2606. setFilteredNodes(_filteredNodes)
  2607. }, []);
  2608. const handleOnChange = useCallback((value) => {
  2609. setValue(value);
  2610. }, [])
  2611. // 是否全选
  2612. const allSelected = useMemo(() => {
  2613. if (!filteredNodes.length) {
  2614. return false;
  2615. }
  2616. const optionValues = filteredNodes.map(i => i.value);
  2617. return !without(optionValues, ...value).length;
  2618. }, [filteredNodes, value]);
  2619. const handleOnAllSelect = useCallback(() => {
  2620. const optionValues = filteredNodes.map(i => i.value);
  2621. handleOnChange(allSelected ? [] : optionValues);
  2622. }, [allSelected, handleOnChange, filteredNodes]);
  2623. const outerBottomSlot = useMemo(() => {
  2624. if (!filteredNodes.length) {
  2625. // 未筛选状态下不展示按钮
  2626. return null;
  2627. }
  2628. return (
  2629. <div style={{ padding: '5px 20px', borderTop: '1px solid var(--semi-color-border)'}}>
  2630. <Typography.Text link={true} onClick={handleOnAllSelect}>
  2631. {allSelected ? '取消全选' : '全选'}
  2632. </Typography.Text>
  2633. </div>
  2634. );
  2635. }, [allSelected, handleOnAllSelect, filteredNodes]);
  2636. return (
  2637. <>
  2638. <span>本用例借助 onSearch 的第三个参数_filteredNodes 自定义搜索全选功能 </span>
  2639. <br />
  2640. <TreeSelect
  2641. multiple
  2642. style={{ width: 300 }}
  2643. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2644. treeData={treeData}
  2645. filterTreeNode
  2646. searchPosition='trigger'
  2647. showFilteredOnly
  2648. onSearch={onSearch}
  2649. onChange={handleOnChange}
  2650. value={value}
  2651. showClear
  2652. outerBottomSlot={outerBottomSlot}
  2653. />
  2654. </>
  2655. );
  2656. };
  2657. export const AutoMerge = () => {
  2658. const [value, setValue] = useState([]);
  2659. const onChange = useCallback((val) => {
  2660. console.log('onChange', val);
  2661. setValue(val);
  2662. }, []);
  2663. return (
  2664. <>
  2665. <TreeSelect
  2666. autoMergeValue={false}
  2667. style={{ width: 300}}
  2668. multiple
  2669. value={value}
  2670. onChange={onChange}
  2671. treeData={treeData1}
  2672. />
  2673. </>
  2674. )
  2675. }
  2676. export const showFilteredOnly = () => {
  2677. return (
  2678. <>
  2679. <span id='info'>searchPosition="trigger", showFilteredOnly, multiple</span>
  2680. <br />
  2681. <TreeSelect
  2682. searchPosition="trigger"
  2683. style={{ width: 300 }}
  2684. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2685. treeData={treeData2}
  2686. multiple
  2687. filterTreeNode
  2688. showFilteredOnly
  2689. maxTagCount={2}
  2690. placeholder="多选"
  2691. />
  2692. </>
  2693. );
  2694. }
  2695. export const EmptyContent = () => {
  2696. const treeData = [
  2697. {
  2698. label: '亚洲',
  2699. value: 'Asia',
  2700. key: '0',
  2701. children: [
  2702. {
  2703. label: '中国',
  2704. value: 'China',
  2705. key: '0-0',
  2706. children: [
  2707. {
  2708. label: '北京',
  2709. value: 'Beijing',
  2710. key: '0-0-0',
  2711. },
  2712. {
  2713. label: '上海',
  2714. value: 'Shanghai',
  2715. key: '0-0-1',
  2716. },
  2717. ],
  2718. },
  2719. ],
  2720. },
  2721. {
  2722. label: '北美洲',
  2723. value: 'North America',
  2724. key: '1',
  2725. }
  2726. ];
  2727. return (
  2728. <>
  2729. <TreeSelect
  2730. style={{ width: 400 }}
  2731. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2732. treeData={[]}
  2733. placeholder="点击 trigger 查看 emptyContent 为 null 效果"
  2734. emptyContent={null}
  2735. />
  2736. <br /><br />
  2737. <TreeSelect
  2738. style={{ width: 400 }}
  2739. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2740. treeData={treeData}
  2741. placeholder="输入 v 查看 emptyContent 为 null 效果"
  2742. filterTreeNode
  2743. showFilteredOnly
  2744. searchPosition={"trigger"}
  2745. emptyContent={null}
  2746. />
  2747. </>
  2748. );
  2749. }
  2750. export const filterAndKeyMaps = () => {
  2751. const treeData = [
  2752. {
  2753. name: 'Asia',
  2754. value: 'Asia',
  2755. key: '0',
  2756. children: [
  2757. {
  2758. name: 'China',
  2759. value: 'China',
  2760. key: '0-0',
  2761. children: [
  2762. {
  2763. name: 'Beijing',
  2764. value: 'Beijing',
  2765. key: '0-0-0',
  2766. },
  2767. {
  2768. name: 'Shanghai',
  2769. value: 'Shanghai',
  2770. key: '0-0-1',
  2771. },
  2772. ],
  2773. },
  2774. {
  2775. name: 'Japan',
  2776. value: 'Japan',
  2777. key: '0-1',
  2778. children: [
  2779. {
  2780. name: 'Osaka',
  2781. value: 'Osaka',
  2782. key: '0-1-0'
  2783. }
  2784. ]
  2785. },
  2786. ],
  2787. },
  2788. {
  2789. name: 'North America',
  2790. value: 'North America',
  2791. key: '1',
  2792. children: [
  2793. {
  2794. name: 'United States',
  2795. value: 'United States',
  2796. key: '1-0'
  2797. },
  2798. {
  2799. name: 'Canada',
  2800. value: 'Canada',
  2801. key: '1-1'
  2802. }
  2803. ]
  2804. }
  2805. ];
  2806. return (
  2807. <TreeSelect
  2808. style={{ width: 300 }}
  2809. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2810. treeData={treeData}
  2811. filterTreeNode
  2812. keyMaps={{
  2813. key: 'key',
  2814. value: 'value',
  2815. label: 'name',
  2816. }}
  2817. placeholder="单选可搜索的"
  2818. />
  2819. )
  2820. }
  2821. export const CustomExpandIcon = () => {
  2822. const expandIconFunc = useCallback((props) => {
  2823. const { expanded, onClick, className } = props;
  2824. if (expanded) {
  2825. return <IconMinus size="small" className={className} onClick={onClick}/>
  2826. } else {
  2827. return <IconPlus size="small" className={className} onClick={onClick}/>
  2828. }
  2829. });
  2830. return (
  2831. <>
  2832. <p>expandIcon 是 ReactNode</p>
  2833. <TreeSelect
  2834. style={{ width: 300}}
  2835. expandIcon={<IconChevronDown size="small" className='testCls'/>}
  2836. multiple
  2837. defaultExpandedKeys={['yazhou']}
  2838. treeData={treeData2}
  2839. />
  2840. <br />
  2841. <br />
  2842. <p>expandIcon 是函数</p>
  2843. <TreeSelect
  2844. style={{ width: 300}}
  2845. multiple
  2846. expandIcon={expandIconFunc}
  2847. defaultExpandedKeys={['yazhou']}
  2848. treeData={treeData2}
  2849. />
  2850. </>
  2851. );
  2852. }