treeSelect.stories.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. defaultOpen
  455. defaultExpandAll
  456. motion={false}
  457. multiple
  458. placeholder="Please select"
  459. />
  460. </div>
  461. );
  462. Multiple.story = {
  463. name: 'multiple',
  464. };
  465. export const MaxTagCount = () => (
  466. <div>
  467. <TreeSelect
  468. style={{ width: 300 }}
  469. // value={this.state.value}
  470. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  471. treeData={treeData2}
  472. multiple
  473. maxTagCount={2}
  474. placeholder="Please select"
  475. />
  476. {/* <br />
  477. <br />
  478. <TreeSelect
  479. style={{ width: 300 }}
  480. // value={this.state.value}
  481. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  482. treeData={treeData2}
  483. multiple
  484. placeholder="Please select"
  485. /> */}
  486. </div>
  487. );
  488. MaxTagCount.story = {
  489. name: 'maxTagCount',
  490. };
  491. export const MultipleSearchable = () => (
  492. <div>
  493. <TreeSelect
  494. style={{ width: 300 }}
  495. // value={this.state.value}
  496. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  497. treeData={treeData2}
  498. multiple
  499. filterTreeNode
  500. treeNodeFilterProp="value"
  501. placeholder="Please select"
  502. />
  503. </div>
  504. );
  505. MultipleSearchable.story = {
  506. name: 'multiple searchable',
  507. };
  508. export const DefaultValues = () => (
  509. <div>
  510. <TreeSelect
  511. style={{ width: 300 }}
  512. defaultValue={'shanghai'}
  513. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  514. treeData={treeData2}
  515. // multiple
  516. filterTreeNode
  517. treeNodeFilterProp="value"
  518. />
  519. <br />
  520. <br />
  521. <TreeSelect
  522. style={{ width: 300 }}
  523. defaultValue={'shanghai'}
  524. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  525. treeData={treeData2}
  526. filterTreeNode
  527. multiple
  528. treeNodeFilterProp="value"
  529. />
  530. <br />
  531. <br />
  532. <TreeSelect
  533. style={{ width: 300 }}
  534. defaultValue={['shanghai', 'daban', 'dongjing']}
  535. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  536. treeData={treeData2}
  537. filterTreeNode
  538. multiple
  539. treeNodeFilterProp="value"
  540. />
  541. <br />
  542. <br />
  543. <TreeSelect
  544. style={{ width: 300 }}
  545. defaultValue={['meiguo', 'dongjing']}
  546. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  547. treeData={treeData2}
  548. filterTreeNode
  549. multiple
  550. treeNodeFilterProp="value"
  551. />
  552. </div>
  553. );
  554. DefaultValues.story = {
  555. name: 'default values',
  556. };
  557. export const Disabled = () => (
  558. <div>
  559. <TreeSelect
  560. style={{ width: 300 }}
  561. defaultValue={'shanghai'}
  562. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  563. treeData={treeData2}
  564. multiple
  565. filterTreeNode
  566. disabled
  567. treeNodeFilterProp="value"
  568. placeholder="Please select"
  569. />
  570. <br />
  571. <br />
  572. <TreeSelect
  573. style={{ width: 300 }}
  574. defaultValue={'shanghai'}
  575. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  576. treeData={treeData2}
  577. filterTreeNode
  578. disabled
  579. treeNodeFilterProp="value"
  580. placeholder="Please select"
  581. />
  582. <br />
  583. <br />
  584. <TreeSelect
  585. style={{ width: 300 }}
  586. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  587. treeData={treeData2}
  588. multiple
  589. filterTreeNode
  590. disabled
  591. treeNodeFilterProp="value"
  592. placeholder="Please select"
  593. />
  594. </div>
  595. );
  596. Disabled.story = {
  597. name: 'disabled',
  598. };
  599. Disabled.parameters = {
  600. chromatic: { disableSnapshot: false },
  601. }
  602. export const OptionLabelProp = () => (
  603. <>
  604. <TreeSelect
  605. style={{ width: 300 }}
  606. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  607. treeData={treeData2}
  608. multiple
  609. filterTreeNode
  610. treeNodeFilterProp="value"
  611. treeNodeLabelProp="value"
  612. placeholder="Please select"
  613. />
  614. </>
  615. );
  616. OptionLabelProp.story = {
  617. name: 'optionLabelProp',
  618. };
  619. export const ValueInArray = () => (
  620. <>
  621. <TreeSelect
  622. style={{ width: 300 }}
  623. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  624. treeData={treeData2}
  625. multiple
  626. filterTreeNode
  627. valueInArray
  628. onChange={(...args) => console.log(args)}
  629. treeNodeFilterProp="value"
  630. treeNodeLabelProp="value"
  631. placeholder="Please select"
  632. />
  633. </>
  634. );
  635. ValueInArray.story = {
  636. name: 'valueInArray',
  637. };
  638. export const OnBlurOnFocus = () => (
  639. <>
  640. <div>single</div>
  641. <TreeSelect
  642. style={{ width: 300 }}
  643. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  644. treeData={treeData2}
  645. onBlur={(...args) => console.log('blur', args)}
  646. onFocus={(...args) => console.log('focus', args)}
  647. placeholder="Please select"
  648. />
  649. <div>multiple</div>
  650. <TreeSelect
  651. style={{ width: 300 }}
  652. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  653. treeData={treeData2}
  654. multiple
  655. onBlur={(...args) => console.log('blur', args)}
  656. onFocus={(...args) => console.log('focus', args)}
  657. placeholder="Please select"
  658. />
  659. </>
  660. );
  661. OnBlurOnFocus.story = {
  662. name: 'onBlur/onFocus',
  663. };
  664. export const LeafOnly = () => (
  665. <div>
  666. <TreeSelect
  667. style={{ width: 300 }}
  668. // value={this.state.value}
  669. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  670. treeData={treeData2}
  671. multiple
  672. leafOnly
  673. placeholder="Please select"
  674. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  675. onSelect={(e, bool) => console.log('select', e, bool)}
  676. onChange={e => console.log('change', e)}
  677. />
  678. </div>
  679. );
  680. LeafOnly.story = {
  681. name: 'leafOnly',
  682. };
  683. class Demo extends React.Component {
  684. constructor() {
  685. super();
  686. this.state = {
  687. value: 'shanghai',
  688. };
  689. }
  690. onChange(value) {
  691. this.setState({ value });
  692. }
  693. render() {
  694. return (
  695. <TreeSelect
  696. style={{ width: 300 }}
  697. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  698. treeData={treeData2}
  699. value={this.state.value}
  700. placeholder="Please select"
  701. onChange={e => this.onChange(e)}
  702. />
  703. );
  704. }
  705. }
  706. export const ControlledComponentSingle = () => <Demo />;
  707. ControlledComponentSingle.story = {
  708. name: 'controlled Component single',
  709. };
  710. class Demo2 extends React.Component {
  711. constructor() {
  712. super();
  713. this.state = {
  714. value: ['Shanghai'],
  715. };
  716. }
  717. onChange(value) {
  718. this.setState({ value });
  719. }
  720. render() {
  721. console.log(this.state.value);
  722. const treeData = [
  723. {
  724. label: '亚洲',
  725. value: 'Asia',
  726. key: '0',
  727. children: [
  728. {
  729. label: '中国',
  730. value: 'China',
  731. key: '0-0',
  732. children: [
  733. {
  734. label: '北京',
  735. value: 'Beijing',
  736. key: '0-0-0',
  737. },
  738. {
  739. label: '上海',
  740. value: 'Shanghai',
  741. key: '0-0-1',
  742. },
  743. ],
  744. },
  745. ],
  746. },
  747. {
  748. label: '北美洲',
  749. value: 'North America',
  750. key: '1',
  751. },
  752. ];
  753. return (
  754. <TreeSelect
  755. style={{ width: 300 }}
  756. multiple
  757. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  758. treeData={treeData}
  759. value={this.state.value}
  760. placeholder="Please select"
  761. onChange={e => this.onChange(e)}
  762. />
  763. );
  764. }
  765. }
  766. export const ControlledComponentMultiple = () => <Demo2 />;
  767. ControlledComponentMultiple.story = {
  768. name: 'controlled Component multiple',
  769. };
  770. class Demo3 extends React.Component {
  771. constructor() {
  772. super();
  773. this.state = {
  774. value: ['Shanghai'],
  775. };
  776. }
  777. // 获取最底层值
  778. getDeepChildrensByNode = node => {
  779. return flattenDeep(
  780. node.map(item => {
  781. if (item.children) {
  782. return this.getDeepChildrensByNode(item.children);
  783. }
  784. return item.value;
  785. })
  786. );
  787. };
  788. onChange(value, node) {
  789. console.log('onchange', value);
  790. value = this.getDeepChildrensByNode(node);
  791. console.log('modifiled', value);
  792. this.setState({ value });
  793. }
  794. render() {
  795. const treeData = [
  796. {
  797. label: '亚洲',
  798. value: 'Asia',
  799. key: '0',
  800. children: [
  801. {
  802. label: '中国',
  803. value: 'China',
  804. key: '0-0',
  805. children: [
  806. {
  807. label: '北京',
  808. value: 'Beijing',
  809. key: '0-0-0',
  810. },
  811. {
  812. label: '上海',
  813. value: 'Shanghai',
  814. key: '0-0-1',
  815. },
  816. ],
  817. },
  818. ],
  819. },
  820. {
  821. label: '北美洲',
  822. value: 'North America',
  823. key: '1',
  824. },
  825. ];
  826. return (
  827. <TreeSelect
  828. style={{ width: 300 }}
  829. multiple
  830. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  831. treeData={treeData}
  832. value={this.state.value}
  833. placeholder="Please select"
  834. onChange={(e, node) => this.onChange(e, node)}
  835. />
  836. );
  837. }
  838. }
  839. export const ControlledComponentMultipleValueModified = () => <Demo3 />;
  840. ControlledComponentMultipleValueModified.story = {
  841. name: 'controlled Component multiple value modified',
  842. };
  843. class ConvertDemo extends React.Component {
  844. constructor(props) {
  845. super(props);
  846. this.formApi = null;
  847. }
  848. handleChange = val => {
  849. let finalVal = val;
  850. let firstClassOption = ['Asia', 'North America'];
  851. // 在这里去做你的value替换逻辑
  852. console.log('originVal:' + val);
  853. if (val.length === 1) {
  854. // do nothing
  855. } else {
  856. if (val.every(item => firstClassOption.includes(item))) {
  857. finalVal = val[val.length - 1];
  858. }
  859. }
  860. console.log('finalVal:' + finalVal);
  861. return finalVal;
  862. };
  863. render() {
  864. const treeData = [
  865. {
  866. label: '亚洲',
  867. value: 'Asia',
  868. key: '0',
  869. children: [
  870. {
  871. label: '中国',
  872. value: 'China',
  873. key: '0-0',
  874. children: [
  875. {
  876. label: '北京',
  877. value: 'Beijing',
  878. key: '0-0-0',
  879. },
  880. {
  881. label: '上海',
  882. value: 'Shanghai',
  883. key: '0-0-1',
  884. },
  885. ],
  886. },
  887. ],
  888. },
  889. {
  890. label: '北美洲',
  891. value: 'North America',
  892. key: '1',
  893. },
  894. ];
  895. return (
  896. <Form getFormApi={this.getFormApi}>
  897. <Form.TreeSelect
  898. field="tree"
  899. label="节点(TreeSelect)"
  900. placeholder="请选择服务节点"
  901. treeData={treeData}
  902. convert={this.handleChange}
  903. filterTreeNode
  904. multiple
  905. ></Form.TreeSelect>
  906. </Form>
  907. );
  908. }
  909. }
  910. export const _ConvertDemo = () => <ConvertDemo />;
  911. _ConvertDemo.story = {
  912. name: 'convert demo',
  913. };
  914. export const TreeselectDefaultOpenInPopover = () => (
  915. <Popover
  916. content={
  917. <TreeSelect
  918. style={{ width: 200 }}
  919. dropdownStyle={{ width: 200 }}
  920. treeData={treeData1}
  921. defaultOpen
  922. // value={this.state.value}
  923. // placeholder="Please select"
  924. // onChange={(e, node) => this.onChange(e, node)}
  925. />
  926. }
  927. >
  928. <Button style={{ marginLeft: 150 }}>悬停此处</Button>
  929. </Popover>
  930. );
  931. TreeselectDefaultOpenInPopover.story = {
  932. name: 'treeselect defaultOpen in popover',
  933. };
  934. export const CustomTriggerDemo = () => <CustomTrigger />;
  935. CustomTriggerDemo.story = { name: 'custom trigger' };
  936. const AutoParentDemo = () => {
  937. const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
  938. const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
  939. const [autoExpandParent, setAutoExpandParent] = useState(true);
  940. const onExpand = expandedKeys => {
  941. console.log('onExpand', expandedKeys);
  942. // if not set autoExpandParent to false, if children expanded, parent can not collapse.
  943. // or, you can remove all expanded children keys.
  944. setExpandedKeys(expandedKeys);
  945. setAutoExpandParent(false);
  946. };
  947. const onSelect = (selectedKeys, info) => {
  948. console.log('onSelect:', info);
  949. setSelectedKeys(selectedKeys);
  950. };
  951. return (
  952. <div>
  953. <Button
  954. onClick={() => {
  955. setSelectedKeys(['beijing']);
  956. setExpandedKeys(['beijing']);
  957. setAutoExpandParent(true);
  958. }}
  959. >
  960. Update
  961. </Button>
  962. <TreeSelect
  963. onExpand={onExpand}
  964. expandedKeys={expandedKeys}
  965. autoExpandParent={autoExpandParent}
  966. onChange={onSelect}
  967. value={selectedKeys}
  968. treeData={treeData2}
  969. // multiple
  970. />
  971. </div>
  972. );
  973. };
  974. export const AutoExpandParent = () => (
  975. <>
  976. <AutoParentDemo />
  977. </>
  978. );
  979. AutoExpandParent.story = {
  980. name: 'autoExpandParent',
  981. };
  982. export const TreeWithoutValueProps = () => (
  983. <TreeSelect
  984. treeData={treeDataWithoutValue}
  985. value="beijing"
  986. defaultExpandAll
  987. onChange={(...args) => console.log(args)}
  988. />
  989. );
  990. TreeWithoutValueProps.story = {
  991. name: 'tree without value props',
  992. };
  993. export const TreeSelectRenderSelectedItem = () => {
  994. const treeData = [
  995. {
  996. label: '亚洲',
  997. value: 'Asia',
  998. key: '0',
  999. children: [
  1000. {
  1001. label: '中国',
  1002. value: 'China',
  1003. key: '0-0',
  1004. children: [
  1005. {
  1006. label: '北京',
  1007. value: 'Beijing',
  1008. key: '0-0-0',
  1009. },
  1010. {
  1011. label: '上海',
  1012. value: 'Shanghai',
  1013. key: '0-0-1',
  1014. },
  1015. ],
  1016. },
  1017. ],
  1018. },
  1019. {
  1020. label: '北美洲',
  1021. value: 'North America',
  1022. key: '1',
  1023. },
  1024. {
  1025. label: '南美洲',
  1026. value: 'South America',
  1027. key: '2',
  1028. },
  1029. {
  1030. label: '南极洲',
  1031. value: 'Antarctica',
  1032. key: '3',
  1033. },
  1034. ];
  1035. return (
  1036. <>
  1037. <h4>单选</h4>
  1038. <TreeSelect
  1039. style={{ width: 300 }}
  1040. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1041. treeData={treeData}
  1042. placeholder="请选择"
  1043. renderSelectedItem={item => `${item.value}-${item.label}`}
  1044. />
  1045. <h4>多选</h4>
  1046. <TreeSelect
  1047. style={{ width: 300 }}
  1048. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1049. treeData={treeData}
  1050. multiple
  1051. placeholder="请选择"
  1052. renderSelectedItem={(item, { index, onClose }) => ({
  1053. content: `${item.value}-${item.label}`,
  1054. isRenderInTag: true,
  1055. })}
  1056. />
  1057. <h4>多选 + isRenderInTag=false</h4>
  1058. <TreeSelect
  1059. style={{ width: 300 }}
  1060. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1061. treeData={treeData}
  1062. multiple
  1063. maxTagCount={2}
  1064. placeholder="请选择"
  1065. renderSelectedItem={(item, { index, onClose }) => ({
  1066. content: (
  1067. <Tag key={index} color="white">
  1068. {item.value}
  1069. </Tag>
  1070. ),
  1071. isRenderInTag: false,
  1072. })}
  1073. />
  1074. </>
  1075. );
  1076. };
  1077. TreeSelectRenderSelectedItem.story = {
  1078. name: 'treeSelect renderSelectedItem',
  1079. };
  1080. const DisableStrictlyDemo = () => {
  1081. const [value, setValue] = useState(['Shanghai']);
  1082. const treeData = [
  1083. {
  1084. label: '亚洲',
  1085. value: 'Asia',
  1086. key: '0',
  1087. children: [
  1088. {
  1089. label: '中国',
  1090. value: 'China',
  1091. key: '0-0',
  1092. disabled: true,
  1093. children: [
  1094. {
  1095. label: '北京',
  1096. value: 'Beijing',
  1097. key: '0-0-0',
  1098. },
  1099. {
  1100. label: '上海',
  1101. value: 'Shanghai',
  1102. key: '0-0-1',
  1103. },
  1104. ],
  1105. },
  1106. {
  1107. label: '日本',
  1108. value: 'Japan',
  1109. key: '0-1',
  1110. },
  1111. ],
  1112. },
  1113. {
  1114. label: '北美洲',
  1115. value: 'North America',
  1116. key: '1',
  1117. },
  1118. ];
  1119. return (
  1120. <div>
  1121. <TreeSelect
  1122. style={{ width: 300 }}
  1123. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1124. treeData={treeData}
  1125. disableStrictly
  1126. multiple
  1127. searchPosition="trigger"
  1128. filterTreeNode
  1129. value={value}
  1130. onChange={value => setValue(value)}
  1131. />
  1132. </div>
  1133. );
  1134. };
  1135. export const DisabledStrictly = () => (
  1136. <>
  1137. <DisableStrictlyDemo />
  1138. </>
  1139. );
  1140. DisabledStrictly.story = {
  1141. name: 'disabledStrictly',
  1142. };
  1143. export const CheckRelationDemo = () => {
  1144. const treeData = [
  1145. {
  1146. label: 'Asia',
  1147. value: 'Asia',
  1148. key: '0',
  1149. children: [
  1150. {
  1151. label: 'China',
  1152. value: 'China',
  1153. key: '0-0',
  1154. children: [
  1155. {
  1156. label: 'Beijing',
  1157. value: 'Beijing',
  1158. key: '0-0-0',
  1159. },
  1160. {
  1161. label: 'Shanghai',
  1162. value: 'Shanghai',
  1163. key: '0-0-1',
  1164. },
  1165. {
  1166. label: 'Chengdu',
  1167. value: 'Chengdu',
  1168. key: '0-0-2',
  1169. },
  1170. ],
  1171. },
  1172. {
  1173. label: 'Japan',
  1174. value: 'Japan',
  1175. key: '0-1',
  1176. children: [
  1177. {
  1178. label: 'Osaka',
  1179. value: 'Osaka',
  1180. key: '0-1-0'
  1181. }
  1182. ]
  1183. },
  1184. ],
  1185. },
  1186. {
  1187. label: 'North America',
  1188. value: 'North America',
  1189. key: '1',
  1190. children: [
  1191. {
  1192. label: 'United States',
  1193. value: 'United States',
  1194. key: '1-0'
  1195. },
  1196. {
  1197. label: 'Canada',
  1198. value: 'Canada',
  1199. key: '1-1'
  1200. }
  1201. ]
  1202. }
  1203. ];
  1204. const [value, setValue] = useState('China');
  1205. const [value2, setValue2] = useState();
  1206. const [value3, setValue3] = useState();
  1207. const style = {
  1208. width: 300,
  1209. };
  1210. const dropdownStyle = {
  1211. maxHeight: 400,
  1212. overflow: 'auto'
  1213. };
  1214. const handleChange = value => {
  1215. console.log(value);
  1216. setValue(value);
  1217. };
  1218. const handleChange2 = value => {
  1219. console.log(value);
  1220. setValue2(value);
  1221. };
  1222. const handleChange3 = value => {
  1223. console.log(value);
  1224. setValue3(value);
  1225. };
  1226. return (
  1227. <>
  1228. <div>checkRelation='unRelated'</div>
  1229. <TreeSelect
  1230. dropdownStyle={dropdownStyle}
  1231. treeData={treeData}
  1232. multiple
  1233. checkRelation='unRelated'
  1234. defaultExpandAll
  1235. style={style}
  1236. />
  1237. <br /><br />
  1238. <div>checkRelation='unRelated' + maxTagCount=2</div>
  1239. <TreeSelect
  1240. dropdownStyle={dropdownStyle}
  1241. treeData={treeData}
  1242. multiple
  1243. maxTagCount={2}
  1244. checkRelation='unRelated'
  1245. defaultExpandAll
  1246. style={style}
  1247. />
  1248. <br /><br />
  1249. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索</div>
  1250. <TreeSelect
  1251. dropdownStyle={dropdownStyle}
  1252. treeData={treeData}
  1253. multiple
  1254. maxTagCount={2}
  1255. filterTreeNode
  1256. checkRelation='unRelated'
  1257. defaultExpandAll
  1258. style={style}
  1259. />
  1260. <br /><br />
  1261. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索 + searchBox in trigger</div>
  1262. <TreeSelect
  1263. dropdownStyle={dropdownStyle}
  1264. treeData={treeData}
  1265. multiple
  1266. maxTagCount={2}
  1267. filterTreeNode
  1268. checkRelation='unRelated'
  1269. searchPosition='trigger'
  1270. defaultExpandAll
  1271. style={style}
  1272. />
  1273. <br /><br />
  1274. <div>checkRelation='unRelated' + 中国节点为 disabled</div>
  1275. <TreeSelect
  1276. dropdownStyle={dropdownStyle}
  1277. treeData={treeDataWithoutValue}
  1278. multiple
  1279. checkRelation='unRelated'
  1280. defaultExpandAll
  1281. style={style}
  1282. />
  1283. <br /><br />
  1284. <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
  1285. <TreeSelect
  1286. dropdownStyle={dropdownStyle}
  1287. treeData={treeDataWithoutValue}
  1288. multiple
  1289. checkRelation='unRelated'
  1290. defaultExpandAll
  1291. disableStrictly
  1292. style={style}
  1293. />
  1294. <br /><br />
  1295. <div>checkRelation='unRelated' + defaultValue 为 China</div>
  1296. <TreeSelect
  1297. dropdownStyle={dropdownStyle}
  1298. treeData={treeData}
  1299. multiple
  1300. checkRelation='unRelated'
  1301. defaultExpandAll
  1302. style={style}
  1303. defaultValue='China'
  1304. />
  1305. <br /><br />
  1306. <div>checkRelation='unRelated' + defaultValue 为 China + 开启搜索</div>
  1307. <TreeSelect
  1308. dropdownStyle={dropdownStyle}
  1309. treeData={treeData}
  1310. multiple
  1311. filterTreeNode
  1312. checkRelation='unRelated'
  1313. defaultExpandAll
  1314. style={style}
  1315. defaultValue='China'
  1316. />
  1317. <br /><br />
  1318. <div>多选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1319. <TreeSelect
  1320. dropdownStyle={dropdownStyle}
  1321. treeData={treeData}
  1322. multiple
  1323. filterTreeNode
  1324. showClear
  1325. checkRelation='unRelated'
  1326. defaultExpandAll
  1327. style={style}
  1328. searchPosition='trigger'
  1329. defaultValue={['China', 'Japan']}
  1330. />
  1331. <br /><br />
  1332. <div>单选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1333. <TreeSelect
  1334. dropdownStyle={dropdownStyle}
  1335. treeData={treeData}
  1336. filterTreeNode
  1337. showClear
  1338. checkRelation='unRelated'
  1339. defaultExpandAll
  1340. style={style}
  1341. searchPosition='trigger'
  1342. defaultValue='China'
  1343. />
  1344. <br /><br />
  1345. <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
  1346. <TreeSelect
  1347. dropdownStyle={dropdownStyle}
  1348. treeData={treeData}
  1349. multiple
  1350. checkRelation='unRelated'
  1351. defaultExpandAll
  1352. style={style}
  1353. value={value}
  1354. onChange={handleChange}
  1355. />
  1356. <br /><br />
  1357. <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
  1358. <TreeSelect
  1359. dropdownStyle={dropdownStyle}
  1360. treeData={treeData}
  1361. multiple
  1362. checkRelation='unRelated'
  1363. defaultExpandAll
  1364. style={style}
  1365. value={value2}
  1366. onChangeWithObject
  1367. onChange={handleChange2}
  1368. />
  1369. <br /><br />
  1370. <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
  1371. <TreeSelect
  1372. dropdownStyle={dropdownStyle}
  1373. leafOnly
  1374. treeData={treeData}
  1375. multiple
  1376. checkRelation='unRelated'
  1377. defaultExpandAll
  1378. style={style}
  1379. value={value3}
  1380. onChange={handleChange3}
  1381. />
  1382. <br /><br />
  1383. <div>checkRelation='unRelated' + onSelect </div>
  1384. <TreeSelect
  1385. dropdownStyle={dropdownStyle}
  1386. treeData={treeData}
  1387. multiple
  1388. checkRelation='unRelated'
  1389. defaultExpandAll
  1390. style={style}
  1391. onSelect={(value,status,node)=>console.log('select', value, status, node)}
  1392. />
  1393. </>
  1394. );
  1395. };
  1396. export const SearchableAndExpandedKeys = () => {
  1397. const [expandedKeys1, setExpandedKeys1] = useState([]);
  1398. const [expandedKeys2, setExpandedKeys2] = useState([]);
  1399. const [expandedKeys3, setExpandedKeys3] = useState([]);
  1400. return (
  1401. <>
  1402. <Title heading={6}>expandedKeys 受控</Title>
  1403. <TreeSelect
  1404. style={{ width: 300, marginBottom: 30 }}
  1405. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1406. treeData={treeData2}
  1407. expandedKeys={expandedKeys1}
  1408. defaultValue='beijing'
  1409. onExpand={v => {
  1410. console.log('onExpand value: ', v);
  1411. setExpandedKeys1(v);
  1412. }}
  1413. />
  1414. <Title heading={6}>expandedKeys 受控 + 开启搜索</Title>
  1415. <TreeSelect
  1416. style={{ width: 300, marginBottom: 30 }}
  1417. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1418. treeData={treeData2}
  1419. filterTreeNode
  1420. defaultValue='beijing'
  1421. expandedKeys={expandedKeys2}
  1422. onExpand={v => {
  1423. console.log('onExpand value: ', v);
  1424. setExpandedKeys2(v);
  1425. }}
  1426. />
  1427. <Title heading={6}>expandedKeys 受控 + 开启搜索 + 搜索时更新 expandedKeys</Title>
  1428. <TreeSelect
  1429. style={{ width: 300, marginBottom: 30 }}
  1430. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1431. treeData={treeData2}
  1432. filterTreeNode
  1433. expandedKeys={expandedKeys3}
  1434. defaultValue='beijing'
  1435. onExpand={v => {
  1436. console.log('onExpand value: ', v);
  1437. setExpandedKeys3(v)
  1438. }}
  1439. onSearch={(input, filterExpandedKeys) => {
  1440. console.log('onExpand filterExpandedKeys: ', filterExpandedKeys);
  1441. setExpandedKeys3(filterExpandedKeys);
  1442. }}
  1443. />
  1444. </>
  1445. )
  1446. }
  1447. export const loadData = () => {
  1448. const initialData = [
  1449. {
  1450. label: 'Expand to load',
  1451. value: '0',
  1452. key: '0',
  1453. },
  1454. {
  1455. label: 'Expand to load',
  1456. value: '1',
  1457. key: '1',
  1458. },
  1459. {
  1460. label: 'Leaf Node',
  1461. value: '2',
  1462. key: '2',
  1463. isLeaf: true,
  1464. },
  1465. ];
  1466. const [treeData, setTreeData] = useState(initialData);
  1467. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1468. function updateTreeData(list, key, children) {
  1469. return list.map(node => {
  1470. if (node.key === key) {
  1471. return { ...node, children };
  1472. }
  1473. if (node.children) {
  1474. return { ...node, children: updateTreeData(node.children, key, children) };
  1475. }
  1476. return node;
  1477. });
  1478. }
  1479. function onLoadData({ key, children }) {
  1480. return new Promise(resolve => {
  1481. if (children) {
  1482. resolve();
  1483. return;
  1484. }
  1485. setTimeout(() => {
  1486. setTreeData(origin =>
  1487. updateTreeData(origin, key, [
  1488. {
  1489. label: 'Child Node',
  1490. key: `${key}-0`,
  1491. },
  1492. {
  1493. label: 'Child Node',
  1494. key: `${key}-1`,
  1495. },
  1496. ]),
  1497. );
  1498. resolve();
  1499. }, 1000);
  1500. });
  1501. }
  1502. return (
  1503. <TreeSelect
  1504. loadData={onLoadData}
  1505. filterTreeNode
  1506. treeData={treeData}
  1507. style={{ width: 300 }}
  1508. placeholder="请选择"
  1509. />
  1510. );
  1511. }
  1512. export const loadDataAndLoadedkeys = () => {
  1513. const initialData = [
  1514. {
  1515. label: 'Expand to load',
  1516. value: '0',
  1517. key: '0',
  1518. },
  1519. {
  1520. label: 'Expand to load',
  1521. value: '1',
  1522. key: '1',
  1523. },
  1524. {
  1525. label: 'Leaf Node',
  1526. value: '2',
  1527. key: '2',
  1528. isLeaf: true,
  1529. },
  1530. ];
  1531. const [treeData, setTreeData] = useState(initialData);
  1532. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1533. function updateTreeData(list, key, children) {
  1534. return list.map(node => {
  1535. if (node.key === key) {
  1536. return { ...node, children };
  1537. }
  1538. if (node.children) {
  1539. return { ...node, children: updateTreeData(node.children, key, children) };
  1540. }
  1541. return node;
  1542. });
  1543. }
  1544. function updateLoadedKeys(key) {
  1545. if(!loadedKeys.includes(key)){
  1546. setLoadedKeys([...loadedKeys, key]);
  1547. console.log('[...loadedKeys, key]', [...loadedKeys, key]);
  1548. }
  1549. }
  1550. function onLoadData({ key, children }) {
  1551. return new Promise(resolve => {
  1552. if (children) {
  1553. resolve();
  1554. return;
  1555. }
  1556. setTimeout(() => {
  1557. setTreeData(origin =>
  1558. updateTreeData(origin, key, [
  1559. {
  1560. label: 'Child Node',
  1561. key: `${key}-0`,
  1562. },
  1563. {
  1564. label: 'Child Node',
  1565. key: `${key}-1`,
  1566. },
  1567. ]),
  1568. );
  1569. // updateLoadedKeys(key);
  1570. resolve();
  1571. }, 1000);
  1572. });
  1573. }
  1574. return (
  1575. <TreeSelect
  1576. loadData={onLoadData}
  1577. filterTreeNode
  1578. // loadedKeys={loadedKeys}
  1579. treeData={treeData}
  1580. style={{ width: 300 }}
  1581. placeholder="请选择"
  1582. />
  1583. );
  1584. }