treeSelect.stories.jsx 41 KB

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