treeSelect.stories.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. import React, { useState } from 'react';
  2. import { Icon, Button, Form, Popover, Tag } from '../../index';
  3. import TreeSelect from '../index';
  4. import { flattenDeep } from 'lodash';
  5. import CustomTrigger from './CustomTrigger';
  6. import { IconCreditCard } from '@douyinfe/semi-icons';
  7. const TreeNode = TreeSelect.TreeNode;
  8. export default {
  9. title: 'TreeSelect',
  10. parameters: {
  11. chromatic: { disableSnapshot: true },
  12. },
  13. }
  14. const treeData1 = [
  15. {
  16. label: 'Node1',
  17. value: '0-0',
  18. key: '0-0',
  19. children: [
  20. {
  21. label: 'Child Node1',
  22. value: '0-0-1',
  23. key: '0-0-1',
  24. },
  25. {
  26. label: 'Child Node2',
  27. value: '0-0-2',
  28. key: '0-0-2',
  29. },
  30. ],
  31. },
  32. {
  33. label: 'Node2',
  34. value: '0-1',
  35. key: '0-1',
  36. },
  37. ];
  38. const treeData2 = [
  39. {
  40. label: '亚洲',
  41. value: 'yazhou',
  42. key: 'yazhou',
  43. children: [
  44. {
  45. label: '中国',
  46. value: 'zhongguo',
  47. key: 'zhongguo',
  48. children: [
  49. {
  50. label: '北京',
  51. value: 'beijing',
  52. key: 'beijing',
  53. },
  54. {
  55. label: '上海',
  56. value: 'shanghai',
  57. key: 'shanghai',
  58. },
  59. ],
  60. },
  61. // {
  62. // label: '日本',
  63. // value: 'riben',
  64. // key: 'riben',
  65. // children: [
  66. // {
  67. // label: '东京',
  68. // value: 'dongjing',
  69. // key: 'dongjing'
  70. // },
  71. // {
  72. // label: '大阪',
  73. // value: 'daban',
  74. // key: 'daban'
  75. // }
  76. // ]
  77. // },
  78. ],
  79. },
  80. {
  81. label: '北美洲',
  82. value: 'beimeizhou',
  83. key: 'beimeizhou',
  84. children: [
  85. {
  86. label: '美国',
  87. value: 'meiguo',
  88. key: 'meiguo',
  89. },
  90. {
  91. label: '加拿大',
  92. value: 'jianada',
  93. key: 'jianada',
  94. },
  95. ],
  96. },
  97. ];
  98. const treeDataWithoutValue = [
  99. {
  100. label: '亚洲',
  101. key: 'yazhou',
  102. children: [
  103. {
  104. label: '中国',
  105. key: 'zhongguo',
  106. disabled: true,
  107. children: [
  108. {
  109. label: '北京',
  110. key: 'beijing',
  111. },
  112. {
  113. label: '上海',
  114. key: 'shanghai',
  115. },
  116. ],
  117. },
  118. {
  119. label: '日本',
  120. key: 'riben',
  121. children: [
  122. {
  123. label: '东京',
  124. key: 'dongjing',
  125. },
  126. {
  127. label: '大阪',
  128. key: 'daban',
  129. },
  130. ],
  131. },
  132. ],
  133. },
  134. {
  135. label: '北美洲',
  136. key: 'beimeizhou',
  137. children: [
  138. {
  139. label: '美国',
  140. key: 'meiguo',
  141. },
  142. {
  143. label: '加拿大',
  144. key: 'jianada',
  145. },
  146. ],
  147. },
  148. ];
  149. class SimpleTree extends React.Component {
  150. render() {
  151. return (
  152. <div>
  153. <TreeSelect
  154. style={{ width: 300 }}
  155. // value={this.state.value}
  156. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  157. treeData={treeData1}
  158. placeholder="Please select"
  159. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  160. onSelect={(e, bool) => console.log('select', e, bool)}
  161. onChange={(e, node) => console.log('change', e, node)}
  162. />
  163. <br />
  164. <br />
  165. <TreeSelect
  166. style={{ width: 300 }}
  167. // value={this.state.value}
  168. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  169. treeData={treeData2}
  170. placeholder="Please select"
  171. />
  172. </div>
  173. );
  174. }
  175. }
  176. export const _TreeSelect = () => {
  177. const treeData = [
  178. {
  179. label: '亚洲',
  180. value: 'Asia',
  181. key: '0',
  182. children: [
  183. {
  184. label: '中国',
  185. value: 'China',
  186. key: '0-0',
  187. children: [
  188. {
  189. label: '北京',
  190. value: 'Beijing',
  191. key: '0-0-0',
  192. },
  193. {
  194. label: '上海',
  195. value: 'Shanghai',
  196. key: '0-0-1',
  197. },
  198. ],
  199. },
  200. ],
  201. },
  202. {
  203. label: '北美洲',
  204. value: 'North America',
  205. key: '1',
  206. },
  207. ];
  208. return (
  209. <TreeSelect
  210. style={{ width: 300 }}
  211. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  212. treeData={treeData2}
  213. placeholder="请选择"
  214. />
  215. );
  216. };
  217. _TreeSelect.story = {
  218. name: 'tree select',
  219. };
  220. export const Searchable = () => (
  221. <div>
  222. <TreeSelect
  223. style={{ width: 300 }}
  224. // value={this.state.value}
  225. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  226. treeData={treeData2}
  227. filterTreeNode
  228. treeNodeFilterProp="value"
  229. placeholder="Please select"
  230. />
  231. <br />
  232. <br />
  233. <TreeSelect
  234. style={{ width: 300 }}
  235. // value={this.state.value}
  236. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  237. treeData={treeData2}
  238. filterTreeNode
  239. placeholder="Please select"
  240. />
  241. <br />
  242. <br />
  243. <TreeSelect
  244. style={{ width: 300 }}
  245. // value={this.state.value}
  246. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  247. treeData={treeData2}
  248. filterTreeNode
  249. searchAutoFocus
  250. placeholder="searchAutoFocus"
  251. />
  252. </div>
  253. );
  254. Searchable.story = {
  255. name: 'searchable',
  256. };
  257. export const SearchPosition = () => (
  258. <>
  259. <TreeSelect
  260. searchPosition="trigger"
  261. style={{ width: 300 }}
  262. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  263. treeData={treeData2}
  264. filterTreeNode
  265. placeholder="单选"
  266. />
  267. <br />
  268. <br />
  269. <TreeSelect
  270. searchPosition="trigger"
  271. style={{ width: 300 }}
  272. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  273. treeData={treeData2}
  274. multiple
  275. filterTreeNode
  276. maxTagCount={2}
  277. placeholder="多选"
  278. />
  279. <br />
  280. <br />
  281. <TreeSelect
  282. searchPosition="trigger"
  283. style={{ width: 300 }}
  284. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  285. treeData={treeData2}
  286. multiple
  287. filterTreeNode
  288. maxTagCount={2}
  289. placeholder="searchAutoFocus"
  290. searchAutoFocus
  291. />
  292. <br />
  293. <br />
  294. <TreeSelect
  295. searchPosition="trigger"
  296. style={{ width: 300 }}
  297. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  298. treeData={treeData2}
  299. multiple
  300. filterTreeNode
  301. maxTagCount={1}
  302. placeholder="maxTagCount=1"
  303. />
  304. </>
  305. );
  306. SearchPosition.story = {
  307. name: 'searchPosition',
  308. };
  309. export const PrefixSuffixInsetLabel = () => (
  310. <div>
  311. <TreeSelect
  312. style={{ width: 300 }}
  313. // value={this.state.value}
  314. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  315. treeData={treeData2}
  316. filterTreeNode
  317. prefix={<IconCreditCard />}
  318. treeNodeFilterProp="value"
  319. placeholder="Please select"
  320. />
  321. <br />
  322. <br />
  323. <TreeSelect
  324. style={{ width: 300 }}
  325. // value={this.state.value}
  326. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  327. treeData={treeData2}
  328. filterTreeNode
  329. prefix={<span>1234</span>}
  330. treeNodeFilterProp="value"
  331. placeholder="Please select"
  332. />
  333. <br />
  334. <br />
  335. <TreeSelect
  336. style={{ width: 300 }}
  337. suffix="RMB"
  338. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  339. treeData={treeData2}
  340. placeholder="Please select"
  341. />
  342. <br />
  343. <br />
  344. <TreeSelect
  345. style={{ width: 300 }}
  346. insetLabel="blablabla"
  347. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  348. treeData={treeData2}
  349. placeholder="Please select"
  350. />
  351. <br />
  352. <br />
  353. <TreeSelect
  354. style={{ width: 300 }}
  355. insetLabel={<span>1234</span>}
  356. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  357. treeData={treeData2}
  358. placeholder="Please select"
  359. />
  360. </div>
  361. );
  362. PrefixSuffixInsetLabel.story = {
  363. name: 'prefix suffix insetLabel',
  364. };
  365. PrefixSuffixInsetLabel.parameters = {
  366. chromatic: { disableSnapshot: false },
  367. }
  368. export const ValidateStatus = () => (
  369. <div>
  370. <TreeSelect
  371. style={{ width: 300 }}
  372. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  373. treeData={treeData2}
  374. multiple
  375. validateStatus="warning"
  376. placeholder="Please select"
  377. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  378. onSelect={(e, bool) => console.log('select', e, bool)}
  379. onChange={e => console.log('change', e)}
  380. />
  381. <br />
  382. <br />
  383. <TreeSelect
  384. style={{ width: 300 }}
  385. validateStatus="error"
  386. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  387. treeData={treeData2}
  388. // multiple
  389. placeholder="Please select"
  390. />
  391. </div>
  392. );
  393. ValidateStatus.story = {
  394. name: 'validate status',
  395. };
  396. ValidateStatus.parameters = {
  397. chromatic: { disableSnapshot: false },
  398. }
  399. export const Multiple = () => (
  400. <div>
  401. <TreeSelect
  402. style={{ width: 300 }}
  403. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  404. treeData={treeData2}
  405. multiple
  406. placeholder="Please select"
  407. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  408. onSelect={(e, bool) => console.log('select', e, bool)}
  409. onChange={e => console.log('change', e)}
  410. />
  411. <br />
  412. <br />
  413. <TreeSelect
  414. style={{ width: 300 }}
  415. // value={this.state.value}
  416. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  417. treeData={treeData2}
  418. multiple
  419. placeholder="Please select"
  420. />
  421. </div>
  422. );
  423. Multiple.story = {
  424. name: 'multiple',
  425. };
  426. export const MaxTagCount = () => (
  427. <div>
  428. <TreeSelect
  429. style={{ width: 300 }}
  430. // value={this.state.value}
  431. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  432. treeData={treeData2}
  433. multiple
  434. maxTagCount={2}
  435. placeholder="Please select"
  436. />
  437. {/* <br />
  438. <br />
  439. <TreeSelect
  440. style={{ width: 300 }}
  441. // value={this.state.value}
  442. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  443. treeData={treeData2}
  444. multiple
  445. placeholder="Please select"
  446. /> */}
  447. </div>
  448. );
  449. MaxTagCount.story = {
  450. name: 'maxTagCount',
  451. };
  452. export const MultipleSearchable = () => (
  453. <div>
  454. <TreeSelect
  455. style={{ width: 300 }}
  456. // value={this.state.value}
  457. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  458. treeData={treeData2}
  459. multiple
  460. filterTreeNode
  461. treeNodeFilterProp="value"
  462. placeholder="Please select"
  463. />
  464. </div>
  465. );
  466. MultipleSearchable.story = {
  467. name: 'multiple searchable',
  468. };
  469. export const DefaultValues = () => (
  470. <div>
  471. <TreeSelect
  472. style={{ width: 300 }}
  473. defaultValue={'shanghai'}
  474. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  475. treeData={treeData2}
  476. // multiple
  477. filterTreeNode
  478. treeNodeFilterProp="value"
  479. />
  480. <br />
  481. <br />
  482. <TreeSelect
  483. style={{ width: 300 }}
  484. defaultValue={'shanghai'}
  485. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  486. treeData={treeData2}
  487. filterTreeNode
  488. multiple
  489. treeNodeFilterProp="value"
  490. />
  491. <br />
  492. <br />
  493. <TreeSelect
  494. style={{ width: 300 }}
  495. defaultValue={['shanghai', 'daban', 'dongjing']}
  496. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  497. treeData={treeData2}
  498. filterTreeNode
  499. multiple
  500. treeNodeFilterProp="value"
  501. />
  502. <br />
  503. <br />
  504. <TreeSelect
  505. style={{ width: 300 }}
  506. defaultValue={['meiguo', 'dongjing']}
  507. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  508. treeData={treeData2}
  509. filterTreeNode
  510. multiple
  511. treeNodeFilterProp="value"
  512. />
  513. </div>
  514. );
  515. DefaultValues.story = {
  516. name: 'default values',
  517. };
  518. export const Disabled = () => (
  519. <div>
  520. <TreeSelect
  521. style={{ width: 300 }}
  522. defaultValue={'shanghai'}
  523. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  524. treeData={treeData2}
  525. multiple
  526. filterTreeNode
  527. disabled
  528. treeNodeFilterProp="value"
  529. placeholder="Please select"
  530. />
  531. <br />
  532. <br />
  533. <TreeSelect
  534. style={{ width: 300 }}
  535. defaultValue={'shanghai'}
  536. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  537. treeData={treeData2}
  538. filterTreeNode
  539. disabled
  540. treeNodeFilterProp="value"
  541. placeholder="Please select"
  542. />
  543. <br />
  544. <br />
  545. <TreeSelect
  546. style={{ width: 300 }}
  547. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  548. treeData={treeData2}
  549. multiple
  550. filterTreeNode
  551. disabled
  552. treeNodeFilterProp="value"
  553. placeholder="Please select"
  554. />
  555. </div>
  556. );
  557. Disabled.story = {
  558. name: 'disabled',
  559. };
  560. Disabled.parameters = {
  561. chromatic: { disableSnapshot: false },
  562. }
  563. export const OptionLabelProp = () => (
  564. <>
  565. <TreeSelect
  566. style={{ width: 300 }}
  567. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  568. treeData={treeData2}
  569. multiple
  570. filterTreeNode
  571. treeNodeFilterProp="value"
  572. treeNodeLabelProp="value"
  573. placeholder="Please select"
  574. />
  575. </>
  576. );
  577. OptionLabelProp.story = {
  578. name: 'optionLabelProp',
  579. };
  580. export const ValueInArray = () => (
  581. <>
  582. <TreeSelect
  583. style={{ width: 300 }}
  584. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  585. treeData={treeData2}
  586. multiple
  587. filterTreeNode
  588. valueInArray
  589. onChange={(...args) => console.log(args)}
  590. treeNodeFilterProp="value"
  591. treeNodeLabelProp="value"
  592. placeholder="Please select"
  593. />
  594. </>
  595. );
  596. ValueInArray.story = {
  597. name: 'valueInArray',
  598. };
  599. export const OnBlurOnFocus = () => (
  600. <>
  601. <div>single</div>
  602. <TreeSelect
  603. style={{ width: 300 }}
  604. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  605. treeData={treeData2}
  606. onBlur={(...args) => console.log('blur', args)}
  607. onFocus={(...args) => console.log('focus', args)}
  608. placeholder="Please select"
  609. />
  610. <div>multiple</div>
  611. <TreeSelect
  612. style={{ width: 300 }}
  613. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  614. treeData={treeData2}
  615. multiple
  616. onBlur={(...args) => console.log('blur', args)}
  617. onFocus={(...args) => console.log('focus', args)}
  618. placeholder="Please select"
  619. />
  620. </>
  621. );
  622. OnBlurOnFocus.story = {
  623. name: 'onBlur/onFocus',
  624. };
  625. export const LeafOnly = () => (
  626. <div>
  627. <TreeSelect
  628. style={{ width: 300 }}
  629. // value={this.state.value}
  630. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  631. treeData={treeData2}
  632. multiple
  633. leafOnly
  634. placeholder="Please select"
  635. onExpand={(e, { expanded, node }) => console.log('expand', e, expanded, node)}
  636. onSelect={(e, bool) => console.log('select', e, bool)}
  637. onChange={e => console.log('change', e)}
  638. />
  639. </div>
  640. );
  641. LeafOnly.story = {
  642. name: 'leafOnly',
  643. };
  644. class Demo extends React.Component {
  645. constructor() {
  646. super();
  647. this.state = {
  648. value: 'shanghai',
  649. };
  650. }
  651. onChange(value) {
  652. this.setState({ value });
  653. }
  654. render() {
  655. return (
  656. <TreeSelect
  657. style={{ width: 300 }}
  658. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  659. treeData={treeData2}
  660. value={this.state.value}
  661. placeholder="Please select"
  662. onChange={e => this.onChange(e)}
  663. />
  664. );
  665. }
  666. }
  667. export const ControlledComponentSingle = () => <Demo />;
  668. ControlledComponentSingle.story = {
  669. name: 'controlled Component single',
  670. };
  671. class Demo2 extends React.Component {
  672. constructor() {
  673. super();
  674. this.state = {
  675. value: ['Shanghai'],
  676. };
  677. }
  678. onChange(value) {
  679. this.setState({ value });
  680. }
  681. render() {
  682. console.log(this.state.value);
  683. const treeData = [
  684. {
  685. label: '亚洲',
  686. value: 'Asia',
  687. key: '0',
  688. children: [
  689. {
  690. label: '中国',
  691. value: 'China',
  692. key: '0-0',
  693. children: [
  694. {
  695. label: '北京',
  696. value: 'Beijing',
  697. key: '0-0-0',
  698. },
  699. {
  700. label: '上海',
  701. value: 'Shanghai',
  702. key: '0-0-1',
  703. },
  704. ],
  705. },
  706. ],
  707. },
  708. {
  709. label: '北美洲',
  710. value: 'North America',
  711. key: '1',
  712. },
  713. ];
  714. return (
  715. <TreeSelect
  716. style={{ width: 300 }}
  717. multiple
  718. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  719. treeData={treeData}
  720. value={this.state.value}
  721. placeholder="Please select"
  722. onChange={e => this.onChange(e)}
  723. />
  724. );
  725. }
  726. }
  727. export const ControlledComponentMultiple = () => <Demo2 />;
  728. ControlledComponentMultiple.story = {
  729. name: 'controlled Component multiple',
  730. };
  731. class Demo3 extends React.Component {
  732. constructor() {
  733. super();
  734. this.state = {
  735. value: ['Shanghai'],
  736. };
  737. }
  738. // 获取最底层值
  739. getDeepChildrensByNode = node => {
  740. return flattenDeep(
  741. node.map(item => {
  742. if (item.children) {
  743. return this.getDeepChildrensByNode(item.children);
  744. }
  745. return item.value;
  746. })
  747. );
  748. };
  749. onChange(value, node) {
  750. console.log('onchange', value);
  751. value = this.getDeepChildrensByNode(node);
  752. console.log('modifiled', value);
  753. this.setState({ value });
  754. }
  755. render() {
  756. const treeData = [
  757. {
  758. label: '亚洲',
  759. value: 'Asia',
  760. key: '0',
  761. children: [
  762. {
  763. label: '中国',
  764. value: 'China',
  765. key: '0-0',
  766. children: [
  767. {
  768. label: '北京',
  769. value: 'Beijing',
  770. key: '0-0-0',
  771. },
  772. {
  773. label: '上海',
  774. value: 'Shanghai',
  775. key: '0-0-1',
  776. },
  777. ],
  778. },
  779. ],
  780. },
  781. {
  782. label: '北美洲',
  783. value: 'North America',
  784. key: '1',
  785. },
  786. ];
  787. return (
  788. <TreeSelect
  789. style={{ width: 300 }}
  790. multiple
  791. dropdownStyle={{ maxHeight: 200, overflow: 'auto' }}
  792. treeData={treeData}
  793. value={this.state.value}
  794. placeholder="Please select"
  795. onChange={(e, node) => this.onChange(e, node)}
  796. />
  797. );
  798. }
  799. }
  800. export const ControlledComponentMultipleValueModified = () => <Demo3 />;
  801. ControlledComponentMultipleValueModified.story = {
  802. name: 'controlled Component multiple value modified',
  803. };
  804. class ConvertDemo extends React.Component {
  805. constructor(props) {
  806. super(props);
  807. this.formApi = null;
  808. }
  809. handleChange = val => {
  810. let finalVal = val;
  811. let firstClassOption = ['Asia', 'North America'];
  812. // 在这里去做你的value替换逻辑
  813. console.log('originVal:' + val);
  814. if (val.length === 1) {
  815. // do nothing
  816. } else {
  817. if (val.every(item => firstClassOption.includes(item))) {
  818. finalVal = val[val.length - 1];
  819. }
  820. }
  821. console.log('finalVal:' + finalVal);
  822. return finalVal;
  823. };
  824. render() {
  825. const treeData = [
  826. {
  827. label: '亚洲',
  828. value: 'Asia',
  829. key: '0',
  830. children: [
  831. {
  832. label: '中国',
  833. value: 'China',
  834. key: '0-0',
  835. children: [
  836. {
  837. label: '北京',
  838. value: 'Beijing',
  839. key: '0-0-0',
  840. },
  841. {
  842. label: '上海',
  843. value: 'Shanghai',
  844. key: '0-0-1',
  845. },
  846. ],
  847. },
  848. ],
  849. },
  850. {
  851. label: '北美洲',
  852. value: 'North America',
  853. key: '1',
  854. },
  855. ];
  856. return (
  857. <Form getFormApi={this.getFormApi}>
  858. <Form.TreeSelect
  859. field="tree"
  860. label="节点(TreeSelect)"
  861. placeholder="请选择服务节点"
  862. treeData={treeData}
  863. convert={this.handleChange}
  864. filterTreeNode
  865. multiple
  866. ></Form.TreeSelect>
  867. </Form>
  868. );
  869. }
  870. }
  871. export const _ConvertDemo = () => <ConvertDemo />;
  872. _ConvertDemo.story = {
  873. name: 'convert demo',
  874. };
  875. export const TreeselectDefaultOpenInPopover = () => (
  876. <Popover
  877. content={
  878. <TreeSelect
  879. style={{ width: 200 }}
  880. dropdownStyle={{ width: 200 }}
  881. treeData={treeData1}
  882. defaultOpen
  883. // value={this.state.value}
  884. // placeholder="Please select"
  885. // onChange={(e, node) => this.onChange(e, node)}
  886. />
  887. }
  888. >
  889. <Button style={{ marginLeft: 150 }}>悬停此处</Button>
  890. </Popover>
  891. );
  892. TreeselectDefaultOpenInPopover.story = {
  893. name: 'treeselect defaultOpen in popover',
  894. };
  895. export const CustomTriggerDemo = () => <CustomTrigger />;
  896. CustomTriggerDemo.story = { name: 'custom trigger' };
  897. const AutoParentDemo = () => {
  898. const [expandedKeys, setExpandedKeys] = useState(['beimeizhou']);
  899. const [selectedKeys, setSelectedKeys] = useState(['beimeizhou']);
  900. const [autoExpandParent, setAutoExpandParent] = useState(true);
  901. const onExpand = expandedKeys => {
  902. console.log('onExpand', expandedKeys);
  903. // if not set autoExpandParent to false, if children expanded, parent can not collapse.
  904. // or, you can remove all expanded children keys.
  905. setExpandedKeys(expandedKeys);
  906. setAutoExpandParent(false);
  907. };
  908. const onSelect = (selectedKeys, info) => {
  909. console.log('onSelect:', info);
  910. setSelectedKeys(selectedKeys);
  911. };
  912. return (
  913. <div>
  914. <Button
  915. onClick={() => {
  916. setSelectedKeys(['beijing']);
  917. setExpandedKeys(['beijing']);
  918. setAutoExpandParent(true);
  919. }}
  920. >
  921. Update
  922. </Button>
  923. <TreeSelect
  924. onExpand={onExpand}
  925. expandedKeys={expandedKeys}
  926. autoExpandParent={autoExpandParent}
  927. onChange={onSelect}
  928. value={selectedKeys}
  929. treeData={treeData2}
  930. // multiple
  931. />
  932. </div>
  933. );
  934. };
  935. export const AutoExpandParent = () => (
  936. <>
  937. <AutoParentDemo />
  938. </>
  939. );
  940. AutoExpandParent.story = {
  941. name: 'autoExpandParent',
  942. };
  943. export const TreeWithoutValueProps = () => (
  944. <TreeSelect
  945. treeData={treeDataWithoutValue}
  946. value="beijing"
  947. defaultExpandAll
  948. onChange={(...args) => console.log(args)}
  949. />
  950. );
  951. TreeWithoutValueProps.story = {
  952. name: 'tree without value props',
  953. };
  954. export const TreeSelectRenderSelectedItem = () => {
  955. const treeData = [
  956. {
  957. label: '亚洲',
  958. value: 'Asia',
  959. key: '0',
  960. children: [
  961. {
  962. label: '中国',
  963. value: 'China',
  964. key: '0-0',
  965. children: [
  966. {
  967. label: '北京',
  968. value: 'Beijing',
  969. key: '0-0-0',
  970. },
  971. {
  972. label: '上海',
  973. value: 'Shanghai',
  974. key: '0-0-1',
  975. },
  976. ],
  977. },
  978. ],
  979. },
  980. {
  981. label: '北美洲',
  982. value: 'North America',
  983. key: '1',
  984. },
  985. {
  986. label: '南美洲',
  987. value: 'South America',
  988. key: '2',
  989. },
  990. {
  991. label: '南极洲',
  992. value: 'Antarctica',
  993. key: '3',
  994. },
  995. ];
  996. return (
  997. <>
  998. <h4>单选</h4>
  999. <TreeSelect
  1000. style={{ width: 300 }}
  1001. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1002. treeData={treeData}
  1003. placeholder="请选择"
  1004. renderSelectedItem={item => `${item.value}-${item.label}`}
  1005. />
  1006. <h4>多选</h4>
  1007. <TreeSelect
  1008. style={{ width: 300 }}
  1009. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1010. treeData={treeData}
  1011. multiple
  1012. placeholder="请选择"
  1013. renderSelectedItem={(item, { index, onClose }) => ({
  1014. content: `${item.value}-${item.label}`,
  1015. isRenderInTag: true,
  1016. })}
  1017. />
  1018. <h4>多选 + isRenderInTag=false</h4>
  1019. <TreeSelect
  1020. style={{ width: 300 }}
  1021. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1022. treeData={treeData}
  1023. multiple
  1024. maxTagCount={2}
  1025. placeholder="请选择"
  1026. renderSelectedItem={(item, { index, onClose }) => ({
  1027. content: (
  1028. <Tag key={index} color="white">
  1029. {item.value}
  1030. </Tag>
  1031. ),
  1032. isRenderInTag: false,
  1033. })}
  1034. />
  1035. </>
  1036. );
  1037. };
  1038. TreeSelectRenderSelectedItem.story = {
  1039. name: 'treeSelect renderSelectedItem',
  1040. };
  1041. const DisableStrictlyDemo = () => {
  1042. const [value, setValue] = useState(['Shanghai']);
  1043. const treeData = [
  1044. {
  1045. label: '亚洲',
  1046. value: 'Asia',
  1047. key: '0',
  1048. children: [
  1049. {
  1050. label: '中国',
  1051. value: 'China',
  1052. key: '0-0',
  1053. disabled: true,
  1054. children: [
  1055. {
  1056. label: '北京',
  1057. value: 'Beijing',
  1058. key: '0-0-0',
  1059. },
  1060. {
  1061. label: '上海',
  1062. value: 'Shanghai',
  1063. key: '0-0-1',
  1064. },
  1065. ],
  1066. },
  1067. {
  1068. label: '日本',
  1069. value: 'Japan',
  1070. key: '0-1',
  1071. },
  1072. ],
  1073. },
  1074. {
  1075. label: '北美洲',
  1076. value: 'North America',
  1077. key: '1',
  1078. },
  1079. ];
  1080. return (
  1081. <div>
  1082. <TreeSelect
  1083. style={{ width: 300 }}
  1084. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  1085. treeData={treeData}
  1086. disableStrictly
  1087. multiple
  1088. searchPosition="trigger"
  1089. filterTreeNode
  1090. value={value}
  1091. onChange={value => setValue(value)}
  1092. />
  1093. </div>
  1094. );
  1095. };
  1096. export const DisabledStrictly = () => (
  1097. <>
  1098. <DisableStrictlyDemo />
  1099. </>
  1100. );
  1101. DisabledStrictly.story = {
  1102. name: 'disabledStrictly',
  1103. };