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