treeSelect.stories.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. import React, { useState } from 'react';
  2. import { Icon, Button, Form, Popover, Tag, Typography, CheckboxGroup } from '../../index';
  3. import TreeSelect from '../index';
  4. import { flattenDeep } from 'lodash';
  5. import CustomTrigger from './CustomTrigger';
  6. import { IconCreditCard } from '@douyinfe/semi-icons';
  7. const TreeNode = TreeSelect.TreeNode;
  8. const { Title } = Typography;
  9. export default {
  10. title: 'TreeSelect',
  11. parameters: {
  12. chromatic: { disableSnapshot: true },
  13. },
  14. }
  15. const treeData1 = [
  16. {
  17. label: 'Node1',
  18. value: '0-0',
  19. key: '0-0',
  20. children: [
  21. {
  22. label: 'Child Node1',
  23. value: '0-0-1',
  24. key: '0-0-1',
  25. },
  26. {
  27. label: 'Child Node2',
  28. value: '0-0-2',
  29. key: '0-0-2',
  30. },
  31. ],
  32. },
  33. {
  34. label: 'Node2',
  35. value: '0-1',
  36. key: '0-1',
  37. },
  38. ];
  39. const treeData2 = [
  40. {
  41. label: '亚洲',
  42. value: 'yazhou',
  43. key: 'yazhou',
  44. children: [
  45. {
  46. label: '中国',
  47. value: 'zhongguo',
  48. key: 'zhongguo',
  49. children: [
  50. {
  51. label: '北京',
  52. value: 'beijing',
  53. key: 'beijing',
  54. },
  55. {
  56. label: '上海',
  57. value: 'shanghai',
  58. key: 'shanghai',
  59. },
  60. ],
  61. },
  62. // {
  63. // label: '日本',
  64. // value: 'riben',
  65. // key: 'riben',
  66. // children: [
  67. // {
  68. // label: '东京',
  69. // value: 'dongjing',
  70. // key: 'dongjing'
  71. // },
  72. // {
  73. // label: '大阪',
  74. // value: 'daban',
  75. // key: 'daban'
  76. // }
  77. // ]
  78. // },
  79. ],
  80. },
  81. {
  82. label: '北美洲',
  83. value: 'beimeizhou',
  84. key: 'beimeizhou',
  85. children: [
  86. {
  87. label: '美国',
  88. value: 'meiguo',
  89. key: 'meiguo',
  90. },
  91. {
  92. label: '加拿大',
  93. value: 'jianada',
  94. key: 'jianada',
  95. },
  96. ],
  97. },
  98. ];
  99. const treeDataWithoutValue = [
  100. {
  101. label: '亚洲',
  102. key: 'yazhou',
  103. children: [
  104. {
  105. label: '中国',
  106. key: 'zhongguo',
  107. disabled: true,
  108. children: [
  109. {
  110. label: '北京',
  111. key: 'beijing',
  112. },
  113. {
  114. label: '上海',
  115. key: 'shanghai',
  116. },
  117. ],
  118. },
  119. {
  120. label: '日本',
  121. key: 'riben',
  122. children: [
  123. {
  124. label: '东京',
  125. key: 'dongjing',
  126. },
  127. {
  128. label: '大阪',
  129. key: 'daban',
  130. },
  131. ],
  132. },
  133. ],
  134. },
  135. {
  136. label: '北美洲',
  137. key: 'beimeizhou',
  138. children: [
  139. {
  140. label: '美国',
  141. key: 'meiguo',
  142. },
  143. {
  144. label: '加拿大',
  145. key: 'jianada',
  146. },
  147. ],
  148. },
  149. ];
  150. export const TreeSelectWrapper = () => (
  151. <div>
  152. <div>github issue 750 修改测试用例</div>
  153. <CheckboxGroup>
  154. <TreeSelect
  155. showClear={true}
  156. expandAll
  157. style={{width: 400}}
  158. treeData={[
  159. {
  160. key: '1',
  161. label: '所有节点',
  162. value: '1',
  163. children: [
  164. { key: '20006251', label: 'Semi', value: '[email protected]' },
  165. { key: '20006248', label: 'Design', value: '[email protected]' },
  166. {
  167. key: '20006205',
  168. label: 'React',
  169. value: '[email protected]',
  170. },
  171. ],
  172. },
  173. ]}
  174. multiple
  175. filterTreeNode
  176. showFilteredOnly={true}
  177. leafOnly
  178. />
  179. </CheckboxGroup>
  180. </div>
  181. );
  182. TreeSelectWrapper.story = {
  183. name: 'treeSelect wrapper',
  184. };
  185. class SimpleTree extends React.Component {
  186. render() {
  187. return (
  188. <div>
  189. <TreeSelect
  190. style={{ width: 300 }}
  191. // value={this.state.value}
  192. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  193. treeData={treeData1}
  194. placeholder="Please select"
  195. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  196. onSelect={(e, bool) => console.log('select', e, bool)}
  197. onChange={(e, node) => console.log('change', e, node)}
  198. />
  199. <br />
  200. <br />
  201. <TreeSelect
  202. style={{ width: 300 }}
  203. // value={this.state.value}
  204. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  205. treeData={treeData2}
  206. placeholder="Please select"
  207. />
  208. </div>
  209. );
  210. }
  211. }
  212. export const _TreeSelect = () => {
  213. const treeData = [
  214. {
  215. label: '亚洲',
  216. value: 'Asia',
  217. key: '0',
  218. children: [
  219. {
  220. label: '中国',
  221. value: 'China',
  222. key: '0-0',
  223. children: [
  224. {
  225. label: '北京',
  226. value: 'Beijing',
  227. key: '0-0-0',
  228. },
  229. {
  230. label: '上海',
  231. value: 'Shanghai',
  232. key: '0-0-1',
  233. },
  234. ],
  235. },
  236. ],
  237. },
  238. {
  239. label: '北美洲',
  240. value: 'North America',
  241. key: '1',
  242. },
  243. ];
  244. return (
  245. <TreeSelect
  246. style={{ width: 300 }}
  247. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  248. treeData={treeData2}
  249. placeholder="请选择"
  250. />
  251. );
  252. };
  253. _TreeSelect.story = {
  254. name: 'tree select',
  255. };
  256. export const Searchable = () => (
  257. <div>
  258. <TreeSelect
  259. style={{ width: 300 }}
  260. // value={this.state.value}
  261. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  262. treeData={treeData2}
  263. filterTreeNode
  264. treeNodeFilterProp="value"
  265. placeholder="Please select"
  266. />
  267. <br />
  268. <br />
  269. <TreeSelect
  270. style={{ width: 300 }}
  271. // value={this.state.value}
  272. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  273. treeData={treeData2}
  274. filterTreeNode
  275. placeholder="Please select"
  276. />
  277. <br />
  278. <br />
  279. <TreeSelect
  280. style={{ width: 300 }}
  281. // value={this.state.value}
  282. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  283. treeData={treeData2}
  284. filterTreeNode
  285. searchAutoFocus
  286. placeholder="searchAutoFocus"
  287. />
  288. </div>
  289. );
  290. Searchable.story = {
  291. name: 'searchable',
  292. };
  293. export const SearchPosition = () => (
  294. <>
  295. <TreeSelect
  296. searchPosition="trigger"
  297. style={{ width: 300 }}
  298. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  299. treeData={treeData2}
  300. filterTreeNode
  301. placeholder="单选"
  302. />
  303. <br />
  304. <br />
  305. <TreeSelect
  306. searchPosition="trigger"
  307. style={{ width: 300 }}
  308. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  309. treeData={treeData2}
  310. multiple
  311. filterTreeNode
  312. maxTagCount={2}
  313. placeholder="多选"
  314. />
  315. <br />
  316. <br />
  317. <TreeSelect
  318. searchPosition="trigger"
  319. style={{ width: 300 }}
  320. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  321. treeData={treeData2}
  322. multiple
  323. filterTreeNode
  324. maxTagCount={2}
  325. placeholder="searchAutoFocus"
  326. searchAutoFocus
  327. />
  328. <br />
  329. <br />
  330. <TreeSelect
  331. searchPosition="trigger"
  332. style={{ width: 300 }}
  333. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  334. treeData={treeData2}
  335. multiple
  336. filterTreeNode
  337. maxTagCount={1}
  338. placeholder="maxTagCount=1"
  339. />
  340. </>
  341. );
  342. SearchPosition.story = {
  343. name: 'searchPosition',
  344. };
  345. export const PrefixSuffixInsetLabel = () => (
  346. <div>
  347. <TreeSelect
  348. style={{ width: 300 }}
  349. // value={this.state.value}
  350. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  351. treeData={treeData2}
  352. filterTreeNode
  353. prefix={<IconCreditCard />}
  354. treeNodeFilterProp="value"
  355. placeholder="Please select"
  356. />
  357. <br />
  358. <br />
  359. <TreeSelect
  360. style={{ width: 300 }}
  361. // value={this.state.value}
  362. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  363. treeData={treeData2}
  364. filterTreeNode
  365. prefix={<span>1234</span>}
  366. treeNodeFilterProp="value"
  367. placeholder="Please select"
  368. />
  369. <br />
  370. <br />
  371. <TreeSelect
  372. style={{ width: 300 }}
  373. suffix="RMB"
  374. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  375. treeData={treeData2}
  376. placeholder="Please select"
  377. />
  378. <br />
  379. <br />
  380. <TreeSelect
  381. style={{ width: 300 }}
  382. insetLabel="blablabla"
  383. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  384. treeData={treeData2}
  385. placeholder="Please select"
  386. />
  387. <br />
  388. <br />
  389. <TreeSelect
  390. style={{ width: 300 }}
  391. insetLabel={<span>1234</span>}
  392. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  393. treeData={treeData2}
  394. placeholder="Please select"
  395. />
  396. </div>
  397. );
  398. PrefixSuffixInsetLabel.story = {
  399. name: 'prefix suffix insetLabel',
  400. };
  401. PrefixSuffixInsetLabel.parameters = {
  402. chromatic: { disableSnapshot: false },
  403. }
  404. export const ValidateStatus = () => (
  405. <div>
  406. <TreeSelect
  407. style={{ width: 300 }}
  408. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  409. treeData={treeData2}
  410. multiple
  411. validateStatus="warning"
  412. placeholder="Please select"
  413. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  414. onSelect={(e, bool) => console.log('select', e, bool)}
  415. onChange={e => console.log('change', e)}
  416. />
  417. <br />
  418. <br />
  419. <TreeSelect
  420. style={{ width: 300 }}
  421. validateStatus="error"
  422. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  423. treeData={treeData2}
  424. // multiple
  425. placeholder="Please select"
  426. />
  427. </div>
  428. );
  429. ValidateStatus.story = {
  430. name: 'validate status',
  431. };
  432. ValidateStatus.parameters = {
  433. chromatic: { disableSnapshot: false },
  434. }
  435. export const Multiple = () => (
  436. <div>
  437. <TreeSelect
  438. style={{ width: 300 }}
  439. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  440. treeData={treeData2}
  441. multiple
  442. placeholder="Please select"
  443. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  444. onSelect={(e, bool) => console.log('select', e, bool)}
  445. onChange={e => console.log('change', e)}
  446. />
  447. <br />
  448. <br />
  449. <TreeSelect
  450. style={{ width: 300 }}
  451. // value={this.state.value}
  452. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  453. treeData={treeData2}
  454. multiple
  455. placeholder="Please select"
  456. />
  457. </div>
  458. );
  459. Multiple.story = {
  460. name: 'multiple',
  461. };
  462. export const MaxTagCount = () => (
  463. <div>
  464. <TreeSelect
  465. style={{ width: 300 }}
  466. // value={this.state.value}
  467. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  468. treeData={treeData2}
  469. multiple
  470. maxTagCount={2}
  471. placeholder="Please select"
  472. />
  473. {/* <br />
  474. <br />
  475. <TreeSelect
  476. style={{ width: 300 }}
  477. // value={this.state.value}
  478. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  479. treeData={treeData2}
  480. multiple
  481. placeholder="Please select"
  482. /> */}
  483. </div>
  484. );
  485. MaxTagCount.story = {
  486. name: 'maxTagCount',
  487. };
  488. export const MultipleSearchable = () => (
  489. <div>
  490. <TreeSelect
  491. style={{ width: 300 }}
  492. // value={this.state.value}
  493. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  494. treeData={treeData2}
  495. multiple
  496. filterTreeNode
  497. treeNodeFilterProp="value"
  498. placeholder="Please select"
  499. />
  500. </div>
  501. );
  502. MultipleSearchable.story = {
  503. name: 'multiple searchable',
  504. };
  505. export const DefaultValues = () => (
  506. <div>
  507. <TreeSelect
  508. style={{ width: 300 }}
  509. defaultValue={'shanghai'}
  510. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  511. treeData={treeData2}
  512. // multiple
  513. filterTreeNode
  514. treeNodeFilterProp="value"
  515. />
  516. <br />
  517. <br />
  518. <TreeSelect
  519. style={{ width: 300 }}
  520. defaultValue={'shanghai'}
  521. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  522. treeData={treeData2}
  523. filterTreeNode
  524. multiple
  525. treeNodeFilterProp="value"
  526. />
  527. <br />
  528. <br />
  529. <TreeSelect
  530. style={{ width: 300 }}
  531. defaultValue={['shanghai', 'daban', 'dongjing']}
  532. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  533. treeData={treeData2}
  534. filterTreeNode
  535. multiple
  536. treeNodeFilterProp="value"
  537. />
  538. <br />
  539. <br />
  540. <TreeSelect
  541. style={{ width: 300 }}
  542. defaultValue={['meiguo', 'dongjing']}
  543. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  544. treeData={treeData2}
  545. filterTreeNode
  546. multiple
  547. treeNodeFilterProp="value"
  548. />
  549. </div>
  550. );
  551. DefaultValues.story = {
  552. name: 'default values',
  553. };
  554. export const Disabled = () => (
  555. <div>
  556. <TreeSelect
  557. style={{ width: 300 }}
  558. defaultValue={'shanghai'}
  559. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  560. treeData={treeData2}
  561. multiple
  562. filterTreeNode
  563. disabled
  564. treeNodeFilterProp="value"
  565. placeholder="Please select"
  566. />
  567. <br />
  568. <br />
  569. <TreeSelect
  570. style={{ width: 300 }}
  571. defaultValue={'shanghai'}
  572. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  573. treeData={treeData2}
  574. filterTreeNode
  575. disabled
  576. treeNodeFilterProp="value"
  577. placeholder="Please select"
  578. />
  579. <br />
  580. <br />
  581. <TreeSelect
  582. style={{ width: 300 }}
  583. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  584. treeData={treeData2}
  585. multiple
  586. filterTreeNode
  587. disabled
  588. treeNodeFilterProp="value"
  589. placeholder="Please select"
  590. />
  591. </div>
  592. );
  593. Disabled.story = {
  594. name: 'disabled',
  595. };
  596. Disabled.parameters = {
  597. chromatic: { disableSnapshot: false },
  598. }
  599. export const OptionLabelProp = () => (
  600. <>
  601. <TreeSelect
  602. style={{ width: 300 }}
  603. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  604. treeData={treeData2}
  605. multiple
  606. filterTreeNode
  607. treeNodeFilterProp="value"
  608. treeNodeLabelProp="value"
  609. placeholder="Please select"
  610. />
  611. </>
  612. );
  613. OptionLabelProp.story = {
  614. name: 'optionLabelProp',
  615. };
  616. export const ValueInArray = () => (
  617. <>
  618. <TreeSelect
  619. style={{ width: 300 }}
  620. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  621. treeData={treeData2}
  622. multiple
  623. filterTreeNode
  624. valueInArray
  625. onChange={(...args) => console.log(args)}
  626. treeNodeFilterProp="value"
  627. treeNodeLabelProp="value"
  628. placeholder="Please select"
  629. />
  630. </>
  631. );
  632. ValueInArray.story = {
  633. name: 'valueInArray',
  634. };
  635. export const OnBlurOnFocus = () => (
  636. <>
  637. <div>single</div>
  638. <TreeSelect
  639. style={{ width: 300 }}
  640. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  641. treeData={treeData2}
  642. onBlur={(...args) => console.log('blur', args)}
  643. onFocus={(...args) => console.log('focus', args)}
  644. placeholder="Please select"
  645. />
  646. <div>multiple</div>
  647. <TreeSelect
  648. style={{ width: 300 }}
  649. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  650. treeData={treeData2}
  651. multiple
  652. onBlur={(...args) => console.log('blur', args)}
  653. onFocus={(...args) => console.log('focus', args)}
  654. placeholder="Please select"
  655. />
  656. </>
  657. );
  658. OnBlurOnFocus.story = {
  659. name: 'onBlur/onFocus',
  660. };
  661. export const LeafOnly = () => (
  662. <div>
  663. <TreeSelect
  664. style={{ width: 300 }}
  665. // value={this.state.value}
  666. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  667. treeData={treeData2}
  668. multiple
  669. leafOnly
  670. placeholder="Please select"
  671. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  672. onSelect={(e, bool) => console.log('select', e, bool)}
  673. onChange={e => console.log('change', e)}
  674. />
  675. </div>
  676. );
  677. LeafOnly.story = {
  678. name: 'leafOnly',
  679. };
  680. class Demo extends React.Component {
  681. constructor() {
  682. super();
  683. this.state = {
  684. value: 'shanghai',
  685. };
  686. }
  687. onChange(value) {
  688. this.setState({ value });
  689. }
  690. render() {
  691. return (
  692. <TreeSelect
  693. style={{ width: 300 }}
  694. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  695. treeData={treeData2}
  696. value={this.state.value}
  697. placeholder="Please select"
  698. onChange={e => this.onChange(e)}
  699. />
  700. );
  701. }
  702. }
  703. export const ControlledComponentSingle = () => <Demo />;
  704. ControlledComponentSingle.story = {
  705. name: 'controlled Component single',
  706. };
  707. class Demo2 extends React.Component {
  708. constructor() {
  709. super();
  710. this.state = {
  711. value: ['Shanghai'],
  712. };
  713. }
  714. onChange(value) {
  715. this.setState({ value });
  716. }
  717. render() {
  718. console.log(this.state.value);
  719. const treeData = [
  720. {
  721. label: '亚洲',
  722. value: 'Asia',
  723. key: '0',
  724. children: [
  725. {
  726. label: '中国',
  727. value: 'China',
  728. key: '0-0',
  729. children: [
  730. {
  731. label: '北京',
  732. value: 'Beijing',
  733. key: '0-0-0',
  734. },
  735. {
  736. label: '上海',
  737. value: 'Shanghai',
  738. key: '0-0-1',
  739. },
  740. ],
  741. },
  742. ],
  743. },
  744. {
  745. label: '北美洲',
  746. value: 'North America',
  747. key: '1',
  748. },
  749. ];
  750. return (
  751. <TreeSelect
  752. style={{ width: 300 }}
  753. multiple
  754. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  755. treeData={treeData}
  756. value={this.state.value}
  757. placeholder="Please select"
  758. onChange={e => this.onChange(e)}
  759. />
  760. );
  761. }
  762. }
  763. export const ControlledComponentMultiple = () => <Demo2 />;
  764. ControlledComponentMultiple.story = {
  765. name: 'controlled Component multiple',
  766. };
  767. class Demo3 extends React.Component {
  768. constructor() {
  769. super();
  770. this.state = {
  771. value: ['Shanghai'],
  772. };
  773. }
  774. // 获取最底层值
  775. getDeepChildrensByNode = node => {
  776. return flattenDeep(
  777. node.map(item => {
  778. if (item.children) {
  779. return this.getDeepChildrensByNode(item.children);
  780. }
  781. return item.value;
  782. })
  783. );
  784. };
  785. onChange(value, node) {
  786. console.log('onchange', value);
  787. value = this.getDeepChildrensByNode(node);
  788. console.log('modifiled', value);
  789. this.setState({ value });
  790. }
  791. render() {
  792. const treeData = [
  793. {
  794. label: '亚洲',
  795. value: 'Asia',
  796. key: '0',
  797. children: [
  798. {
  799. label: '中国',
  800. value: 'China',
  801. key: '0-0',
  802. children: [
  803. {
  804. label: '北京',
  805. value: 'Beijing',
  806. key: '0-0-0',
  807. },
  808. {
  809. label: '上海',
  810. value: 'Shanghai',
  811. key: '0-0-1',
  812. },
  813. ],
  814. },
  815. ],
  816. },
  817. {
  818. label: '北美洲',
  819. value: 'North America',
  820. key: '1',
  821. },
  822. ];
  823. return (
  824. <TreeSelect
  825. style={{ width: 300 }}
  826. multiple
  827. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  828. treeData={treeData}
  829. value={this.state.value}
  830. placeholder="Please select"
  831. onChange={(e, node) => this.onChange(e, node)}
  832. />
  833. );
  834. }
  835. }
  836. export const ControlledComponentMultipleValueModified = () => <Demo3 />;
  837. ControlledComponentMultipleValueModified.story = {
  838. name: 'controlled Component multiple value modified',
  839. };
  840. class ConvertDemo extends React.Component {
  841. constructor(props) {
  842. super(props);
  843. this.formApi = null;
  844. }
  845. handleChange = val => {
  846. let finalVal = val;
  847. let firstClassOption = ['Asia', 'North America'];
  848. // 在这里去做你的value替换逻辑
  849. console.log('originVal:' + val);
  850. if (val.length === 1) {
  851. // do nothing
  852. } else {
  853. if (val.every(item => firstClassOption.includes(item))) {
  854. finalVal = val[val.length - 1];
  855. }
  856. }
  857. console.log('finalVal:' + finalVal);
  858. return finalVal;
  859. };
  860. render() {
  861. const treeData = [
  862. {
  863. label: '亚洲',
  864. value: 'Asia',
  865. key: '0',
  866. children: [
  867. {
  868. label: '中国',
  869. value: 'China',
  870. key: '0-0',
  871. children: [
  872. {
  873. label: '北京',
  874. value: 'Beijing',
  875. key: '0-0-0',
  876. },
  877. {
  878. label: '上海',
  879. value: 'Shanghai',
  880. key: '0-0-1',
  881. },
  882. ],
  883. },
  884. ],
  885. },
  886. {
  887. label: '北美洲',
  888. value: 'North America',
  889. key: '1',
  890. },
  891. ];
  892. return (
  893. <Form getFormApi={this.getFormApi}>
  894. <Form.TreeSelect
  895. field="tree"
  896. label="节点(TreeSelect)"
  897. placeholder="请选择服务节点"
  898. treeData={treeData}
  899. convert={this.handleChange}
  900. filterTreeNode
  901. multiple
  902. ></Form.TreeSelect>
  903. </Form>
  904. );
  905. }
  906. }
  907. export const _ConvertDemo = () => <ConvertDemo />;
  908. _ConvertDemo.story = {
  909. name: 'convert demo',
  910. };
  911. export const TreeselectDefaultOpenInPopover = () => (
  912. <Popover
  913. content={
  914. <TreeSelect
  915. style={{ width: 200 }}
  916. dropdownStyle={{ width: 200 }}
  917. treeData={treeData1}
  918. defaultOpen
  919. // value={this.state.value}
  920. // placeholder="Please select"
  921. // onChange={(e, node) => this.onChange(e, node)}
  922. />
  923. }
  924. >
  925. <Button style={{ marginLeft: 150 }}>悬停此处</Button>
  926. </Popover>
  927. );
  928. TreeselectDefaultOpenInPopover.story = {
  929. name: 'treeselect defaultOpen in popover',
  930. };
  931. export const CustomTriggerDemo = () => <CustomTrigger />;
  932. CustomTriggerDemo.story = { name: 'custom trigger' };
  933. const AutoParentDemo = () => {
  934. const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
  935. const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
  936. const [autoExpandParent, setAutoExpandParent] = useState(true);
  937. const onExpand = expandedKeys => {
  938. console.log('onExpand', expandedKeys);
  939. // if not set autoExpandParent to false, if children expanded, parent can not collapse.
  940. // or, you can remove all expanded children keys.
  941. setExpandedKeys(expandedKeys);
  942. setAutoExpandParent(false);
  943. };
  944. const onSelect = (selectedKeys, info) => {
  945. console.log('onSelect:', info);
  946. setSelectedKeys(selectedKeys);
  947. };
  948. return (
  949. <div>
  950. <Button
  951. onClick={() => {
  952. setSelectedKeys(['beijing']);
  953. setExpandedKeys(['beijing']);
  954. setAutoExpandParent(true);
  955. }}
  956. >
  957. Update
  958. </Button>
  959. <TreeSelect
  960. onExpand={onExpand}
  961. expandedKeys={expandedKeys}
  962. autoExpandParent={autoExpandParent}
  963. onChange={onSelect}
  964. value={selectedKeys}
  965. treeData={treeData2}
  966. // multiple
  967. />
  968. </div>
  969. );
  970. };
  971. export const AutoExpandParent = () => (
  972. <>
  973. <AutoParentDemo />
  974. </>
  975. );
  976. AutoExpandParent.story = {
  977. name: 'autoExpandParent',
  978. };
  979. export const TreeWithoutValueProps = () => (
  980. <TreeSelect
  981. treeData={treeDataWithoutValue}
  982. value="beijing"
  983. defaultExpandAll
  984. onChange={(...args) => console.log(args)}
  985. />
  986. );
  987. TreeWithoutValueProps.story = {
  988. name: 'tree without value props',
  989. };
  990. export const TreeSelectRenderSelectedItem = () => {
  991. const treeData = [
  992. {
  993. label: '亚洲',
  994. value: 'Asia',
  995. key: '0',
  996. children: [
  997. {
  998. label: '中国',
  999. value: 'China',
  1000. key: '0-0',
  1001. children: [
  1002. {
  1003. label: '北京',
  1004. value: 'Beijing',
  1005. key: '0-0-0',
  1006. },
  1007. {
  1008. label: '上海',
  1009. value: 'Shanghai',
  1010. key: '0-0-1',
  1011. },
  1012. ],
  1013. },
  1014. ],
  1015. },
  1016. {
  1017. label: '北美洲',
  1018. value: 'North America',
  1019. key: '1',
  1020. },
  1021. {
  1022. label: '南美洲',
  1023. value: 'South America',
  1024. key: '2',
  1025. },
  1026. {
  1027. label: '南极洲',
  1028. value: 'Antarctica',
  1029. key: '3',
  1030. },
  1031. ];
  1032. return (
  1033. <>
  1034. <h4>单选</h4>
  1035. <TreeSelect
  1036. style={{ width: 300 }}
  1037. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1038. treeData={treeData}
  1039. placeholder="请选择"
  1040. renderSelectedItem={item => `${item.value}-${item.label}`}
  1041. />
  1042. <h4>多选</h4>
  1043. <TreeSelect
  1044. style={{ width: 300 }}
  1045. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1046. treeData={treeData}
  1047. multiple
  1048. placeholder="请选择"
  1049. renderSelectedItem={(item, { index, onClose }) => ({
  1050. content: `${item.value}-${item.label}`,
  1051. isRenderInTag: true,
  1052. })}
  1053. />
  1054. <h4>多选 + isRenderInTag=false</h4>
  1055. <TreeSelect
  1056. style={{ width: 300 }}
  1057. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1058. treeData={treeData}
  1059. multiple
  1060. maxTagCount={2}
  1061. placeholder="请选择"
  1062. renderSelectedItem={(item, { index, onClose }) => ({
  1063. content: (
  1064. <Tag key={index} color="white">
  1065. {item.value}
  1066. </Tag>
  1067. ),
  1068. isRenderInTag: false,
  1069. })}
  1070. />
  1071. </>
  1072. );
  1073. };
  1074. TreeSelectRenderSelectedItem.story = {
  1075. name: 'treeSelect renderSelectedItem',
  1076. };
  1077. const DisableStrictlyDemo = () => {
  1078. const [value, setValue] = useState(['Shanghai']);
  1079. const treeData = [
  1080. {
  1081. label: '亚洲',
  1082. value: 'Asia',
  1083. key: '0',
  1084. children: [
  1085. {
  1086. label: '中国',
  1087. value: 'China',
  1088. key: '0-0',
  1089. disabled: true,
  1090. children: [
  1091. {
  1092. label: '北京',
  1093. value: 'Beijing',
  1094. key: '0-0-0',
  1095. },
  1096. {
  1097. label: '上海',
  1098. value: 'Shanghai',
  1099. key: '0-0-1',
  1100. },
  1101. ],
  1102. },
  1103. {
  1104. label: '日本',
  1105. value: 'Japan',
  1106. key: '0-1',
  1107. },
  1108. ],
  1109. },
  1110. {
  1111. label: '北美洲',
  1112. value: 'North America',
  1113. key: '1',
  1114. },
  1115. ];
  1116. return (
  1117. <div>
  1118. <TreeSelect
  1119. style={{ width: 300 }}
  1120. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1121. treeData={treeData}
  1122. disableStrictly
  1123. multiple
  1124. searchPosition="trigger"
  1125. filterTreeNode
  1126. value={value}
  1127. onChange={value => setValue(value)}
  1128. />
  1129. </div>
  1130. );
  1131. };
  1132. export const DisabledStrictly = () => (
  1133. <>
  1134. <DisableStrictlyDemo />
  1135. </>
  1136. );
  1137. DisabledStrictly.story = {
  1138. name: 'disabledStrictly',
  1139. };
  1140. export const CheckRelationDemo = () => {
  1141. const treeData = [
  1142. {
  1143. label: 'Asia',
  1144. value: 'Asia',
  1145. key: '0',
  1146. children: [
  1147. {
  1148. label: 'China',
  1149. value: 'China',
  1150. key: '0-0',
  1151. children: [
  1152. {
  1153. label: 'Beijing',
  1154. value: 'Beijing',
  1155. key: '0-0-0',
  1156. },
  1157. {
  1158. label: 'Shanghai',
  1159. value: 'Shanghai',
  1160. key: '0-0-1',
  1161. },
  1162. {
  1163. label: 'Chengdu',
  1164. value: 'Chengdu',
  1165. key: '0-0-2',
  1166. },
  1167. ],
  1168. },
  1169. {
  1170. label: 'Japan',
  1171. value: 'Japan',
  1172. key: '0-1',
  1173. children: [
  1174. {
  1175. label: 'Osaka',
  1176. value: 'Osaka',
  1177. key: '0-1-0'
  1178. }
  1179. ]
  1180. },
  1181. ],
  1182. },
  1183. {
  1184. label: 'North America',
  1185. value: 'North America',
  1186. key: '1',
  1187. children: [
  1188. {
  1189. label: 'United States',
  1190. value: 'United States',
  1191. key: '1-0'
  1192. },
  1193. {
  1194. label: 'Canada',
  1195. value: 'Canada',
  1196. key: '1-1'
  1197. }
  1198. ]
  1199. }
  1200. ];
  1201. const [value, setValue] = useState('China');
  1202. const [value2, setValue2] = useState();
  1203. const [value3, setValue3] = useState();
  1204. const style = {
  1205. width: 300,
  1206. };
  1207. const dropdownStyle = {
  1208. maxHeight: 400,
  1209. overflow: 'auto'
  1210. };
  1211. const handleChange = value => {
  1212. console.log(value);
  1213. setValue(value);
  1214. };
  1215. const handleChange2 = value => {
  1216. console.log(value);
  1217. setValue2(value);
  1218. };
  1219. const handleChange3 = value => {
  1220. console.log(value);
  1221. setValue3(value);
  1222. };
  1223. return (
  1224. <>
  1225. <div>checkRelation='unRelated'</div>
  1226. <TreeSelect
  1227. dropdownStyle={dropdownStyle}
  1228. treeData={treeData}
  1229. multiple
  1230. checkRelation='unRelated'
  1231. defaultExpandAll
  1232. style={style}
  1233. />
  1234. <br /><br />
  1235. <div>checkRelation='unRelated' + maxTagCount=2</div>
  1236. <TreeSelect
  1237. dropdownStyle={dropdownStyle}
  1238. treeData={treeData}
  1239. multiple
  1240. maxTagCount={2}
  1241. checkRelation='unRelated'
  1242. defaultExpandAll
  1243. style={style}
  1244. />
  1245. <br /><br />
  1246. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索</div>
  1247. <TreeSelect
  1248. dropdownStyle={dropdownStyle}
  1249. treeData={treeData}
  1250. multiple
  1251. maxTagCount={2}
  1252. filterTreeNode
  1253. checkRelation='unRelated'
  1254. defaultExpandAll
  1255. style={style}
  1256. />
  1257. <br /><br />
  1258. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索 + searchBox in trigger</div>
  1259. <TreeSelect
  1260. dropdownStyle={dropdownStyle}
  1261. treeData={treeData}
  1262. multiple
  1263. maxTagCount={2}
  1264. filterTreeNode
  1265. checkRelation='unRelated'
  1266. searchPosition='trigger'
  1267. defaultExpandAll
  1268. style={style}
  1269. />
  1270. <br /><br />
  1271. <div>checkRelation='unRelated' + 中国节点为 disabled</div>
  1272. <TreeSelect
  1273. dropdownStyle={dropdownStyle}
  1274. treeData={treeDataWithoutValue}
  1275. multiple
  1276. checkRelation='unRelated'
  1277. defaultExpandAll
  1278. style={style}
  1279. />
  1280. <br /><br />
  1281. <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
  1282. <TreeSelect
  1283. dropdownStyle={dropdownStyle}
  1284. treeData={treeDataWithoutValue}
  1285. multiple
  1286. checkRelation='unRelated'
  1287. defaultExpandAll
  1288. disableStrictly
  1289. style={style}
  1290. />
  1291. <br /><br />
  1292. <div>checkRelation='unRelated' + defaultValue 为 China</div>
  1293. <TreeSelect
  1294. dropdownStyle={dropdownStyle}
  1295. treeData={treeData}
  1296. multiple
  1297. checkRelation='unRelated'
  1298. defaultExpandAll
  1299. style={style}
  1300. defaultValue='China'
  1301. />
  1302. <br /><br />
  1303. <div>checkRelation='unRelated' + defaultValue 为 China + 开启搜索</div>
  1304. <TreeSelect
  1305. dropdownStyle={dropdownStyle}
  1306. treeData={treeData}
  1307. multiple
  1308. filterTreeNode
  1309. checkRelation='unRelated'
  1310. defaultExpandAll
  1311. style={style}
  1312. defaultValue='China'
  1313. />
  1314. <br /><br />
  1315. <div>多选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1316. <TreeSelect
  1317. dropdownStyle={dropdownStyle}
  1318. treeData={treeData}
  1319. multiple
  1320. filterTreeNode
  1321. showClear
  1322. checkRelation='unRelated'
  1323. defaultExpandAll
  1324. style={style}
  1325. searchPosition='trigger'
  1326. defaultValue={['China', 'Japan']}
  1327. />
  1328. <br /><br />
  1329. <div>单选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1330. <TreeSelect
  1331. dropdownStyle={dropdownStyle}
  1332. treeData={treeData}
  1333. filterTreeNode
  1334. showClear
  1335. checkRelation='unRelated'
  1336. defaultExpandAll
  1337. style={style}
  1338. searchPosition='trigger'
  1339. defaultValue='China'
  1340. />
  1341. <br /><br />
  1342. <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
  1343. <TreeSelect
  1344. dropdownStyle={dropdownStyle}
  1345. treeData={treeData}
  1346. multiple
  1347. checkRelation='unRelated'
  1348. defaultExpandAll
  1349. style={style}
  1350. value={value}
  1351. onChange={handleChange}
  1352. />
  1353. <br /><br />
  1354. <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
  1355. <TreeSelect
  1356. dropdownStyle={dropdownStyle}
  1357. treeData={treeData}
  1358. multiple
  1359. checkRelation='unRelated'
  1360. defaultExpandAll
  1361. style={style}
  1362. value={value2}
  1363. onChangeWithObject
  1364. onChange={handleChange2}
  1365. />
  1366. <br /><br />
  1367. <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
  1368. <TreeSelect
  1369. dropdownStyle={dropdownStyle}
  1370. leafOnly
  1371. treeData={treeData}
  1372. multiple
  1373. checkRelation='unRelated'
  1374. defaultExpandAll
  1375. style={style}
  1376. value={value3}
  1377. onChange={handleChange3}
  1378. />
  1379. <br /><br />
  1380. <div>checkRelation='unRelated' + onSelect </div>
  1381. <TreeSelect
  1382. dropdownStyle={dropdownStyle}
  1383. treeData={treeData}
  1384. multiple
  1385. checkRelation='unRelated'
  1386. defaultExpandAll
  1387. style={style}
  1388. onSelect={(value,status,node)=>console.log('select', value, status, node)}
  1389. />
  1390. </>
  1391. );
  1392. };
  1393. export const SearchableAndExpandedKeys = () => {
  1394. const [expandedKeys1, setExpandedKeys1] = useState([]);
  1395. const [expandedKeys2, setExpandedKeys2] = useState([]);
  1396. const [expandedKeys3, setExpandedKeys3] = useState([]);
  1397. return (
  1398. <>
  1399. <Title heading={6}>expandedKeys 受控</Title>
  1400. <TreeSelect
  1401. style={{ width: 300, marginBottom: 30 }}
  1402. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1403. treeData={treeData2}
  1404. expandedKeys={expandedKeys1}
  1405. defaultValue='beijing'
  1406. onExpand={v => {
  1407. console.log('onExpand value: ', v);
  1408. setExpandedKeys1(v);
  1409. }}
  1410. />
  1411. <Title heading={6}>expandedKeys 受控 + 开启搜索</Title>
  1412. <TreeSelect
  1413. style={{ width: 300, marginBottom: 30 }}
  1414. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1415. treeData={treeData2}
  1416. filterTreeNode
  1417. defaultValue='beijing'
  1418. expandedKeys={expandedKeys2}
  1419. onExpand={v => {
  1420. console.log('onExpand value: ', v);
  1421. setExpandedKeys2(v);
  1422. }}
  1423. />
  1424. <Title heading={6}>expandedKeys 受控 + 开启搜索 + 搜索时更新 expandedKeys</Title>
  1425. <TreeSelect
  1426. style={{ width: 300, marginBottom: 30 }}
  1427. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1428. treeData={treeData2}
  1429. filterTreeNode
  1430. expandedKeys={expandedKeys3}
  1431. defaultValue='beijing'
  1432. onExpand={v => {
  1433. console.log('onExpand value: ', v);
  1434. setExpandedKeys3(v)
  1435. }}
  1436. onSearch={(input, filterExpandedKeys) => {
  1437. console.log('onExpand filterExpandedKeys: ', filterExpandedKeys);
  1438. setExpandedKeys3(filterExpandedKeys);
  1439. }}
  1440. />
  1441. </>
  1442. )
  1443. }
  1444. export const loadData = () => {
  1445. const initialData = [
  1446. {
  1447. label: 'Expand to load',
  1448. value: '0',
  1449. key: '0',
  1450. },
  1451. {
  1452. label: 'Expand to load',
  1453. value: '1',
  1454. key: '1',
  1455. },
  1456. {
  1457. label: 'Leaf Node',
  1458. value: '2',
  1459. key: '2',
  1460. isLeaf: true,
  1461. },
  1462. ];
  1463. const [treeData, setTreeData] = useState(initialData);
  1464. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1465. function updateTreeData(list, key, children) {
  1466. return list.map(node => {
  1467. if (node.key === key) {
  1468. return { ...node, children };
  1469. }
  1470. if (node.children) {
  1471. return { ...node, children: updateTreeData(node.children, key, children) };
  1472. }
  1473. return node;
  1474. });
  1475. }
  1476. function onLoadData({ key, children }) {
  1477. return new Promise(resolve => {
  1478. if (children) {
  1479. resolve();
  1480. return;
  1481. }
  1482. setTimeout(() => {
  1483. setTreeData(origin =>
  1484. updateTreeData(origin, key, [
  1485. {
  1486. label: 'Child Node',
  1487. key: `${key}-0`,
  1488. },
  1489. {
  1490. label: 'Child Node',
  1491. key: `${key}-1`,
  1492. },
  1493. ]),
  1494. );
  1495. resolve();
  1496. }, 1000);
  1497. });
  1498. }
  1499. return (
  1500. <TreeSelect
  1501. loadData={onLoadData}
  1502. filterTreeNode
  1503. treeData={treeData}
  1504. style={{ width: 300 }}
  1505. placeholder="请选择"
  1506. />
  1507. );
  1508. }
  1509. export const loadDataAndLoadedkeys = () => {
  1510. const initialData = [
  1511. {
  1512. label: 'Expand to load',
  1513. value: '0',
  1514. key: '0',
  1515. },
  1516. {
  1517. label: 'Expand to load',
  1518. value: '1',
  1519. key: '1',
  1520. },
  1521. {
  1522. label: 'Leaf Node',
  1523. value: '2',
  1524. key: '2',
  1525. isLeaf: true,
  1526. },
  1527. ];
  1528. const [treeData, setTreeData] = useState(initialData);
  1529. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1530. function updateTreeData(list, key, children) {
  1531. return list.map(node => {
  1532. if (node.key === key) {
  1533. return { ...node, children };
  1534. }
  1535. if (node.children) {
  1536. return { ...node, children: updateTreeData(node.children, key, children) };
  1537. }
  1538. return node;
  1539. });
  1540. }
  1541. function updateLoadedKeys(key) {
  1542. if(!loadedKeys.includes(key)){
  1543. setLoadedKeys([...loadedKeys, key]);
  1544. console.log('[...loadedKeys, key]', [...loadedKeys, key]);
  1545. }
  1546. }
  1547. function onLoadData({ key, children }) {
  1548. return new Promise(resolve => {
  1549. if (children) {
  1550. resolve();
  1551. return;
  1552. }
  1553. setTimeout(() => {
  1554. setTreeData(origin =>
  1555. updateTreeData(origin, key, [
  1556. {
  1557. label: 'Child Node',
  1558. key: `${key}-0`,
  1559. },
  1560. {
  1561. label: 'Child Node',
  1562. key: `${key}-1`,
  1563. },
  1564. ]),
  1565. );
  1566. // updateLoadedKeys(key);
  1567. resolve();
  1568. }, 1000);
  1569. });
  1570. }
  1571. return (
  1572. <TreeSelect
  1573. loadData={onLoadData}
  1574. filterTreeNode
  1575. // loadedKeys={loadedKeys}
  1576. treeData={treeData}
  1577. style={{ width: 300 }}
  1578. placeholder="请选择"
  1579. />
  1580. );
  1581. }