treeSelect.stories.jsx 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. import React, { useState, useMemo, useRef, useCallback, useEffect } from 'react';
  2. import { Icon, Input, Button, Form, Popover, Tag, Typography, CheckboxGroup, TagInput, Switch, Tree } from '../../index';
  3. import TreeSelect from '../index';
  4. import { flattenDeep, cloneDeep } from 'lodash';
  5. import CustomTrigger from './CustomTrigger';
  6. import { IconCreditCard, IconChevronDown, IconClose } from '@douyinfe/semi-icons';
  7. import { setFocusToPreviousMenuItem } from '@douyinfe/semi-foundation/utils/a11y';
  8. import { node } from 'prop-types';
  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. <>
  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. </>
  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. </div>
  648. );
  649. Multiple.story = {
  650. name: 'multiple',
  651. };
  652. export const MaxTagCount = () => (
  653. <div>
  654. <TreeSelect
  655. style={{ width: 300 }}
  656. // value={this.state.value}
  657. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  658. treeData={treeData2}
  659. multiple
  660. maxTagCount={2}
  661. placeholder="Please select"
  662. />
  663. {/* <br />
  664. <br />
  665. <TreeSelect
  666. style={{ width: 300 }}
  667. // value={this.state.value}
  668. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  669. treeData={treeData2}
  670. multiple
  671. placeholder="Please select"
  672. /> */}
  673. </div>
  674. );
  675. MaxTagCount.story = {
  676. name: 'maxTagCount',
  677. };
  678. export const MultipleSearchable = () => (
  679. <div>
  680. <TreeSelect
  681. style={{ width: 300 }}
  682. // value={this.state.value}
  683. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  684. treeData={treeData2}
  685. multiple
  686. filterTreeNode
  687. treeNodeFilterProp="value"
  688. placeholder="Please select"
  689. />
  690. </div>
  691. );
  692. MultipleSearchable.story = {
  693. name: 'multiple searchable',
  694. };
  695. export const DefaultValues = () => (
  696. <div>
  697. <TreeSelect
  698. style={{ width: 300 }}
  699. defaultValue={'shanghai'}
  700. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  701. treeData={treeData2}
  702. // multiple
  703. filterTreeNode
  704. treeNodeFilterProp="value"
  705. />
  706. <br />
  707. <br />
  708. <TreeSelect
  709. style={{ width: 300 }}
  710. defaultValue={'shanghai'}
  711. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  712. treeData={treeData2}
  713. filterTreeNode
  714. multiple
  715. treeNodeFilterProp="value"
  716. />
  717. <br />
  718. <br />
  719. <TreeSelect
  720. style={{ width: 300 }}
  721. defaultValue={['shanghai', 'daban', 'dongjing']}
  722. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  723. treeData={treeData2}
  724. filterTreeNode
  725. multiple
  726. treeNodeFilterProp="value"
  727. />
  728. <br />
  729. <br />
  730. <TreeSelect
  731. style={{ width: 300 }}
  732. defaultValue={['meiguo', 'dongjing']}
  733. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  734. treeData={treeData2}
  735. filterTreeNode
  736. multiple
  737. treeNodeFilterProp="value"
  738. />
  739. </div>
  740. );
  741. DefaultValues.story = {
  742. name: 'default values',
  743. };
  744. export const Disabled = () => (
  745. <div>
  746. <TreeSelect
  747. style={{ width: 300 }}
  748. defaultValue={'shanghai'}
  749. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  750. treeData={treeData2}
  751. multiple
  752. filterTreeNode
  753. disabled
  754. treeNodeFilterProp="value"
  755. placeholder="Please select"
  756. />
  757. <br />
  758. <br />
  759. <TreeSelect
  760. style={{ width: 300 }}
  761. defaultValue={'shanghai'}
  762. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  763. treeData={treeData2}
  764. filterTreeNode
  765. disabled
  766. treeNodeFilterProp="value"
  767. placeholder="Please select"
  768. />
  769. <br />
  770. <br />
  771. <TreeSelect
  772. style={{ width: 300 }}
  773. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  774. treeData={treeData2}
  775. multiple
  776. filterTreeNode
  777. disabled
  778. treeNodeFilterProp="value"
  779. placeholder="Please select"
  780. />
  781. </div>
  782. );
  783. Disabled.story = {
  784. name: 'disabled',
  785. };
  786. Disabled.parameters = {
  787. chromatic: { disableSnapshot: false },
  788. }
  789. export const OptionLabelProp = () => (
  790. <>
  791. <TreeSelect
  792. style={{ width: 300 }}
  793. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  794. treeData={treeData2}
  795. multiple
  796. filterTreeNode
  797. treeNodeFilterProp="value"
  798. treeNodeLabelProp="value"
  799. placeholder="Please select"
  800. />
  801. </>
  802. );
  803. OptionLabelProp.story = {
  804. name: 'optionLabelProp',
  805. };
  806. export const ValueInArray = () => (
  807. <>
  808. <TreeSelect
  809. style={{ width: 300 }}
  810. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  811. treeData={treeData2}
  812. multiple
  813. filterTreeNode
  814. valueInArray
  815. onChange={(...args) => console.log(args)}
  816. treeNodeFilterProp="value"
  817. treeNodeLabelProp="value"
  818. placeholder="Please select"
  819. />
  820. </>
  821. );
  822. ValueInArray.story = {
  823. name: 'valueInArray',
  824. };
  825. export const OnBlurOnFocus = () => (
  826. <>
  827. <div>single</div>
  828. <TreeSelect
  829. style={{ width: 300 }}
  830. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  831. treeData={treeData2}
  832. onBlur={(...args) => console.log('blur', args)}
  833. onFocus={(...args) => console.log('focus', args)}
  834. placeholder="Please select"
  835. />
  836. <div>multiple</div>
  837. <TreeSelect
  838. style={{ width: 300 }}
  839. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  840. treeData={treeData2}
  841. multiple
  842. onBlur={(...args) => console.log('blur', args)}
  843. onFocus={(...args) => console.log('focus', args)}
  844. placeholder="Please select"
  845. />
  846. <div>single, filterTreeNode, searchPosition=dropdown</div>
  847. <TreeSelect
  848. filterTreeNode
  849. style={{ width: 300 }}
  850. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  851. treeData={treeData2}
  852. onBlur={(...args) => console.log('blur', args)}
  853. onFocus={(...args) => console.log('focus', args)}
  854. placeholder="Please select"
  855. />
  856. <div>multiple, filterTreeNode, searchPosition=dropdown</div>
  857. <TreeSelect
  858. filterTreeNode
  859. style={{ width: 300 }}
  860. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  861. treeData={treeData2}
  862. multiple
  863. onBlur={(...args) => console.log('blur', args)}
  864. onFocus={(...args) => console.log('focus', args)}
  865. placeholder="Please select"
  866. />
  867. <div>single, filterTreeNode, searchPosition=trigger</div>
  868. <TreeSelect
  869. searchPosition="trigger"
  870. filterTreeNode
  871. style={{ width: 300 }}
  872. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  873. treeData={treeData2}
  874. onBlur={(...args) => console.log('blur', args)}
  875. onFocus={(...args) => console.log('focus', args)}
  876. placeholder="Please select"
  877. />
  878. <div>multiple, filterTreeNode, searchPosition=trigger</div>
  879. <TreeSelect
  880. searchPosition="trigger"
  881. filterTreeNode
  882. style={{ width: 300 }}
  883. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  884. treeData={treeData2}
  885. multiple
  886. onBlur={(...args) => console.log('blur', args)}
  887. onFocus={(...args) => console.log('focus', args)}
  888. placeholder="Please select"
  889. />
  890. </>
  891. );
  892. OnBlurOnFocus.story = {
  893. name: 'onBlur/onFocus',
  894. };
  895. export const LeafOnly = () => (
  896. <div>
  897. <TreeSelect
  898. style={{ width: 300 }}
  899. // value={this.state.value}
  900. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  901. treeData={treeData2}
  902. multiple
  903. leafOnly
  904. placeholder="Please select"
  905. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  906. onSelect={(e, bool) => console.log('select', e, bool)}
  907. onChange={e => console.log('change', e)}
  908. />
  909. </div>
  910. );
  911. LeafOnly.story = {
  912. name: 'leafOnly',
  913. };
  914. class Demo extends React.Component {
  915. constructor() {
  916. super();
  917. this.state = {
  918. value: 'shanghai',
  919. };
  920. }
  921. onChange(value) {
  922. this.setState({ value });
  923. }
  924. render() {
  925. return (
  926. <TreeSelect
  927. style={{ width: 300 }}
  928. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  929. treeData={treeData2}
  930. value={this.state.value}
  931. placeholder="Please select"
  932. onChange={e => this.onChange(e)}
  933. />
  934. );
  935. }
  936. }
  937. export const ControlledComponentSingle = () => <Demo />;
  938. ControlledComponentSingle.story = {
  939. name: 'controlled Component single',
  940. };
  941. class Demo2 extends React.Component {
  942. constructor() {
  943. super();
  944. this.state = {
  945. value: ['Shanghai'],
  946. };
  947. }
  948. onChange(value) {
  949. this.setState({ value });
  950. }
  951. render() {
  952. console.log(this.state.value);
  953. const treeData = [
  954. {
  955. label: '亚洲',
  956. value: 'Asia',
  957. key: '0',
  958. children: [
  959. {
  960. label: '中国',
  961. value: 'China',
  962. key: '0-0',
  963. children: [
  964. {
  965. label: '北京',
  966. value: 'Beijing',
  967. key: '0-0-0',
  968. },
  969. {
  970. label: '上海',
  971. value: 'Shanghai',
  972. key: '0-0-1',
  973. },
  974. ],
  975. },
  976. ],
  977. },
  978. {
  979. label: '北美洲',
  980. value: 'North America',
  981. key: '1',
  982. },
  983. ];
  984. return (
  985. <TreeSelect
  986. style={{ width: 300 }}
  987. multiple
  988. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  989. treeData={treeData}
  990. value={this.state.value}
  991. placeholder="Please select"
  992. onChange={e => this.onChange(e)}
  993. />
  994. );
  995. }
  996. }
  997. export const ControlledComponentMultiple = () => <Demo2 />;
  998. ControlledComponentMultiple.story = {
  999. name: 'controlled Component multiple',
  1000. };
  1001. class Demo3 extends React.Component {
  1002. constructor() {
  1003. super();
  1004. this.state = {
  1005. value: ['Shanghai'],
  1006. };
  1007. }
  1008. // 获取最底层值
  1009. getDeepChildrensByNode = node => {
  1010. return flattenDeep(
  1011. node.map(item => {
  1012. if (item.children) {
  1013. return this.getDeepChildrensByNode(item.children);
  1014. }
  1015. return item.value;
  1016. })
  1017. );
  1018. };
  1019. onChange(value, node) {
  1020. console.log('onchange', value);
  1021. value = this.getDeepChildrensByNode(node);
  1022. console.log('modifiled', value);
  1023. this.setState({ value });
  1024. }
  1025. render() {
  1026. const treeData = [
  1027. {
  1028. label: '亚洲',
  1029. value: 'Asia',
  1030. key: '0',
  1031. children: [
  1032. {
  1033. label: '中国',
  1034. value: 'China',
  1035. key: '0-0',
  1036. children: [
  1037. {
  1038. label: '北京',
  1039. value: 'Beijing',
  1040. key: '0-0-0',
  1041. },
  1042. {
  1043. label: '上海',
  1044. value: 'Shanghai',
  1045. key: '0-0-1',
  1046. },
  1047. ],
  1048. },
  1049. ],
  1050. },
  1051. {
  1052. label: '北美洲',
  1053. value: 'North America',
  1054. key: '1',
  1055. },
  1056. ];
  1057. return (
  1058. <TreeSelect
  1059. style={{ width: 300 }}
  1060. multiple
  1061. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  1062. treeData={treeData}
  1063. value={this.state.value}
  1064. placeholder="Please select"
  1065. onChange={(e, node) => this.onChange(e, node)}
  1066. />
  1067. );
  1068. }
  1069. }
  1070. export const ControlledComponentMultipleValueModified = () => <Demo3 />;
  1071. ControlledComponentMultipleValueModified.story = {
  1072. name: 'controlled Component multiple value modified',
  1073. };
  1074. class ConvertDemo extends React.Component {
  1075. constructor(props) {
  1076. super(props);
  1077. this.formApi = null;
  1078. }
  1079. handleChange = val => {
  1080. let finalVal = val;
  1081. let firstClassOption = ['Asia', 'North America'];
  1082. // 在这里去做你的value替换逻辑
  1083. console.log('originVal:' + val);
  1084. if (val.length === 1) {
  1085. // do nothing
  1086. } else {
  1087. if (val.every(item => firstClassOption.includes(item))) {
  1088. finalVal = val[val.length - 1];
  1089. }
  1090. }
  1091. console.log('finalVal:' + finalVal);
  1092. return finalVal;
  1093. };
  1094. render() {
  1095. const treeData = [
  1096. {
  1097. label: '亚洲',
  1098. value: 'Asia',
  1099. key: '0',
  1100. children: [
  1101. {
  1102. label: '中国',
  1103. value: 'China',
  1104. key: '0-0',
  1105. children: [
  1106. {
  1107. label: '北京',
  1108. value: 'Beijing',
  1109. key: '0-0-0',
  1110. },
  1111. {
  1112. label: '上海',
  1113. value: 'Shanghai',
  1114. key: '0-0-1',
  1115. },
  1116. ],
  1117. },
  1118. ],
  1119. },
  1120. {
  1121. label: '北美洲',
  1122. value: 'North America',
  1123. key: '1',
  1124. },
  1125. ];
  1126. return (
  1127. <Form getFormApi={this.getFormApi}>
  1128. <Form.TreeSelect
  1129. field="tree"
  1130. label="节点(TreeSelect)"
  1131. placeholder="请选择服务节点"
  1132. treeData={treeData}
  1133. convert={this.handleChange}
  1134. filterTreeNode
  1135. multiple
  1136. ></Form.TreeSelect>
  1137. </Form>
  1138. );
  1139. }
  1140. }
  1141. export const _ConvertDemo = () => <ConvertDemo />;
  1142. _ConvertDemo.story = {
  1143. name: 'convert demo',
  1144. };
  1145. export const TreeselectDefaultOpenInPopover = () => (
  1146. <Popover
  1147. content={
  1148. <TreeSelect
  1149. style={{ width: 200 }}
  1150. dropdownStyle={{ width: 200 }}
  1151. treeData={treeData1}
  1152. defaultOpen
  1153. // value={this.state.value}
  1154. // placeholder="Please select"
  1155. // onChange={(e, node) => this.onChange(e, node)}
  1156. />
  1157. }
  1158. >
  1159. <Button style={{ marginLeft: 150 }}>悬停此处</Button>
  1160. </Popover>
  1161. );
  1162. TreeselectDefaultOpenInPopover.story = {
  1163. name: 'treeselect defaultOpen in popover',
  1164. };
  1165. export const CustomTriggerDemo = () => <CustomTrigger />;
  1166. CustomTriggerDemo.story = { name: 'custom trigger' };
  1167. const AutoParentDemo = () => {
  1168. const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
  1169. const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
  1170. const [autoExpandParent, setAutoExpandParent] = useState(true);
  1171. const onExpand = expandedKeys => {
  1172. console.log('onExpand', expandedKeys);
  1173. // if not set autoExpandParent to false, if children expanded, parent can not collapse.
  1174. // or, you can remove all expanded children keys.
  1175. setExpandedKeys(expandedKeys);
  1176. setAutoExpandParent(false);
  1177. };
  1178. const onSelect = (selectedKeys, info) => {
  1179. console.log('onSelect:', info);
  1180. setSelectedKeys(selectedKeys);
  1181. };
  1182. return (
  1183. <div>
  1184. <Button
  1185. onClick={() => {
  1186. setSelectedKeys(['beijing']);
  1187. setExpandedKeys(['beijing']);
  1188. setAutoExpandParent(true);
  1189. }}
  1190. >
  1191. Update
  1192. </Button>
  1193. <TreeSelect
  1194. onExpand={onExpand}
  1195. expandedKeys={expandedKeys}
  1196. autoExpandParent={autoExpandParent}
  1197. onChange={onSelect}
  1198. value={selectedKeys}
  1199. treeData={treeData2}
  1200. // multiple
  1201. />
  1202. </div>
  1203. );
  1204. };
  1205. export const AutoExpandParent = () => (
  1206. <>
  1207. <AutoParentDemo />
  1208. </>
  1209. );
  1210. AutoExpandParent.story = {
  1211. name: 'autoExpandParent',
  1212. };
  1213. export const TreeWithoutValueProps = () => (
  1214. <TreeSelect
  1215. treeData={treeDataWithoutValue}
  1216. value="beijing"
  1217. defaultExpandAll
  1218. onChange={(...args) => console.log(args)}
  1219. />
  1220. );
  1221. TreeWithoutValueProps.story = {
  1222. name: 'tree without value props',
  1223. };
  1224. export const TreeSelectRenderSelectedItem = () => {
  1225. const treeData = [
  1226. {
  1227. label: '亚洲',
  1228. value: 'Asia',
  1229. key: '0',
  1230. children: [
  1231. {
  1232. label: '中国',
  1233. value: 'China',
  1234. key: '0-0',
  1235. children: [
  1236. {
  1237. label: '北京',
  1238. value: 'Beijing',
  1239. key: '0-0-0',
  1240. },
  1241. {
  1242. label: '上海',
  1243. value: 'Shanghai',
  1244. key: '0-0-1',
  1245. },
  1246. ],
  1247. },
  1248. ],
  1249. },
  1250. {
  1251. label: '北美洲',
  1252. value: 'North America',
  1253. key: '1',
  1254. },
  1255. {
  1256. label: '南美洲',
  1257. value: 'South America',
  1258. key: '2',
  1259. },
  1260. {
  1261. label: '南极洲',
  1262. value: 'Antarctica',
  1263. key: '3',
  1264. },
  1265. ];
  1266. return (
  1267. <>
  1268. <h4>单选</h4>
  1269. <TreeSelect
  1270. style={{ width: 300 }}
  1271. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1272. treeData={treeData}
  1273. placeholder="请选择"
  1274. renderSelectedItem={item => `${item.value}-${item.label}`}
  1275. />
  1276. <h4>多选</h4>
  1277. <TreeSelect
  1278. style={{ width: 300 }}
  1279. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1280. treeData={treeData}
  1281. multiple
  1282. placeholder="请选择"
  1283. renderSelectedItem={(item, { index, onClose }) => ({
  1284. content: `${item.value}-${item.label}`,
  1285. isRenderInTag: true,
  1286. })}
  1287. />
  1288. <h4>多选 + isRenderInTag=false</h4>
  1289. <TreeSelect
  1290. style={{ width: 300 }}
  1291. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1292. treeData={treeData}
  1293. multiple
  1294. maxTagCount={2}
  1295. placeholder="请选择"
  1296. renderSelectedItem={(item, { index, onClose }) => ({
  1297. content: (
  1298. <Tag key={index} color="white">
  1299. {item.value}
  1300. </Tag>
  1301. ),
  1302. isRenderInTag: false,
  1303. })}
  1304. />
  1305. </>
  1306. );
  1307. };
  1308. TreeSelectRenderSelectedItem.story = {
  1309. name: 'treeSelect renderSelectedItem',
  1310. };
  1311. const DisableStrictlyDemo = () => {
  1312. const [value, setValue] = useState(['Shanghai']);
  1313. const treeData = [
  1314. {
  1315. label: '亚洲',
  1316. value: 'Asia',
  1317. key: '0',
  1318. children: [
  1319. {
  1320. label: '中国',
  1321. value: 'China',
  1322. key: '0-0',
  1323. disabled: true,
  1324. children: [
  1325. {
  1326. label: '北京',
  1327. value: 'Beijing',
  1328. key: '0-0-0',
  1329. },
  1330. {
  1331. label: '上海',
  1332. value: 'Shanghai',
  1333. key: '0-0-1',
  1334. },
  1335. ],
  1336. },
  1337. {
  1338. label: '日本',
  1339. value: 'Japan',
  1340. key: '0-1',
  1341. },
  1342. ],
  1343. },
  1344. {
  1345. label: '北美洲',
  1346. value: 'North America',
  1347. key: '1',
  1348. },
  1349. ];
  1350. return (
  1351. <div>
  1352. <TreeSelect
  1353. style={{ width: 300 }}
  1354. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1355. treeData={treeData}
  1356. disableStrictly
  1357. multiple
  1358. searchPosition="trigger"
  1359. filterTreeNode
  1360. value={value}
  1361. onChange={value => setValue(value)}
  1362. />
  1363. </div>
  1364. );
  1365. };
  1366. export const DisabledStrictly = () => (
  1367. <>
  1368. <DisableStrictlyDemo />
  1369. </>
  1370. );
  1371. DisabledStrictly.story = {
  1372. name: 'disabledStrictly',
  1373. };
  1374. export const CheckRelationDemo = () => {
  1375. const treeData = treeDataEn;
  1376. const [value, setValue] = useState('China');
  1377. const [value2, setValue2] = useState();
  1378. const [value3, setValue3] = useState();
  1379. const style = {
  1380. width: 300,
  1381. };
  1382. const dropdownStyle = {
  1383. maxHeight: 400,
  1384. overflow: 'auto'
  1385. };
  1386. const handleChange = value => {
  1387. console.log(value);
  1388. setValue(value);
  1389. };
  1390. const handleChange2 = value => {
  1391. console.log(value);
  1392. setValue2(value);
  1393. };
  1394. const handleChange3 = value => {
  1395. console.log(value);
  1396. setValue3(value);
  1397. };
  1398. return (
  1399. <>
  1400. <div>checkRelation='unRelated'</div>
  1401. <TreeSelect
  1402. dropdownStyle={dropdownStyle}
  1403. treeData={treeData}
  1404. multiple
  1405. checkRelation='unRelated'
  1406. defaultExpandAll
  1407. style={style}
  1408. />
  1409. <br /><br />
  1410. <div>checkRelation='unRelated' + maxTagCount=2</div>
  1411. <TreeSelect
  1412. dropdownStyle={dropdownStyle}
  1413. treeData={treeData}
  1414. multiple
  1415. maxTagCount={2}
  1416. checkRelation='unRelated'
  1417. defaultExpandAll
  1418. style={style}
  1419. />
  1420. <br /><br />
  1421. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索</div>
  1422. <TreeSelect
  1423. dropdownStyle={dropdownStyle}
  1424. treeData={treeData}
  1425. multiple
  1426. maxTagCount={2}
  1427. filterTreeNode
  1428. checkRelation='unRelated'
  1429. defaultExpandAll
  1430. style={style}
  1431. />
  1432. <br /><br />
  1433. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索 + searchBox in trigger</div>
  1434. <TreeSelect
  1435. dropdownStyle={dropdownStyle}
  1436. treeData={treeData}
  1437. multiple
  1438. maxTagCount={2}
  1439. filterTreeNode
  1440. checkRelation='unRelated'
  1441. searchPosition='trigger'
  1442. defaultExpandAll
  1443. style={style}
  1444. />
  1445. <br /><br />
  1446. <div>checkRelation='unRelated' + 中国节点为 disabled</div>
  1447. <TreeSelect
  1448. dropdownStyle={dropdownStyle}
  1449. treeData={treeDataWithoutValue}
  1450. multiple
  1451. checkRelation='unRelated'
  1452. defaultExpandAll
  1453. style={style}
  1454. />
  1455. <br /><br />
  1456. <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
  1457. <TreeSelect
  1458. dropdownStyle={dropdownStyle}
  1459. treeData={treeDataWithoutValue}
  1460. multiple
  1461. checkRelation='unRelated'
  1462. defaultExpandAll
  1463. disableStrictly
  1464. style={style}
  1465. />
  1466. <br /><br />
  1467. <div>checkRelation='unRelated' + defaultValue 为 China</div>
  1468. <TreeSelect
  1469. dropdownStyle={dropdownStyle}
  1470. treeData={treeData}
  1471. multiple
  1472. checkRelation='unRelated'
  1473. defaultExpandAll
  1474. style={style}
  1475. defaultValue='China'
  1476. />
  1477. <br /><br />
  1478. <div>checkRelation='unRelated' + defaultValue 为 China + 开启搜索</div>
  1479. <TreeSelect
  1480. dropdownStyle={dropdownStyle}
  1481. treeData={treeData}
  1482. multiple
  1483. filterTreeNode
  1484. checkRelation='unRelated'
  1485. defaultExpandAll
  1486. style={style}
  1487. defaultValue='China'
  1488. />
  1489. <br /><br />
  1490. <div>多选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1491. <TreeSelect
  1492. dropdownStyle={dropdownStyle}
  1493. treeData={treeData}
  1494. multiple
  1495. filterTreeNode
  1496. showClear
  1497. checkRelation='unRelated'
  1498. defaultExpandAll
  1499. style={style}
  1500. searchPosition='trigger'
  1501. defaultValue={['China', 'Japan']}
  1502. />
  1503. <br /><br />
  1504. <div>单选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1505. <TreeSelect
  1506. dropdownStyle={dropdownStyle}
  1507. treeData={treeData}
  1508. filterTreeNode
  1509. showClear
  1510. checkRelation='unRelated'
  1511. defaultExpandAll
  1512. style={style}
  1513. searchPosition='trigger'
  1514. defaultValue='China'
  1515. />
  1516. <br /><br />
  1517. <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
  1518. <TreeSelect
  1519. dropdownStyle={dropdownStyle}
  1520. treeData={treeData}
  1521. multiple
  1522. checkRelation='unRelated'
  1523. defaultExpandAll
  1524. style={style}
  1525. value={value}
  1526. onChange={handleChange}
  1527. />
  1528. <br /><br />
  1529. <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
  1530. <TreeSelect
  1531. dropdownStyle={dropdownStyle}
  1532. treeData={treeData}
  1533. multiple
  1534. checkRelation='unRelated'
  1535. defaultExpandAll
  1536. style={style}
  1537. value={value2}
  1538. onChangeWithObject
  1539. onChange={handleChange2}
  1540. />
  1541. <br /><br />
  1542. <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
  1543. <TreeSelect
  1544. dropdownStyle={dropdownStyle}
  1545. leafOnly
  1546. treeData={treeData}
  1547. multiple
  1548. checkRelation='unRelated'
  1549. defaultExpandAll
  1550. style={style}
  1551. value={value3}
  1552. onChange={handleChange3}
  1553. />
  1554. <br /><br />
  1555. <div>checkRelation='unRelated' + onSelect </div>
  1556. <TreeSelect
  1557. dropdownStyle={dropdownStyle}
  1558. treeData={treeData}
  1559. multiple
  1560. checkRelation='unRelated'
  1561. defaultExpandAll
  1562. style={style}
  1563. onSelect={(value,status,node)=>console.log('select', value, status, node)}
  1564. />
  1565. </>
  1566. );
  1567. };
  1568. export const SearchableAndExpandedKeys = () => {
  1569. const [expandedKeys1, setExpandedKeys1] = useState([]);
  1570. const [expandedKeys2, setExpandedKeys2] = useState([]);
  1571. const [expandedKeys3, setExpandedKeys3] = useState([]);
  1572. return (
  1573. <>
  1574. <Title heading={6}>expandedKeys 受控</Title>
  1575. <TreeSelect
  1576. style={{ width: 300, marginBottom: 30 }}
  1577. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1578. treeData={treeData2}
  1579. expandedKeys={expandedKeys1}
  1580. defaultValue='beijing'
  1581. onExpand={v => {
  1582. console.log('onExpand value: ', v);
  1583. setExpandedKeys1(v);
  1584. }}
  1585. />
  1586. <Title heading={6}>expandedKeys 受控 + 开启搜索</Title>
  1587. <TreeSelect
  1588. style={{ width: 300, marginBottom: 30 }}
  1589. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1590. treeData={treeData2}
  1591. filterTreeNode
  1592. defaultValue='beijing'
  1593. expandedKeys={expandedKeys2}
  1594. onExpand={v => {
  1595. console.log('onExpand value: ', v);
  1596. setExpandedKeys2(v);
  1597. }}
  1598. />
  1599. <Title heading={6}>expandedKeys 受控 + 开启搜索 + 搜索时更新 expandedKeys</Title>
  1600. <TreeSelect
  1601. style={{ width: 300, marginBottom: 30 }}
  1602. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1603. treeData={treeData2}
  1604. filterTreeNode
  1605. expandedKeys={expandedKeys3}
  1606. defaultValue='beijing'
  1607. onExpand={v => {
  1608. console.log('onExpand value: ', v);
  1609. setExpandedKeys3(v)
  1610. }}
  1611. onSearch={(input, filterExpandedKeys) => {
  1612. console.log('onExpand filterExpandedKeys: ', filterExpandedKeys);
  1613. setExpandedKeys3(filterExpandedKeys);
  1614. }}
  1615. />
  1616. </>
  1617. )
  1618. }
  1619. export const loadData = () => {
  1620. const initialData = [
  1621. {
  1622. label: 'Expand to load',
  1623. value: '0',
  1624. key: '0',
  1625. },
  1626. {
  1627. label: 'Expand to load',
  1628. value: '1',
  1629. key: '1',
  1630. },
  1631. {
  1632. label: 'Leaf Node',
  1633. value: '2',
  1634. key: '2',
  1635. isLeaf: true,
  1636. },
  1637. ];
  1638. const [treeData, setTreeData] = useState(initialData);
  1639. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1640. function updateTreeData(list, key, children) {
  1641. return list.map(node => {
  1642. if (node.key === key) {
  1643. return { ...node, children };
  1644. }
  1645. if (node.children) {
  1646. return { ...node, children: updateTreeData(node.children, key, children) };
  1647. }
  1648. return node;
  1649. });
  1650. }
  1651. function onLoadData({ key, children }) {
  1652. return new Promise(resolve => {
  1653. if (children) {
  1654. resolve();
  1655. return;
  1656. }
  1657. setTimeout(() => {
  1658. setTreeData(origin =>
  1659. updateTreeData(origin, key, [
  1660. {
  1661. label: 'Child Node',
  1662. key: `${key}-0`,
  1663. },
  1664. {
  1665. label: 'Child Node',
  1666. key: `${key}-1`,
  1667. },
  1668. ]),
  1669. );
  1670. resolve();
  1671. }, 1000);
  1672. });
  1673. }
  1674. return (
  1675. <TreeSelect
  1676. loadData={onLoadData}
  1677. filterTreeNode
  1678. treeData={treeData}
  1679. style={{ width: 300 }}
  1680. placeholder="请选择"
  1681. />
  1682. );
  1683. }
  1684. export const loadDataAndLoadedkeys = () => {
  1685. const initialData = [
  1686. {
  1687. label: 'Expand to load',
  1688. value: '0',
  1689. key: '0',
  1690. },
  1691. {
  1692. label: 'Expand to load',
  1693. value: '1',
  1694. key: '1',
  1695. },
  1696. {
  1697. label: 'Leaf Node',
  1698. value: '2',
  1699. key: '2',
  1700. isLeaf: true,
  1701. },
  1702. ];
  1703. const [treeData, setTreeData] = useState(initialData);
  1704. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1705. function updateTreeData(list, key, children) {
  1706. return list.map(node => {
  1707. if (node.key === key) {
  1708. return { ...node, children };
  1709. }
  1710. if (node.children) {
  1711. return { ...node, children: updateTreeData(node.children, key, children) };
  1712. }
  1713. return node;
  1714. });
  1715. }
  1716. function updateLoadedKeys(key) {
  1717. if(!loadedKeys.includes(key)){
  1718. setLoadedKeys([...loadedKeys, key]);
  1719. console.log('[...loadedKeys, key]', [...loadedKeys, key]);
  1720. }
  1721. }
  1722. function onLoadData({ key, children }) {
  1723. return new Promise(resolve => {
  1724. if (children) {
  1725. resolve();
  1726. return;
  1727. }
  1728. setTimeout(() => {
  1729. setTreeData(origin =>
  1730. updateTreeData(origin, key, [
  1731. {
  1732. label: 'Child Node',
  1733. key: `${key}-0`,
  1734. },
  1735. {
  1736. label: 'Child Node',
  1737. key: `${key}-1`,
  1738. },
  1739. ]),
  1740. );
  1741. // updateLoadedKeys(key);
  1742. resolve();
  1743. }, 1000);
  1744. });
  1745. }
  1746. return (
  1747. <TreeSelect
  1748. loadData={onLoadData}
  1749. filterTreeNode
  1750. // loadedKeys={loadedKeys}
  1751. treeData={treeData}
  1752. style={{ width: 300 }}
  1753. placeholder="请选择"
  1754. />
  1755. );
  1756. }
  1757. export const size = () => {
  1758. const props = {
  1759. style: { width: 300 },
  1760. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  1761. defaultValue: ['0-0-1', '0-0-2', '0-0-3', '0-0-4', '0-0-5', '0-0-6', '0-0-7'],
  1762. treeData: treeData3,
  1763. multiple: true,
  1764. filterTreeNode: true,
  1765. searchPosition: "trigger"
  1766. };
  1767. return (<>
  1768. <TreeSelect {...props} size={'small'} placeholder={'small'} />
  1769. <br/><br/>
  1770. <TreeSelect {...props} size={'default'} placeholder={'default'} />
  1771. <br/><br/>
  1772. <TreeSelect {...props} size={'large'} placeholder={'large'} />
  1773. </>);
  1774. }
  1775. export const valueNotInTreeDataIssue = () => {
  1776. const treeData = [
  1777. {
  1778. key: "test",
  1779. label: "测试标签",
  1780. children: [
  1781. {
  1782. key: "test_2",
  1783. label: "测试二级标签"
  1784. },
  1785. {
  1786. key: "jzr_test",
  1787. label: "之睿测试"
  1788. }
  1789. ]
  1790. },
  1791. {
  1792. key: "create",
  1793. label: "创作构思",
  1794. children: [
  1795. {
  1796. key: "material",
  1797. label: "素材积累"
  1798. },
  1799. {
  1800. key: "lens_script",
  1801. label: "分镜脚本"
  1802. }
  1803. ]
  1804. }
  1805. ];
  1806. const treeDataWithValue = [
  1807. {
  1808. value: "test",
  1809. key: "0",
  1810. label: "测试标签",
  1811. children: [
  1812. {
  1813. value: "test_2",
  1814. key: "0-1",
  1815. label: "测试二级标签"
  1816. },
  1817. {
  1818. value: "jzr_test",
  1819. key: "0-2",
  1820. label: "之睿测试"
  1821. }
  1822. ]
  1823. },
  1824. {
  1825. value: "create",
  1826. key: "1",
  1827. label: "创作构思",
  1828. children: [
  1829. {
  1830. value: "material",
  1831. key: "1-1",
  1832. label: "素材积累"
  1833. },
  1834. {
  1835. value: "lens_script",
  1836. key: "1-2",
  1837. label: "分镜脚本"
  1838. }
  1839. ]
  1840. }
  1841. ];
  1842. const commonProps = useMemo(() => {
  1843. return {
  1844. multiple: true,
  1845. style: { width: 300 },
  1846. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  1847. onChange: (value) => {
  1848. console.log('onChange', value);
  1849. },
  1850. onSelect: (value) => {
  1851. console.log('onSelect', value);
  1852. },
  1853. };
  1854. }, []);
  1855. return (
  1856. <>
  1857. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,无 value</p>
  1858. <p>checkRelation='related'</p>
  1859. <TreeSelect
  1860. defaultExpandAll
  1861. defaultValue={["test_2", 0]}
  1862. treeData={treeData}
  1863. {...commonProps}
  1864. />
  1865. <p>checkRelation='unRelated'</p>
  1866. <TreeSelect
  1867. defaultExpandAll
  1868. defaultValue={["test_2", "fish"]}
  1869. checkRelation='unRelated'
  1870. treeData={treeData}
  1871. {...commonProps}
  1872. />
  1873. <p>onChangeWithObject, checkRelation='related'</p>
  1874. <TreeSelect
  1875. defaultExpandAll
  1876. onChangeWithObject
  1877. defaultValue={[
  1878. {
  1879. key: "test_2",
  1880. label: "测试二级标签"
  1881. },
  1882. {
  1883. key: "fish",
  1884. label: "鱼"
  1885. }
  1886. ]}
  1887. treeData={treeData}
  1888. {...commonProps}
  1889. />
  1890. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1891. <TreeSelect
  1892. defaultExpandAll
  1893. onChangeWithObject
  1894. defaultValue={[
  1895. {
  1896. key: "test_2",
  1897. label: "测试二级标签"
  1898. },
  1899. {
  1900. key: "fish",
  1901. label: "鱼"
  1902. }
  1903. ]}
  1904. treeData={treeData}
  1905. {...commonProps}
  1906. />
  1907. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,有 value</p>
  1908. <p>checkRelation='related'</p>
  1909. <TreeSelect
  1910. defaultExpandAll
  1911. defaultValue={["test", "fish"]}
  1912. treeData={treeDataWithValue}
  1913. {...commonProps}
  1914. />
  1915. <p>checkRelation='unRelated'</p>
  1916. <TreeSelect
  1917. defaultExpandAll
  1918. defaultValue={["test", "fish"]}
  1919. checkRelation='unRelated'
  1920. treeData={treeDataWithValue}
  1921. {...commonProps}
  1922. />
  1923. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1924. <TreeSelect
  1925. defaultExpandAll
  1926. onChangeWithObject
  1927. defaultValue={[
  1928. {
  1929. value: "test_2",
  1930. key: "0-1",
  1931. label: "测试二级标签"
  1932. },
  1933. {
  1934. key: "fish",
  1935. value: "Fish",
  1936. label: "鱼"
  1937. }
  1938. ]}
  1939. treeData={treeDataWithValue}
  1940. {...commonProps}
  1941. />
  1942. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1943. <TreeSelect
  1944. defaultExpandAll
  1945. onChangeWithObject
  1946. defaultValue={[
  1947. {
  1948. value: "test_2",
  1949. key: "0-1",
  1950. label: "测试二级标签"
  1951. },
  1952. {
  1953. key: "fish",
  1954. value: "Fish",
  1955. label: "鱼"
  1956. }
  1957. ]}
  1958. treeData={treeDataWithValue}
  1959. {...commonProps}
  1960. />
  1961. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,无 value</p>
  1962. <TreeSelect
  1963. defaultExpandAll
  1964. defaultValue={"fish"}
  1965. treeData={treeData}
  1966. {...commonProps}
  1967. multiple={false}
  1968. />
  1969. <p>onChangeWithObject</p>
  1970. <TreeSelect
  1971. defaultExpandAll
  1972. defaultValue={{
  1973. key: "fish",
  1974. value: "Fish",
  1975. label: "鱼"
  1976. }}
  1977. treeData={treeData}
  1978. {...commonProps}
  1979. multiple={false}
  1980. onChangeWithObject
  1981. />
  1982. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,有 value</p>
  1983. <TreeSelect
  1984. defaultExpandAll
  1985. defaultValue={"fish"}
  1986. treeData={treeDataWithValue}
  1987. {...commonProps}
  1988. multiple={false}
  1989. />
  1990. <p>onChangeWithObject</p>
  1991. <TreeSelect
  1992. defaultExpandAll
  1993. defaultValue={{
  1994. key: "fish",
  1995. value: "Fish",
  1996. label: "鱼"
  1997. }}
  1998. treeData={treeDataWithValue}
  1999. {...commonProps}
  2000. multiple={false}
  2001. onChangeWithObject
  2002. />
  2003. </>
  2004. );
  2005. };
  2006. class ValueTypeIsNumber extends React.Component {
  2007. constructor() {
  2008. super();
  2009. this.state = {
  2010. value: 1
  2011. };
  2012. }
  2013. onChange(value) {
  2014. console.log('onChange', value);
  2015. this.setState({ value });
  2016. }
  2017. render() {
  2018. const treeData = [
  2019. {
  2020. label: '北美洲',
  2021. value: 'North America',
  2022. key: '1',
  2023. },
  2024. {
  2025. label: '亚洲',
  2026. value: 'Asia',
  2027. key: '0',
  2028. children: [
  2029. {
  2030. label: '中国',
  2031. value: 'China',
  2032. key: '0-0',
  2033. children: [
  2034. {
  2035. label: '北京',
  2036. value: 'Beijing',
  2037. key: '0-0-0',
  2038. },
  2039. {
  2040. label: '上海',
  2041. value: 'Shanghai',
  2042. key: '0-0-1',
  2043. },
  2044. ],
  2045. },
  2046. ],
  2047. },
  2048. ];
  2049. return (
  2050. <TreeSelect
  2051. style={{ width: 300 }}
  2052. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2053. treeData={treeData}
  2054. value={this.state.value}
  2055. placeholder="请选择"
  2056. multiple
  2057. onChange={e => this.onChange(e)}
  2058. />
  2059. );
  2060. }
  2061. }
  2062. export const valueIsNumber = () => <ValueTypeIsNumber />
  2063. export const searchPositionInTriggerAndVirtualize = () => {
  2064. return (
  2065. <>
  2066. <TreeSelect
  2067. searchPosition="trigger"
  2068. style={{ width: 300 }}
  2069. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2070. treeData={treeData2}
  2071. filterTreeNode
  2072. placeholder="单选"
  2073. virtualize={{
  2074. itemSize: 28,
  2075. // dropDown height 300 minus search box height minus padding 8 * 2
  2076. // or if you set dropdown height, it will fill 100% of rest space
  2077. height: 236
  2078. }}
  2079. />
  2080. </>
  2081. );
  2082. };
  2083. export const clickTriggerToHide = () => (
  2084. <>
  2085. <p>clickTriggerToHide 未设置,默认为 true</p>
  2086. <TreeSelect
  2087. style={{ width: 300 }}
  2088. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2089. treeData={treeData2}
  2090. placeholder="单选"
  2091. />
  2092. <p>clickTriggerToHide 设置为 false</p>
  2093. <TreeSelect
  2094. style={{ width: 300 }}
  2095. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2096. treeData={treeData2}
  2097. placeholder="单选"
  2098. clickTriggerToHide={false}
  2099. />
  2100. </>
  2101. );
  2102. export const triggerRenderAddMethod = () => {
  2103. const treeData = useMemo(() => [
  2104. {
  2105. label: '亚洲',
  2106. value: '亚洲',
  2107. key: '0',
  2108. children: [
  2109. {
  2110. label: '中国',
  2111. value: '中国',
  2112. key: '0-0',
  2113. children: [
  2114. {
  2115. label: '北京',
  2116. value: '北京',
  2117. key: '0-0-0',
  2118. },
  2119. {
  2120. label: '上海',
  2121. value: '上海',
  2122. key: '0-0-1',
  2123. },
  2124. ],
  2125. },
  2126. ],
  2127. },
  2128. {
  2129. label: '北美洲',
  2130. value: '北美洲',
  2131. key: '1',
  2132. }
  2133. ], []);
  2134. const onValueChange = useCallback((value) => {
  2135. console.log('onChange', value);
  2136. });
  2137. const closeIcon = useCallback((value, onClear) => {
  2138. return value && value.length ? <IconClose onClick={onClear} /> : <IconChevronDown />;
  2139. }, []);
  2140. const renderTagItem = useCallback((item, onRemove) => (
  2141. <Tag closable key={item.key} onClose={() => { onRemove(item.key); }}>{item.label}</Tag>
  2142. ), []);
  2143. const renderTrigger1 = useCallback((props) => {
  2144. const { value, placeholder, onClear } = props;
  2145. return (
  2146. <Button theme={'light'} icon={closeIcon(value, onClear)} iconPosition={'right'}>
  2147. {value && value.length ? value.map(item => item.label).join(',') : placeholder}
  2148. </Button>
  2149. );
  2150. }, []);
  2151. const renderTrigger2 = useCallback((props) => {
  2152. const { value, onSearch, onRemove, onClear } = props;
  2153. return (
  2154. <div style={{ border: '1px solid grey', width: 'fit-content', padding: 5, borderRadius: 5 }}>
  2155. {value && value.length > 0 &&
  2156. <div style={{ width: 'fit-content', minWidth: 10, padding: 5 }}>
  2157. {value.map(item => renderTagItem(item, onRemove))}
  2158. </div>
  2159. }
  2160. <Input style={{ width: 200 }} onChange={onSearch} />
  2161. {closeIcon(value, onClear)}
  2162. </div>
  2163. );
  2164. }, []);
  2165. const renderTrigger3 = useCallback((props) => {
  2166. const { value, onSearch, onRemove, inputValue } = props;
  2167. const tagInputValue = value.map(item => item.key);
  2168. const renderTagInMultiple = (key) => {
  2169. const label = value.find(item => item.key === key).label;
  2170. const onCloseTag = (value, e, tagKey) => {
  2171. onRemove(tagKey);
  2172. }
  2173. return <Tag style={{ marginLeft: 2 }} tagKey={key} key={key} onClose={onCloseTag} closable>{label}</Tag>
  2174. }
  2175. return (
  2176. <TagInput
  2177. inputValue={inputValue}
  2178. value={tagInputValue}
  2179. onInputChange={onSearch}
  2180. renderTagItem={renderTagInMultiple}
  2181. />
  2182. )
  2183. }, []);
  2184. return (
  2185. <>
  2186. <TreeSelect
  2187. triggerRender={renderTrigger1}
  2188. multiple
  2189. treeData={treeData}
  2190. placeholder='Custom Trigger'
  2191. onChange={onValueChange}
  2192. style={{ width: 300 }}
  2193. />
  2194. <br />
  2195. <TreeSelect
  2196. triggerRender={renderTrigger2}
  2197. filterTreeNode
  2198. searchPosition="trigger"
  2199. multiple
  2200. treeData={treeData}
  2201. placeholder='Custom Trigger'
  2202. onChange={onValueChange}
  2203. style={{ width: 300 }}
  2204. />
  2205. <br />
  2206. <TreeSelect
  2207. triggerRender={renderTrigger3}
  2208. filterTreeNode
  2209. searchPosition="trigger"
  2210. multiple
  2211. treeData={treeData}
  2212. placeholder='Custom Trigger'
  2213. onChange={onValueChange}
  2214. style={{ width: 300 }}
  2215. />
  2216. </>
  2217. );
  2218. }
  2219. export const AutoSearchFocusPlusPreventScroll = () => {
  2220. return (
  2221. <div>
  2222. <div style={{height: '100vh' }}>我是一个高度和视窗高度一致的div。
  2223. <br />由于 TreeSelect 设置了 searchAutoFocus 以及 preventScroll,
  2224. <br /> 符合预期的情况是在没有滚动屏幕情况下,你不会看到 TreeSelect 的 trigger
  2225. </div>
  2226. <TreeSelect
  2227. searchAutoFocus
  2228. searchPosition="trigger"
  2229. preventScroll={true}
  2230. style={{ width: 300 }}
  2231. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2232. treeData={treeData2}
  2233. filterTreeNode
  2234. placeholder="单选"
  2235. />
  2236. </div>
  2237. );
  2238. };
  2239. export const LongLabel = () => {
  2240. const treeData = [
  2241. {
  2242. label: '这是一个超长的中文测试用标题这是一个超长的中文测试用标题这是一个超长的中文测试用标题这是一个超长的中文测试用标题',
  2243. value: 'v1',
  2244. key: '0',
  2245. },
  2246. {
  2247. label: 'ThisISAVeryLongTestSentenceThisISAVeryLongTestSentenceThisISAVeryLongTestSentence',
  2248. value: 'v2',
  2249. key: '1',
  2250. }
  2251. ];
  2252. return (
  2253. <>
  2254. <p>单选</p>
  2255. <TreeSelect
  2256. defaultValue='v1'
  2257. style={{ width: 300 }}
  2258. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2259. treeData={treeData}
  2260. placeholder="请选择"
  2261. />
  2262. <p>单选,可搜索, searchPosition='trigger'</p>
  2263. <TreeSelect
  2264. filterTreeNode
  2265. searchPosition='trigger'
  2266. defaultValue='v1'
  2267. style={{ width: 300 }}
  2268. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2269. treeData={treeData}
  2270. placeholder="请选择"
  2271. />
  2272. <p>单选,可搜索, searchPosition='dropDown'</p>
  2273. <TreeSelect
  2274. filterTreeNode
  2275. defaultValue='v1'
  2276. style={{ width: 300 }}
  2277. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2278. treeData={treeData}
  2279. placeholder="请选择"
  2280. />
  2281. <p>单选</p>
  2282. <TreeSelect
  2283. defaultValue='v2'
  2284. style={{ width: 300 }}
  2285. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2286. treeData={treeData}
  2287. placeholder="请选择"
  2288. />
  2289. <p>单选,可搜索, searchPosition='trigger'</p>
  2290. <TreeSelect
  2291. filterTreeNode
  2292. searchPosition='trigger'
  2293. defaultValue='v2'
  2294. style={{ width: 300 }}
  2295. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2296. treeData={treeData}
  2297. placeholder="请选择"
  2298. />
  2299. <p>单选,可搜索, searchPosition='dropDown'</p>
  2300. <TreeSelect
  2301. filterTreeNode
  2302. defaultValue='v2'
  2303. style={{ width: 300 }}
  2304. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2305. treeData={treeData}
  2306. placeholder="请选择"
  2307. />
  2308. </>
  2309. );
  2310. }
  2311. export const UnRelatedAndAsyncLoad = () => {
  2312. const initialData = [
  2313. {
  2314. label: 'Expand to load0',
  2315. value: '0',
  2316. key: '0',
  2317. },
  2318. {
  2319. label: 'Expand to load1',
  2320. value: '1',
  2321. key: '1',
  2322. },
  2323. {
  2324. label: 'Leaf Node',
  2325. value: '2',
  2326. key: '2',
  2327. isLeaf: true,
  2328. },
  2329. ];
  2330. const [treeData, setTreeData] = useState(initialData);
  2331. function updateTreeData(list, key, children) {
  2332. return list.map(node => {
  2333. if (node.key === key) {
  2334. return { ...node, children };
  2335. }
  2336. if (node.children) {
  2337. return { ...node, children: updateTreeData(node.children, key, children) };
  2338. }
  2339. return node;
  2340. });
  2341. }
  2342. function onLoadData({ key, children }) {
  2343. return new Promise(resolve => {
  2344. if (children) {
  2345. resolve();
  2346. return;
  2347. }
  2348. setTimeout(() => {
  2349. setTreeData(origin =>
  2350. updateTreeData(origin, key, [
  2351. {
  2352. label: `Child Node${key}-0`,
  2353. key: `${key}-0`,
  2354. },
  2355. {
  2356. label: `Child Node${key}-1`,
  2357. key: `${key}-1`,
  2358. },
  2359. ]),
  2360. );
  2361. resolve();
  2362. }, 1000);
  2363. });
  2364. }
  2365. return (
  2366. <>
  2367. <span>issue 1852: checkRelation='unRelated', 异步加载数据</span>
  2368. <TreeSelect
  2369. checkRelation='unRelated'
  2370. defaultValue={['0']}
  2371. multiple
  2372. defaultOpen
  2373. loadData={onLoadData}
  2374. treeData={[...treeData]}
  2375. />
  2376. </>
  2377. );
  2378. };
  2379. const constructLargeData = () => {
  2380. const newArray = (new Array(10)).fill(0).map((item, m) => {
  2381. const parent = {
  2382. key: `key-${m}`,
  2383. label: `node-${m}`,
  2384. children: []
  2385. }
  2386. new Array(100).fill(0).map((item, n) => {
  2387. const children = {
  2388. key: `key-${m}-${n}`,
  2389. label: `value-${m}-${n}`,
  2390. children: []
  2391. }
  2392. new Array(10).fill(0).map((item, o) => {
  2393. const grandChildren = {
  2394. key: `key-${m}-${n}-${o}`,
  2395. label: `value-${m}-${n}-${o}`,
  2396. }
  2397. children.children.push(grandChildren);
  2398. });
  2399. parent.children.push(children);
  2400. });
  2401. return parent;
  2402. });
  2403. return newArray;
  2404. }
  2405. export const ChangeTreeData = () => {
  2406. const [sign, setSign] = useState(true);
  2407. const treeData1 = useMemo(() => {
  2408. return constructLargeData();
  2409. }, []);
  2410. const treeData2 = useMemo(() => {
  2411. return constructLargeData();
  2412. }, []);
  2413. const onButtonClick = useCallback(() => {
  2414. setSign((sign) => {
  2415. return !sign;
  2416. })
  2417. }, []);
  2418. return <>
  2419. <Button onClick={onButtonClick}>点击修改TreeData</Button>
  2420. <br/><br/>
  2421. <TreeSelect
  2422. treeData={sign ? treeData1 : treeData2}
  2423. style={{ width: 300 }}
  2424. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2425. placeholder="请选择"
  2426. />
  2427. </>
  2428. }
  2429. export const KeyMaps = () => {
  2430. const [withObject, setWithObject] = useState(false);
  2431. const [value1, setValue1] = useState(undefined);
  2432. const [value2, setValue2] = useState(undefined);
  2433. const [expandKeys, setExpandedKeys] = useState(["yazhou", 'zhongguo']);
  2434. const switchChange = useCallback((checked) => {
  2435. setWithObject(checked);
  2436. setValue1(undefined);
  2437. setValue2(undefined);
  2438. }, []);
  2439. const onSingleChange = useCallback((value) => {
  2440. console.log('onSingleChange', value);
  2441. setValue1(value);
  2442. }, []);
  2443. const onMultipleChange = useCallback((value) => {
  2444. console.log('onMultipleChange', value);
  2445. setValue2(value);
  2446. }, []);
  2447. const normalChange = useCallback((value) => {
  2448. console.log('onChange', value);
  2449. }, []);
  2450. const normalExpand = useCallback((expandedKeys, {expanded, node}) => {
  2451. console.log('onExpanded', expandedKeys, expanded, cloneDeep(node));
  2452. }, []);
  2453. const keyMaps = useMemo(() => {
  2454. return {
  2455. // value: 'value1',
  2456. key: 'key1',
  2457. label: 'label1',
  2458. children: 'children1',
  2459. disabled: 'disabled1'
  2460. };
  2461. }, []);
  2462. const regularTreeProps = useMemo(() => ({
  2463. keyMaps: keyMaps,
  2464. treeData: specialTreeData,
  2465. style: { width: 300 },
  2466. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  2467. onChange: normalChange,
  2468. onExpand: normalExpand,
  2469. onChangeWithObject: withObject,
  2470. }), [withObject]);
  2471. const defaultSelectedObj = {
  2472. label1: '北京',
  2473. // value1: 'Beijing',
  2474. key1: 'beijing',
  2475. };
  2476. return (
  2477. <>
  2478. <span>onChangeWithObject</span><Switch checked={withObject} onChange={switchChange} />
  2479. <div key={String(withObject)} style={{ marginTop: 10 }}>
  2480. <div> Single select</div>
  2481. <TreeSelect
  2482. {...regularTreeProps}
  2483. defaultValue={withObject ? defaultSelectedObj : 'beijing'}
  2484. />
  2485. <div> Single select, onSearch, filterTreeNode, treeNodeFilterProp</div>
  2486. <TreeSelect
  2487. {...regularTreeProps}
  2488. filterTreeNode={(inputValue, treeNodeString, data)=> {
  2489. console.log("filterTreeNode", inputValue, treeNodeString, data);
  2490. return treeNodeString.includes(inputValue);
  2491. }}
  2492. treeNodeFilterProp={"key1"}
  2493. onSearch={(input, filteredExpandedKeys) => {
  2494. console.log('onSearch', input, filteredExpandedKeys);
  2495. }}
  2496. />
  2497. <div>Single select, controlled</div>
  2498. <TreeSelect
  2499. {...regularTreeProps}
  2500. value={value1}
  2501. onChange={onSingleChange}
  2502. />
  2503. <div> Multiple select</div>
  2504. <TreeSelect
  2505. {...regularTreeProps}
  2506. multiple
  2507. defaultValue={withObject ? [defaultSelectedObj] : ['beijing']}
  2508. />
  2509. <div> Multiple select, controlled</div>
  2510. <TreeSelect
  2511. {...regularTreeProps}
  2512. value={value2}
  2513. multiple
  2514. onChange={onMultipleChange}
  2515. />
  2516. <div> Multiple select, disableStrictly</div>
  2517. <TreeSelect
  2518. {...regularTreeProps}
  2519. multiple
  2520. disableStrictly
  2521. />
  2522. <div> Multiple, 展开受控</div>
  2523. <TreeSelect
  2524. {...regularTreeProps}
  2525. multiple
  2526. expandedKeys={expandKeys}
  2527. onExpand={(expandedKeys, {expanded, node}) => {
  2528. console.log('onExpanded', expandedKeys, expanded, cloneDeep(node));
  2529. setExpandedKeys(expandedKeys);
  2530. }}
  2531. />
  2532. </div>
  2533. </>
  2534. );
  2535. }
  2536. export const Issue1542 = () => {
  2537. const [expandedKeys, setExpandedKeys] = useState([]);
  2538. const treeData = treeDataEn;
  2539. const onExpand = useCallback((expandedKeys) => {
  2540. setExpandedKeys(expandedKeys);
  2541. }, [expandedKeys]);
  2542. const onSearch = useCallback((inputValue, filteredExpandedKeys) => {
  2543. const set = new Set([...filteredExpandedKeys, ...expandedKeys]);
  2544. setExpandedKeys(Array.from(set));
  2545. }, [setExpandedKeys]);
  2546. return (
  2547. <>
  2548. <TreeSelect
  2549. // multiple
  2550. style={{ width: 300 }}
  2551. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  2552. treeData={treeData}
  2553. filterTreeNode
  2554. searchPosition='trigger'
  2555. showFilteredOnly
  2556. expandedKeys={expandedKeys}
  2557. onExpand={onExpand}
  2558. onSearch={onSearch}
  2559. />
  2560. </>
  2561. );
  2562. };