treeSelect.stories.jsx 65 KB

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