treeSelect.stories.jsx 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. import React, { useState, useMemo } 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. <div>single, filterTreeNode, searchPosition=dropdown</div>
  725. <TreeSelect
  726. filterTreeNode
  727. style={{ width: 300 }}
  728. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  729. treeData={treeData2}
  730. onBlur={(...args) => console.log('blur', args)}
  731. onFocus={(...args) => console.log('focus', args)}
  732. placeholder="Please select"
  733. />
  734. <div>multiple, filterTreeNode, searchPosition=dropdown</div>
  735. <TreeSelect
  736. filterTreeNode
  737. style={{ width: 300 }}
  738. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  739. treeData={treeData2}
  740. multiple
  741. onBlur={(...args) => console.log('blur', args)}
  742. onFocus={(...args) => console.log('focus', args)}
  743. placeholder="Please select"
  744. />
  745. <div>single, filterTreeNode, searchPosition=trigger</div>
  746. <TreeSelect
  747. searchPosition="trigger"
  748. filterTreeNode
  749. style={{ width: 300 }}
  750. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  751. treeData={treeData2}
  752. onBlur={(...args) => console.log('blur', args)}
  753. onFocus={(...args) => console.log('focus', args)}
  754. placeholder="Please select"
  755. />
  756. <div>multiple, filterTreeNode, searchPosition=trigger</div>
  757. <TreeSelect
  758. searchPosition="trigger"
  759. filterTreeNode
  760. style={{ width: 300 }}
  761. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  762. treeData={treeData2}
  763. multiple
  764. onBlur={(...args) => console.log('blur', args)}
  765. onFocus={(...args) => console.log('focus', args)}
  766. placeholder="Please select"
  767. />
  768. </>
  769. );
  770. OnBlurOnFocus.story = {
  771. name: 'onBlur/onFocus',
  772. };
  773. export const LeafOnly = () => (
  774. <div>
  775. <TreeSelect
  776. style={{ width: 300 }}
  777. // value={this.state.value}
  778. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  779. treeData={treeData2}
  780. multiple
  781. leafOnly
  782. placeholder="Please select"
  783. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  784. onSelect={(e, bool) => console.log('select', e, bool)}
  785. onChange={e => console.log('change', e)}
  786. />
  787. </div>
  788. );
  789. LeafOnly.story = {
  790. name: 'leafOnly',
  791. };
  792. class Demo extends React.Component {
  793. constructor() {
  794. super();
  795. this.state = {
  796. value: 'shanghai',
  797. };
  798. }
  799. onChange(value) {
  800. this.setState({ value });
  801. }
  802. render() {
  803. return (
  804. <TreeSelect
  805. style={{ width: 300 }}
  806. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  807. treeData={treeData2}
  808. value={this.state.value}
  809. placeholder="Please select"
  810. onChange={e => this.onChange(e)}
  811. />
  812. );
  813. }
  814. }
  815. export const ControlledComponentSingle = () => <Demo />;
  816. ControlledComponentSingle.story = {
  817. name: 'controlled Component single',
  818. };
  819. class Demo2 extends React.Component {
  820. constructor() {
  821. super();
  822. this.state = {
  823. value: ['Shanghai'],
  824. };
  825. }
  826. onChange(value) {
  827. this.setState({ value });
  828. }
  829. render() {
  830. console.log(this.state.value);
  831. const treeData = [
  832. {
  833. label: '亚洲',
  834. value: 'Asia',
  835. key: '0',
  836. children: [
  837. {
  838. label: '中国',
  839. value: 'China',
  840. key: '0-0',
  841. children: [
  842. {
  843. label: '北京',
  844. value: 'Beijing',
  845. key: '0-0-0',
  846. },
  847. {
  848. label: '上海',
  849. value: 'Shanghai',
  850. key: '0-0-1',
  851. },
  852. ],
  853. },
  854. ],
  855. },
  856. {
  857. label: '北美洲',
  858. value: 'North America',
  859. key: '1',
  860. },
  861. ];
  862. return (
  863. <TreeSelect
  864. style={{ width: 300 }}
  865. multiple
  866. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  867. treeData={treeData}
  868. value={this.state.value}
  869. placeholder="Please select"
  870. onChange={e => this.onChange(e)}
  871. />
  872. );
  873. }
  874. }
  875. export const ControlledComponentMultiple = () => <Demo2 />;
  876. ControlledComponentMultiple.story = {
  877. name: 'controlled Component multiple',
  878. };
  879. class Demo3 extends React.Component {
  880. constructor() {
  881. super();
  882. this.state = {
  883. value: ['Shanghai'],
  884. };
  885. }
  886. // 获取最底层值
  887. getDeepChildrensByNode = node => {
  888. return flattenDeep(
  889. node.map(item => {
  890. if (item.children) {
  891. return this.getDeepChildrensByNode(item.children);
  892. }
  893. return item.value;
  894. })
  895. );
  896. };
  897. onChange(value, node) {
  898. console.log('onchange', value);
  899. value = this.getDeepChildrensByNode(node);
  900. console.log('modifiled', value);
  901. this.setState({ value });
  902. }
  903. render() {
  904. const treeData = [
  905. {
  906. label: '亚洲',
  907. value: 'Asia',
  908. key: '0',
  909. children: [
  910. {
  911. label: '中国',
  912. value: 'China',
  913. key: '0-0',
  914. children: [
  915. {
  916. label: '北京',
  917. value: 'Beijing',
  918. key: '0-0-0',
  919. },
  920. {
  921. label: '上海',
  922. value: 'Shanghai',
  923. key: '0-0-1',
  924. },
  925. ],
  926. },
  927. ],
  928. },
  929. {
  930. label: '北美洲',
  931. value: 'North America',
  932. key: '1',
  933. },
  934. ];
  935. return (
  936. <TreeSelect
  937. style={{ width: 300 }}
  938. multiple
  939. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  940. treeData={treeData}
  941. value={this.state.value}
  942. placeholder="Please select"
  943. onChange={(e, node) => this.onChange(e, node)}
  944. />
  945. );
  946. }
  947. }
  948. export const ControlledComponentMultipleValueModified = () => <Demo3 />;
  949. ControlledComponentMultipleValueModified.story = {
  950. name: 'controlled Component multiple value modified',
  951. };
  952. class ConvertDemo extends React.Component {
  953. constructor(props) {
  954. super(props);
  955. this.formApi = null;
  956. }
  957. handleChange = val => {
  958. let finalVal = val;
  959. let firstClassOption = ['Asia', 'North America'];
  960. // 在这里去做你的value替换逻辑
  961. console.log('originVal:' + val);
  962. if (val.length === 1) {
  963. // do nothing
  964. } else {
  965. if (val.every(item => firstClassOption.includes(item))) {
  966. finalVal = val[val.length - 1];
  967. }
  968. }
  969. console.log('finalVal:' + finalVal);
  970. return finalVal;
  971. };
  972. render() {
  973. const treeData = [
  974. {
  975. label: '亚洲',
  976. value: 'Asia',
  977. key: '0',
  978. children: [
  979. {
  980. label: '中国',
  981. value: 'China',
  982. key: '0-0',
  983. children: [
  984. {
  985. label: '北京',
  986. value: 'Beijing',
  987. key: '0-0-0',
  988. },
  989. {
  990. label: '上海',
  991. value: 'Shanghai',
  992. key: '0-0-1',
  993. },
  994. ],
  995. },
  996. ],
  997. },
  998. {
  999. label: '北美洲',
  1000. value: 'North America',
  1001. key: '1',
  1002. },
  1003. ];
  1004. return (
  1005. <Form getFormApi={this.getFormApi}>
  1006. <Form.TreeSelect
  1007. field="tree"
  1008. label="节点(TreeSelect)"
  1009. placeholder="请选择服务节点"
  1010. treeData={treeData}
  1011. convert={this.handleChange}
  1012. filterTreeNode
  1013. multiple
  1014. ></Form.TreeSelect>
  1015. </Form>
  1016. );
  1017. }
  1018. }
  1019. export const _ConvertDemo = () => <ConvertDemo />;
  1020. _ConvertDemo.story = {
  1021. name: 'convert demo',
  1022. };
  1023. export const TreeselectDefaultOpenInPopover = () => (
  1024. <Popover
  1025. content={
  1026. <TreeSelect
  1027. style={{ width: 200 }}
  1028. dropdownStyle={{ width: 200 }}
  1029. treeData={treeData1}
  1030. defaultOpen
  1031. // value={this.state.value}
  1032. // placeholder="Please select"
  1033. // onChange={(e, node) => this.onChange(e, node)}
  1034. />
  1035. }
  1036. >
  1037. <Button style={{ marginLeft: 150 }}>悬停此处</Button>
  1038. </Popover>
  1039. );
  1040. TreeselectDefaultOpenInPopover.story = {
  1041. name: 'treeselect defaultOpen in popover',
  1042. };
  1043. export const CustomTriggerDemo = () => <CustomTrigger />;
  1044. CustomTriggerDemo.story = { name: 'custom trigger' };
  1045. const AutoParentDemo = () => {
  1046. const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
  1047. const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
  1048. const [autoExpandParent, setAutoExpandParent] = useState(true);
  1049. const onExpand = expandedKeys => {
  1050. console.log('onExpand', expandedKeys);
  1051. // if not set autoExpandParent to false, if children expanded, parent can not collapse.
  1052. // or, you can remove all expanded children keys.
  1053. setExpandedKeys(expandedKeys);
  1054. setAutoExpandParent(false);
  1055. };
  1056. const onSelect = (selectedKeys, info) => {
  1057. console.log('onSelect:', info);
  1058. setSelectedKeys(selectedKeys);
  1059. };
  1060. return (
  1061. <div>
  1062. <Button
  1063. onClick={() => {
  1064. setSelectedKeys(['beijing']);
  1065. setExpandedKeys(['beijing']);
  1066. setAutoExpandParent(true);
  1067. }}
  1068. >
  1069. Update
  1070. </Button>
  1071. <TreeSelect
  1072. onExpand={onExpand}
  1073. expandedKeys={expandedKeys}
  1074. autoExpandParent={autoExpandParent}
  1075. onChange={onSelect}
  1076. value={selectedKeys}
  1077. treeData={treeData2}
  1078. // multiple
  1079. />
  1080. </div>
  1081. );
  1082. };
  1083. export const AutoExpandParent = () => (
  1084. <>
  1085. <AutoParentDemo />
  1086. </>
  1087. );
  1088. AutoExpandParent.story = {
  1089. name: 'autoExpandParent',
  1090. };
  1091. export const TreeWithoutValueProps = () => (
  1092. <TreeSelect
  1093. treeData={treeDataWithoutValue}
  1094. value="beijing"
  1095. defaultExpandAll
  1096. onChange={(...args) => console.log(args)}
  1097. />
  1098. );
  1099. TreeWithoutValueProps.story = {
  1100. name: 'tree without value props',
  1101. };
  1102. export const TreeSelectRenderSelectedItem = () => {
  1103. const treeData = [
  1104. {
  1105. label: '亚洲',
  1106. value: 'Asia',
  1107. key: '0',
  1108. children: [
  1109. {
  1110. label: '中国',
  1111. value: 'China',
  1112. key: '0-0',
  1113. children: [
  1114. {
  1115. label: '北京',
  1116. value: 'Beijing',
  1117. key: '0-0-0',
  1118. },
  1119. {
  1120. label: '上海',
  1121. value: 'Shanghai',
  1122. key: '0-0-1',
  1123. },
  1124. ],
  1125. },
  1126. ],
  1127. },
  1128. {
  1129. label: '北美洲',
  1130. value: 'North America',
  1131. key: '1',
  1132. },
  1133. {
  1134. label: '南美洲',
  1135. value: 'South America',
  1136. key: '2',
  1137. },
  1138. {
  1139. label: '南极洲',
  1140. value: 'Antarctica',
  1141. key: '3',
  1142. },
  1143. ];
  1144. return (
  1145. <>
  1146. <h4>单选</h4>
  1147. <TreeSelect
  1148. style={{ width: 300 }}
  1149. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1150. treeData={treeData}
  1151. placeholder="请选择"
  1152. renderSelectedItem={item => `${item.value}-${item.label}`}
  1153. />
  1154. <h4>多选</h4>
  1155. <TreeSelect
  1156. style={{ width: 300 }}
  1157. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1158. treeData={treeData}
  1159. multiple
  1160. placeholder="请选择"
  1161. renderSelectedItem={(item, { index, onClose }) => ({
  1162. content: `${item.value}-${item.label}`,
  1163. isRenderInTag: true,
  1164. })}
  1165. />
  1166. <h4>多选 + isRenderInTag=false</h4>
  1167. <TreeSelect
  1168. style={{ width: 300 }}
  1169. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1170. treeData={treeData}
  1171. multiple
  1172. maxTagCount={2}
  1173. placeholder="请选择"
  1174. renderSelectedItem={(item, { index, onClose }) => ({
  1175. content: (
  1176. <Tag key={index} color="white">
  1177. {item.value}
  1178. </Tag>
  1179. ),
  1180. isRenderInTag: false,
  1181. })}
  1182. />
  1183. </>
  1184. );
  1185. };
  1186. TreeSelectRenderSelectedItem.story = {
  1187. name: 'treeSelect renderSelectedItem',
  1188. };
  1189. const DisableStrictlyDemo = () => {
  1190. const [value, setValue] = useState(['Shanghai']);
  1191. const treeData = [
  1192. {
  1193. label: '亚洲',
  1194. value: 'Asia',
  1195. key: '0',
  1196. children: [
  1197. {
  1198. label: '中国',
  1199. value: 'China',
  1200. key: '0-0',
  1201. disabled: true,
  1202. children: [
  1203. {
  1204. label: '北京',
  1205. value: 'Beijing',
  1206. key: '0-0-0',
  1207. },
  1208. {
  1209. label: '上海',
  1210. value: 'Shanghai',
  1211. key: '0-0-1',
  1212. },
  1213. ],
  1214. },
  1215. {
  1216. label: '日本',
  1217. value: 'Japan',
  1218. key: '0-1',
  1219. },
  1220. ],
  1221. },
  1222. {
  1223. label: '北美洲',
  1224. value: 'North America',
  1225. key: '1',
  1226. },
  1227. ];
  1228. return (
  1229. <div>
  1230. <TreeSelect
  1231. style={{ width: 300 }}
  1232. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1233. treeData={treeData}
  1234. disableStrictly
  1235. multiple
  1236. searchPosition="trigger"
  1237. filterTreeNode
  1238. value={value}
  1239. onChange={value => setValue(value)}
  1240. />
  1241. </div>
  1242. );
  1243. };
  1244. export const DisabledStrictly = () => (
  1245. <>
  1246. <DisableStrictlyDemo />
  1247. </>
  1248. );
  1249. DisabledStrictly.story = {
  1250. name: 'disabledStrictly',
  1251. };
  1252. export const CheckRelationDemo = () => {
  1253. const treeData = [
  1254. {
  1255. label: 'Asia',
  1256. value: 'Asia',
  1257. key: '0',
  1258. children: [
  1259. {
  1260. label: 'China',
  1261. value: 'China',
  1262. key: '0-0',
  1263. children: [
  1264. {
  1265. label: 'Beijing',
  1266. value: 'Beijing',
  1267. key: '0-0-0',
  1268. },
  1269. {
  1270. label: 'Shanghai',
  1271. value: 'Shanghai',
  1272. key: '0-0-1',
  1273. },
  1274. {
  1275. label: 'Chengdu',
  1276. value: 'Chengdu',
  1277. key: '0-0-2',
  1278. },
  1279. ],
  1280. },
  1281. {
  1282. label: 'Japan',
  1283. value: 'Japan',
  1284. key: '0-1',
  1285. children: [
  1286. {
  1287. label: 'Osaka',
  1288. value: 'Osaka',
  1289. key: '0-1-0'
  1290. }
  1291. ]
  1292. },
  1293. ],
  1294. },
  1295. {
  1296. label: 'North America',
  1297. value: 'North America',
  1298. key: '1',
  1299. children: [
  1300. {
  1301. label: 'United States',
  1302. value: 'United States',
  1303. key: '1-0'
  1304. },
  1305. {
  1306. label: 'Canada',
  1307. value: 'Canada',
  1308. key: '1-1'
  1309. }
  1310. ]
  1311. }
  1312. ];
  1313. const [value, setValue] = useState('China');
  1314. const [value2, setValue2] = useState();
  1315. const [value3, setValue3] = useState();
  1316. const style = {
  1317. width: 300,
  1318. };
  1319. const dropdownStyle = {
  1320. maxHeight: 400,
  1321. overflow: 'auto'
  1322. };
  1323. const handleChange = value => {
  1324. console.log(value);
  1325. setValue(value);
  1326. };
  1327. const handleChange2 = value => {
  1328. console.log(value);
  1329. setValue2(value);
  1330. };
  1331. const handleChange3 = value => {
  1332. console.log(value);
  1333. setValue3(value);
  1334. };
  1335. return (
  1336. <>
  1337. <div>checkRelation='unRelated'</div>
  1338. <TreeSelect
  1339. dropdownStyle={dropdownStyle}
  1340. treeData={treeData}
  1341. multiple
  1342. checkRelation='unRelated'
  1343. defaultExpandAll
  1344. style={style}
  1345. />
  1346. <br /><br />
  1347. <div>checkRelation='unRelated' + maxTagCount=2</div>
  1348. <TreeSelect
  1349. dropdownStyle={dropdownStyle}
  1350. treeData={treeData}
  1351. multiple
  1352. maxTagCount={2}
  1353. checkRelation='unRelated'
  1354. defaultExpandAll
  1355. style={style}
  1356. />
  1357. <br /><br />
  1358. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索</div>
  1359. <TreeSelect
  1360. dropdownStyle={dropdownStyle}
  1361. treeData={treeData}
  1362. multiple
  1363. maxTagCount={2}
  1364. filterTreeNode
  1365. checkRelation='unRelated'
  1366. defaultExpandAll
  1367. style={style}
  1368. />
  1369. <br /><br />
  1370. <div>checkRelation='unRelated' + maxTagCount=2 + 开启搜索 + searchBox in trigger</div>
  1371. <TreeSelect
  1372. dropdownStyle={dropdownStyle}
  1373. treeData={treeData}
  1374. multiple
  1375. maxTagCount={2}
  1376. filterTreeNode
  1377. checkRelation='unRelated'
  1378. searchPosition='trigger'
  1379. defaultExpandAll
  1380. style={style}
  1381. />
  1382. <br /><br />
  1383. <div>checkRelation='unRelated' + 中国节点为 disabled</div>
  1384. <TreeSelect
  1385. dropdownStyle={dropdownStyle}
  1386. treeData={treeDataWithoutValue}
  1387. multiple
  1388. checkRelation='unRelated'
  1389. defaultExpandAll
  1390. style={style}
  1391. />
  1392. <br /><br />
  1393. <div>checkRelation='unRelated' + 中国节点为 disabled + 严格禁用</div>
  1394. <TreeSelect
  1395. dropdownStyle={dropdownStyle}
  1396. treeData={treeDataWithoutValue}
  1397. multiple
  1398. checkRelation='unRelated'
  1399. defaultExpandAll
  1400. disableStrictly
  1401. style={style}
  1402. />
  1403. <br /><br />
  1404. <div>checkRelation='unRelated' + defaultValue 为 China</div>
  1405. <TreeSelect
  1406. dropdownStyle={dropdownStyle}
  1407. treeData={treeData}
  1408. multiple
  1409. checkRelation='unRelated'
  1410. defaultExpandAll
  1411. style={style}
  1412. defaultValue='China'
  1413. />
  1414. <br /><br />
  1415. <div>checkRelation='unRelated' + defaultValue 为 China + 开启搜索</div>
  1416. <TreeSelect
  1417. dropdownStyle={dropdownStyle}
  1418. treeData={treeData}
  1419. multiple
  1420. filterTreeNode
  1421. checkRelation='unRelated'
  1422. defaultExpandAll
  1423. style={style}
  1424. defaultValue='China'
  1425. />
  1426. <br /><br />
  1427. <div>多选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1428. <TreeSelect
  1429. dropdownStyle={dropdownStyle}
  1430. treeData={treeData}
  1431. multiple
  1432. filterTreeNode
  1433. showClear
  1434. checkRelation='unRelated'
  1435. defaultExpandAll
  1436. style={style}
  1437. searchPosition='trigger'
  1438. defaultValue={['China', 'Japan']}
  1439. />
  1440. <br /><br />
  1441. <div>单选 + checkRelation='unRelated' + defaultValue 为 China + 开启搜索 + searchBox in trigger + showClear</div>
  1442. <TreeSelect
  1443. dropdownStyle={dropdownStyle}
  1444. treeData={treeData}
  1445. filterTreeNode
  1446. showClear
  1447. checkRelation='unRelated'
  1448. defaultExpandAll
  1449. style={style}
  1450. searchPosition='trigger'
  1451. defaultValue='China'
  1452. />
  1453. <br /><br />
  1454. <div>checkRelation='unRelated' + 受控 + value 初始为 China</div>
  1455. <TreeSelect
  1456. dropdownStyle={dropdownStyle}
  1457. treeData={treeData}
  1458. multiple
  1459. checkRelation='unRelated'
  1460. defaultExpandAll
  1461. style={style}
  1462. value={value}
  1463. onChange={handleChange}
  1464. />
  1465. <br /><br />
  1466. <div>checkRelation='unRelated' + 受控 + onChangeWithObject</div>
  1467. <TreeSelect
  1468. dropdownStyle={dropdownStyle}
  1469. treeData={treeData}
  1470. multiple
  1471. checkRelation='unRelated'
  1472. defaultExpandAll
  1473. style={style}
  1474. value={value2}
  1475. onChangeWithObject
  1476. onChange={handleChange2}
  1477. />
  1478. <br /><br />
  1479. <div>checkRelation='unRelated' + 受控 + leafOnly,此时 leafOnly 失效</div>
  1480. <TreeSelect
  1481. dropdownStyle={dropdownStyle}
  1482. leafOnly
  1483. treeData={treeData}
  1484. multiple
  1485. checkRelation='unRelated'
  1486. defaultExpandAll
  1487. style={style}
  1488. value={value3}
  1489. onChange={handleChange3}
  1490. />
  1491. <br /><br />
  1492. <div>checkRelation='unRelated' + onSelect </div>
  1493. <TreeSelect
  1494. dropdownStyle={dropdownStyle}
  1495. treeData={treeData}
  1496. multiple
  1497. checkRelation='unRelated'
  1498. defaultExpandAll
  1499. style={style}
  1500. onSelect={(value,status,node)=>console.log('select', value, status, node)}
  1501. />
  1502. </>
  1503. );
  1504. };
  1505. export const SearchableAndExpandedKeys = () => {
  1506. const [expandedKeys1, setExpandedKeys1] = useState([]);
  1507. const [expandedKeys2, setExpandedKeys2] = useState([]);
  1508. const [expandedKeys3, setExpandedKeys3] = useState([]);
  1509. return (
  1510. <>
  1511. <Title heading={6}>expandedKeys 受控</Title>
  1512. <TreeSelect
  1513. style={{ width: 300, marginBottom: 30 }}
  1514. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1515. treeData={treeData2}
  1516. expandedKeys={expandedKeys1}
  1517. defaultValue='beijing'
  1518. onExpand={v => {
  1519. console.log('onExpand value: ', v);
  1520. setExpandedKeys1(v);
  1521. }}
  1522. />
  1523. <Title heading={6}>expandedKeys 受控 + 开启搜索</Title>
  1524. <TreeSelect
  1525. style={{ width: 300, marginBottom: 30 }}
  1526. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1527. treeData={treeData2}
  1528. filterTreeNode
  1529. defaultValue='beijing'
  1530. expandedKeys={expandedKeys2}
  1531. onExpand={v => {
  1532. console.log('onExpand value: ', v);
  1533. setExpandedKeys2(v);
  1534. }}
  1535. />
  1536. <Title heading={6}>expandedKeys 受控 + 开启搜索 + 搜索时更新 expandedKeys</Title>
  1537. <TreeSelect
  1538. style={{ width: 300, marginBottom: 30 }}
  1539. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1540. treeData={treeData2}
  1541. filterTreeNode
  1542. expandedKeys={expandedKeys3}
  1543. defaultValue='beijing'
  1544. onExpand={v => {
  1545. console.log('onExpand value: ', v);
  1546. setExpandedKeys3(v)
  1547. }}
  1548. onSearch={(input, filterExpandedKeys) => {
  1549. console.log('onExpand filterExpandedKeys: ', filterExpandedKeys);
  1550. setExpandedKeys3(filterExpandedKeys);
  1551. }}
  1552. />
  1553. </>
  1554. )
  1555. }
  1556. export const loadData = () => {
  1557. const initialData = [
  1558. {
  1559. label: 'Expand to load',
  1560. value: '0',
  1561. key: '0',
  1562. },
  1563. {
  1564. label: 'Expand to load',
  1565. value: '1',
  1566. key: '1',
  1567. },
  1568. {
  1569. label: 'Leaf Node',
  1570. value: '2',
  1571. key: '2',
  1572. isLeaf: true,
  1573. },
  1574. ];
  1575. const [treeData, setTreeData] = useState(initialData);
  1576. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1577. function updateTreeData(list, key, children) {
  1578. return list.map(node => {
  1579. if (node.key === key) {
  1580. return { ...node, children };
  1581. }
  1582. if (node.children) {
  1583. return { ...node, children: updateTreeData(node.children, key, children) };
  1584. }
  1585. return node;
  1586. });
  1587. }
  1588. function onLoadData({ key, children }) {
  1589. return new Promise(resolve => {
  1590. if (children) {
  1591. resolve();
  1592. return;
  1593. }
  1594. setTimeout(() => {
  1595. setTreeData(origin =>
  1596. updateTreeData(origin, key, [
  1597. {
  1598. label: 'Child Node',
  1599. key: `${key}-0`,
  1600. },
  1601. {
  1602. label: 'Child Node',
  1603. key: `${key}-1`,
  1604. },
  1605. ]),
  1606. );
  1607. resolve();
  1608. }, 1000);
  1609. });
  1610. }
  1611. return (
  1612. <TreeSelect
  1613. loadData={onLoadData}
  1614. filterTreeNode
  1615. treeData={treeData}
  1616. style={{ width: 300 }}
  1617. placeholder="请选择"
  1618. />
  1619. );
  1620. }
  1621. export const loadDataAndLoadedkeys = () => {
  1622. const initialData = [
  1623. {
  1624. label: 'Expand to load',
  1625. value: '0',
  1626. key: '0',
  1627. },
  1628. {
  1629. label: 'Expand to load',
  1630. value: '1',
  1631. key: '1',
  1632. },
  1633. {
  1634. label: 'Leaf Node',
  1635. value: '2',
  1636. key: '2',
  1637. isLeaf: true,
  1638. },
  1639. ];
  1640. const [treeData, setTreeData] = useState(initialData);
  1641. const [loadedKeys, setLoadedKeys] = useState(['2']);
  1642. function updateTreeData(list, key, children) {
  1643. return list.map(node => {
  1644. if (node.key === key) {
  1645. return { ...node, children };
  1646. }
  1647. if (node.children) {
  1648. return { ...node, children: updateTreeData(node.children, key, children) };
  1649. }
  1650. return node;
  1651. });
  1652. }
  1653. function updateLoadedKeys(key) {
  1654. if(!loadedKeys.includes(key)){
  1655. setLoadedKeys([...loadedKeys, key]);
  1656. console.log('[...loadedKeys, key]', [...loadedKeys, key]);
  1657. }
  1658. }
  1659. function onLoadData({ key, children }) {
  1660. return new Promise(resolve => {
  1661. if (children) {
  1662. resolve();
  1663. return;
  1664. }
  1665. setTimeout(() => {
  1666. setTreeData(origin =>
  1667. updateTreeData(origin, key, [
  1668. {
  1669. label: 'Child Node',
  1670. key: `${key}-0`,
  1671. },
  1672. {
  1673. label: 'Child Node',
  1674. key: `${key}-1`,
  1675. },
  1676. ]),
  1677. );
  1678. // updateLoadedKeys(key);
  1679. resolve();
  1680. }, 1000);
  1681. });
  1682. }
  1683. return (
  1684. <TreeSelect
  1685. loadData={onLoadData}
  1686. filterTreeNode
  1687. // loadedKeys={loadedKeys}
  1688. treeData={treeData}
  1689. style={{ width: 300 }}
  1690. placeholder="请选择"
  1691. />
  1692. );
  1693. }
  1694. export const size = () => {
  1695. const props = {
  1696. style: { width: 300 },
  1697. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  1698. defaultValue: ['0-0-1', '0-0-2', '0-0-3', '0-0-4', '0-0-5', '0-0-6', '0-0-7'],
  1699. treeData: treeData3,
  1700. multiple: true,
  1701. filterTreeNode: true,
  1702. searchPosition: "trigger"
  1703. };
  1704. return (<>
  1705. <TreeSelect {...props} size={'small'} placeholder={'small'} />
  1706. <br/><br/>
  1707. <TreeSelect {...props} size={'default'} placeholder={'default'} />
  1708. <br/><br/>
  1709. <TreeSelect {...props} size={'large'} placeholder={'large'} />
  1710. </>);
  1711. }
  1712. export const valueNotInTreeDataIssue = () => {
  1713. const treeData = [
  1714. {
  1715. key: "test",
  1716. label: "测试标签",
  1717. children: [
  1718. {
  1719. key: "test_2",
  1720. label: "测试二级标签"
  1721. },
  1722. {
  1723. key: "jzr_test",
  1724. label: "之睿测试"
  1725. }
  1726. ]
  1727. },
  1728. {
  1729. key: "create",
  1730. label: "创作构思",
  1731. children: [
  1732. {
  1733. key: "material",
  1734. label: "素材积累"
  1735. },
  1736. {
  1737. key: "lens_script",
  1738. label: "分镜脚本"
  1739. }
  1740. ]
  1741. }
  1742. ];
  1743. const treeDataWithValue = [
  1744. {
  1745. value: "test",
  1746. key: "0",
  1747. label: "测试标签",
  1748. children: [
  1749. {
  1750. value: "test_2",
  1751. key: "0-1",
  1752. label: "测试二级标签"
  1753. },
  1754. {
  1755. value: "jzr_test",
  1756. key: "0-2",
  1757. label: "之睿测试"
  1758. }
  1759. ]
  1760. },
  1761. {
  1762. value: "create",
  1763. key: "1",
  1764. label: "创作构思",
  1765. children: [
  1766. {
  1767. value: "material",
  1768. key: "1-1",
  1769. label: "素材积累"
  1770. },
  1771. {
  1772. value: "lens_script",
  1773. key: "1-2",
  1774. label: "分镜脚本"
  1775. }
  1776. ]
  1777. }
  1778. ];
  1779. const commonProps = useMemo(() => {
  1780. return {
  1781. multiple: true,
  1782. style: { width: 300 },
  1783. dropdownStyle: { maxHeight: 400, overflow: 'auto' },
  1784. onChange: (value) => {
  1785. console.log('onChange', value);
  1786. },
  1787. onSelect: (value) => {
  1788. console.log('onSelect', value);
  1789. },
  1790. };
  1791. }, []);
  1792. return (
  1793. <>
  1794. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,无 value</p>
  1795. <p>checkRelation='related'</p>
  1796. <TreeSelect
  1797. defaultExpandAll
  1798. defaultValue={["test_2", 0]}
  1799. treeData={treeData}
  1800. {...commonProps}
  1801. />
  1802. <p>checkRelation='unRelated'</p>
  1803. <TreeSelect
  1804. defaultExpandAll
  1805. defaultValue={["test_2", "fish"]}
  1806. checkRelation='unRelated'
  1807. treeData={treeData}
  1808. {...commonProps}
  1809. />
  1810. <p>onChangeWithObject, checkRelation='related'</p>
  1811. <TreeSelect
  1812. defaultExpandAll
  1813. onChangeWithObject
  1814. defaultValue={[
  1815. {
  1816. key: "test_2",
  1817. label: "测试二级标签"
  1818. },
  1819. {
  1820. key: "fish",
  1821. label: "鱼"
  1822. }
  1823. ]}
  1824. treeData={treeData}
  1825. {...commonProps}
  1826. />
  1827. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1828. <TreeSelect
  1829. defaultExpandAll
  1830. onChangeWithObject
  1831. defaultValue={[
  1832. {
  1833. key: "test_2",
  1834. label: "测试二级标签"
  1835. },
  1836. {
  1837. key: "fish",
  1838. label: "鱼"
  1839. }
  1840. ]}
  1841. treeData={treeData}
  1842. {...commonProps}
  1843. />
  1844. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>多选,有 value</p>
  1845. <p>checkRelation='related'</p>
  1846. <TreeSelect
  1847. defaultExpandAll
  1848. defaultValue={["test", "fish"]}
  1849. treeData={treeDataWithValue}
  1850. {...commonProps}
  1851. />
  1852. <p>checkRelation='unRelated'</p>
  1853. <TreeSelect
  1854. defaultExpandAll
  1855. defaultValue={["test", "fish"]}
  1856. checkRelation='unRelated'
  1857. treeData={treeDataWithValue}
  1858. {...commonProps}
  1859. />
  1860. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1861. <TreeSelect
  1862. defaultExpandAll
  1863. onChangeWithObject
  1864. defaultValue={[
  1865. {
  1866. value: "test_2",
  1867. key: "0-1",
  1868. label: "测试二级标签"
  1869. },
  1870. {
  1871. key: "fish",
  1872. value: "Fish",
  1873. label: "鱼"
  1874. }
  1875. ]}
  1876. treeData={treeDataWithValue}
  1877. {...commonProps}
  1878. />
  1879. <p>onChangeWithObject, checkRelation='unRelated'</p>
  1880. <TreeSelect
  1881. defaultExpandAll
  1882. onChangeWithObject
  1883. defaultValue={[
  1884. {
  1885. value: "test_2",
  1886. key: "0-1",
  1887. label: "测试二级标签"
  1888. },
  1889. {
  1890. key: "fish",
  1891. value: "Fish",
  1892. label: "鱼"
  1893. }
  1894. ]}
  1895. treeData={treeDataWithValue}
  1896. {...commonProps}
  1897. />
  1898. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,无 value</p>
  1899. <TreeSelect
  1900. defaultExpandAll
  1901. defaultValue={"fish"}
  1902. treeData={treeData}
  1903. {...commonProps}
  1904. multiple={false}
  1905. />
  1906. <p>onChangeWithObject</p>
  1907. <TreeSelect
  1908. defaultExpandAll
  1909. defaultValue={{
  1910. key: "fish",
  1911. value: "Fish",
  1912. label: "鱼"
  1913. }}
  1914. treeData={treeData}
  1915. {...commonProps}
  1916. multiple={false}
  1917. onChangeWithObject
  1918. />
  1919. <p style={{ backgroundColor: 'yellowgreen', width: 'fit-content' }}>单选,有 value</p>
  1920. <TreeSelect
  1921. defaultExpandAll
  1922. defaultValue={"fish"}
  1923. treeData={treeDataWithValue}
  1924. {...commonProps}
  1925. multiple={false}
  1926. />
  1927. <p>onChangeWithObject</p>
  1928. <TreeSelect
  1929. defaultExpandAll
  1930. defaultValue={{
  1931. key: "fish",
  1932. value: "Fish",
  1933. label: "鱼"
  1934. }}
  1935. treeData={treeDataWithValue}
  1936. {...commonProps}
  1937. multiple={false}
  1938. onChangeWithObject
  1939. />
  1940. </>
  1941. );
  1942. };
  1943. class ValueTypeIsNumber extends React.Component {
  1944. constructor() {
  1945. super();
  1946. this.state = {
  1947. value: 1
  1948. };
  1949. }
  1950. onChange(value) {
  1951. console.log('onChange', value);
  1952. this.setState({ value });
  1953. }
  1954. render() {
  1955. const treeData = [
  1956. {
  1957. label: '北美洲',
  1958. value: 'North America',
  1959. key: '1',
  1960. },
  1961. {
  1962. label: '亚洲',
  1963. value: 'Asia',
  1964. key: '0',
  1965. children: [
  1966. {
  1967. label: '中国',
  1968. value: 'China',
  1969. key: '0-0',
  1970. children: [
  1971. {
  1972. label: '北京',
  1973. value: 'Beijing',
  1974. key: '0-0-0',
  1975. },
  1976. {
  1977. label: '上海',
  1978. value: 'Shanghai',
  1979. key: '0-0-1',
  1980. },
  1981. ],
  1982. },
  1983. ],
  1984. },
  1985. ];
  1986. return (
  1987. <TreeSelect
  1988. style={{ width: 300 }}
  1989. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1990. treeData={treeData}
  1991. value={this.state.value}
  1992. placeholder="请选择"
  1993. multiple
  1994. onChange={e => this.onChange(e)}
  1995. />
  1996. );
  1997. }
  1998. }
  1999. export const valueIsNumber = () => <ValueTypeIsNumber />