treeSelect.stories.jsx 71 KB

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