index.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. import React from 'react';
  2. import cls from 'classnames';
  3. import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
  4. import PropTypes from 'prop-types';
  5. import { isEqual, noop, omit, isEmpty, isArray } from 'lodash';
  6. import TransferFoundation, { TransferAdapter, BasicDataItem, OnSortEndProps } from '@douyinfe/semi-foundation/transfer/foundation';
  7. import { _generateDataByType, _generateSelectedItems } from '@douyinfe/semi-foundation/transfer/transferUtlls';
  8. import { cssClasses, strings } from '@douyinfe/semi-foundation/transfer/constants';
  9. import '@douyinfe/semi-foundation/transfer/transfer.scss';
  10. import BaseComponent from '../_base/baseComponent';
  11. import LocaleConsumer from '../locale/localeConsumer';
  12. import { Locale } from '../locale/interface';
  13. import { Checkbox } from '../checkbox/index';
  14. import Input, { InputProps } from '../input/index';
  15. import Spin from '../spin';
  16. import Button from '../button';
  17. import Tree from '../tree';
  18. import { IconClose, IconSearch, IconHandle } from '@douyinfe/semi-icons';
  19. import { Value as TreeValue, TreeProps } from '../tree/interface';
  20. export interface DataItem extends BasicDataItem {
  21. label?: React.ReactNode;
  22. style?: React.CSSProperties;
  23. }
  24. export interface GroupItem {
  25. title?: string;
  26. children?: Array<DataItem>;
  27. }
  28. export interface TreeItem extends DataItem {
  29. children: Array<TreeItem>;
  30. }
  31. export interface RenderSourceItemProps extends DataItem {
  32. checked: boolean;
  33. onChange?: () => void;
  34. }
  35. export interface RenderSelectedItemProps extends DataItem {
  36. onRemove?: () => void;
  37. sortableHandle?: typeof SortableHandle;
  38. }
  39. export interface EmptyContent {
  40. left?: React.ReactNode;
  41. right?: React.ReactNode;
  42. search?: React.ReactNode;
  43. }
  44. export type Type = 'list' | 'groupList' | 'treeList';
  45. export interface SourcePanelProps {
  46. value: Array<string | number>;
  47. /* Loading */
  48. loading: boolean;
  49. /* Whether there are no items that match the current search value */
  50. noMatch: boolean;
  51. /* Items that match the current search value */
  52. filterData: Array<DataItem>;
  53. /* All items */
  54. sourceData: Array<DataItem>;
  55. /* Whether to select all */
  56. allChecked: boolean;
  57. /* Number of filtered results */
  58. showNumber: number;
  59. /* Input search box value */
  60. inputValue: string;
  61. /* The function that should be called when the search box changes */
  62. onSearch: (searchString: string) => void;
  63. /* The function that should be called when all the buttons on the left are clicked */
  64. onAllClick: () => void;
  65. /* Selected item on the left */
  66. selectedItems: Map<string | number, DataItem>;
  67. /* The function that should be called when selecting or deleting a single option */
  68. onSelectOrRemove: (item: DataItem) => void;
  69. /* The function that should be called when selecting an option, */
  70. onSelect: (value: Array<string | number>) => void;
  71. }
  72. export type OnSortEnd = ({ oldIndex, newIndex }: OnSortEndProps) => void;
  73. export interface SelectedPanelProps {
  74. /* Number of selected options */
  75. length: number;
  76. /* Collection of all selected options */
  77. selectedData: Array<DataItem>;
  78. /* Callback function that should be called when click to clear */
  79. onClear: () => void;
  80. /* The function that should be called when a single option is deleted */
  81. onRemove: (item: DataItem) => void;
  82. /* The function that should be called when reordering the results */
  83. onSortEnd: OnSortEnd;
  84. }
  85. export interface ResolvedDataItem extends DataItem {
  86. _parent?: {
  87. title: string;
  88. };
  89. _optionKey?: string | number;
  90. }
  91. export type DataSource = Array<DataItem> | Array<GroupItem> | Array<TreeItem>;
  92. interface HeaderConfig {
  93. totalContent: string;
  94. allContent: string;
  95. onAllClick: () => void;
  96. type: string;
  97. showButton: boolean;
  98. }
  99. export interface TransferState {
  100. data: Array<ResolvedDataItem>;
  101. selectedItems: Map<number | string, ResolvedDataItem>;
  102. searchResult: Set<number | string>;
  103. inputValue: string;
  104. }
  105. export interface TransferProps {
  106. style?: React.CSSProperties;
  107. className?: string;
  108. disabled?: boolean;
  109. dataSource?: DataSource;
  110. filter?: boolean | ((sugInput: string, item: DataItem) => boolean);
  111. defaultValue?: Array<string | number>;
  112. value?: Array<string | number>;
  113. inputProps?: InputProps;
  114. type?: Type;
  115. emptyContent?: EmptyContent;
  116. draggable?: boolean;
  117. treeProps?: Omit<TreeProps, 'value' | 'ref' | 'onChange'>;
  118. showPath?: boolean;
  119. loading?: boolean;
  120. onChange?: (values: Array<string | number>, items: Array<DataItem>) => void;
  121. onSelect?: (item: DataItem) => void;
  122. onDeselect?: (item: DataItem) => void;
  123. onSearch?: (sunInput: string) => void;
  124. renderSourceItem?: (item: RenderSourceItemProps) => React.ReactNode;
  125. renderSelectedItem?: (item: RenderSelectedItemProps) => React.ReactNode;
  126. renderSourcePanel?: (sourcePanelProps: SourcePanelProps) => React.ReactNode;
  127. renderSelectedPanel?: (selectedPanelProps: SelectedPanelProps) => React.ReactNode;
  128. }
  129. const prefixcls = cssClasses.PREFIX;
  130. class Transfer extends BaseComponent<TransferProps, TransferState> {
  131. static propTypes = {
  132. style: PropTypes.object,
  133. className: PropTypes.string,
  134. disabled: PropTypes.bool,
  135. dataSource: PropTypes.array,
  136. filter: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
  137. onSearch: PropTypes.func,
  138. inputProps: PropTypes.object,
  139. value: PropTypes.array,
  140. defaultValue: PropTypes.array,
  141. onChange: PropTypes.func,
  142. onSelect: PropTypes.func,
  143. onDeselect: PropTypes.func,
  144. renderSourceItem: PropTypes.func,
  145. renderSelectedItem: PropTypes.func,
  146. loading: PropTypes.bool,
  147. type: PropTypes.oneOf(['list', 'groupList', 'treeList']),
  148. treeProps: PropTypes.object,
  149. showPath: PropTypes.bool,
  150. emptyContent: PropTypes.shape({
  151. search: PropTypes.node,
  152. left: PropTypes.node,
  153. right: PropTypes.node,
  154. }),
  155. renderSourcePanel: PropTypes.func,
  156. renderSelectedPanel: PropTypes.func,
  157. draggable: PropTypes.bool,
  158. };
  159. static defaultProps = {
  160. type: strings.TYPE_LIST,
  161. dataSource: [] as DataSource,
  162. onSearch: noop,
  163. onChange: noop,
  164. onSelect: noop,
  165. onDeselect: noop,
  166. onClear: noop,
  167. defaultValue: [] as Array<string | number>,
  168. emptyContent: {},
  169. showPath: false,
  170. };
  171. _treeRef: Tree = null;
  172. constructor(props: TransferProps) {
  173. super(props);
  174. const { defaultValue = [], dataSource, type } = props;
  175. this.foundation = new TransferFoundation<TransferProps, TransferState>(this.adapter);
  176. this.state = {
  177. data: [],
  178. selectedItems: new Map(),
  179. searchResult: new Set(),
  180. inputValue: '',
  181. };
  182. if (Boolean(dataSource) && isArray(dataSource)) {
  183. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  184. // @ts-ignore Avoid reporting errors this.state.xxx is read-only
  185. this.state.data = _generateDataByType(dataSource, type);
  186. }
  187. if (Boolean(defaultValue) && isArray(defaultValue)) {
  188. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  189. // @ts-ignore Avoid reporting errors this.state.xxx is read-only
  190. this.state.selectedItems = _generateSelectedItems(defaultValue, this.state.data);
  191. }
  192. this.onSelectOrRemove = this.onSelectOrRemove.bind(this);
  193. this.onInputChange = this.onInputChange.bind(this);
  194. this.onSortEnd = this.onSortEnd.bind(this);
  195. }
  196. static getDerivedStateFromProps(props: TransferProps, state: TransferState) {
  197. const { value, dataSource, type, filter } = props;
  198. const mergedState = {} as TransferState;
  199. let newData = state.data;
  200. let newSelectedItems = state.selectedItems;
  201. if (Boolean(dataSource) && Array.isArray(dataSource)) {
  202. newData = _generateDataByType(dataSource, type);
  203. mergedState.data = newData;
  204. }
  205. if (Boolean(value) && Array.isArray(value)) {
  206. newSelectedItems = _generateSelectedItems(value, newData);
  207. mergedState.selectedItems = newSelectedItems;
  208. }
  209. if (!isEqual(state.data, newData)) {
  210. if (typeof state.inputValue === 'string' && state.inputValue !== '') {
  211. const filterFunc = typeof filter === 'function' ?
  212. (item: DataItem) => filter(state.inputValue, item) :
  213. (item: DataItem) => typeof item.label === 'string' && item.label.includes(state.inputValue);
  214. const searchData = newData.filter(filterFunc);
  215. const searchResult = new Set(searchData.map(item => item.key));
  216. mergedState.searchResult = searchResult;
  217. }
  218. }
  219. return isEmpty(mergedState) ? null : mergedState;
  220. }
  221. get adapter(): TransferAdapter<TransferProps, TransferState> {
  222. return {
  223. ...super.adapter,
  224. getSelected: () => new Map(this.state.selectedItems),
  225. updateSelected: selectedItems => {
  226. this.setState({ selectedItems });
  227. },
  228. notifyChange: (values, items) => {
  229. this.props.onChange(values, items);
  230. },
  231. notifySearch: input => {
  232. this.props.onSearch(input);
  233. },
  234. notifySelect: item => {
  235. this.props.onSelect(item);
  236. },
  237. notifyDeselect: item => {
  238. this.props.onDeselect(item);
  239. },
  240. updateInput: input => {
  241. this.setState({ inputValue: input });
  242. },
  243. updateSearchResult: searchResult => {
  244. this.setState({ searchResult });
  245. },
  246. searchTree: keyword => {
  247. this._treeRef && (this._treeRef as any).search(keyword); // TODO check this._treeRef.current?
  248. }
  249. };
  250. }
  251. onInputChange(value: string) {
  252. this.foundation.handleInputChange(value);
  253. }
  254. onSelectOrRemove(item: ResolvedDataItem) {
  255. this.foundation.handleSelectOrRemove(item);
  256. }
  257. onSortEnd(callbackProps: OnSortEndProps) {
  258. this.foundation.handleSortEnd(callbackProps);
  259. }
  260. renderFilter(locale: Locale['Transfer']) {
  261. const { inputProps, filter, disabled } = this.props;
  262. if (typeof filter === 'boolean' && !filter) {
  263. return null;
  264. }
  265. return (
  266. <div role="search" aria-label="Transfer filter" className={`${prefixcls}-filter`}>
  267. <Input
  268. prefix={<IconSearch />}
  269. placeholder={locale.placeholder}
  270. showClear
  271. value={this.state.inputValue}
  272. disabled={disabled}
  273. onChange={this.onInputChange}
  274. {...inputProps}
  275. />
  276. </div>
  277. );
  278. }
  279. renderHeader(headerConfig: HeaderConfig) {
  280. const { disabled } = this.props;
  281. const { totalContent, allContent, onAllClick, type, showButton } = headerConfig;
  282. const headerCls = cls({
  283. [`${prefixcls}-header`]: true,
  284. [`${prefixcls}-right-header`]: type === 'right',
  285. [`${prefixcls}-left-header`]: type === 'left',
  286. });
  287. return (
  288. <div className={headerCls}>
  289. <span className={`${prefixcls}-header-total`}>{totalContent}</span>
  290. {showButton ? (
  291. <Button
  292. theme="borderless"
  293. disabled={disabled}
  294. type="tertiary"
  295. size="small"
  296. className={`${prefixcls}-header-all`}
  297. onClick={onAllClick}
  298. >
  299. {allContent}
  300. </Button>
  301. ) : null}
  302. </div>
  303. );
  304. }
  305. renderLeftItem(item: ResolvedDataItem, index: number) {
  306. const { renderSourceItem, disabled } = this.props;
  307. const { selectedItems } = this.state;
  308. const checked = selectedItems.has(item.key);
  309. if (renderSourceItem) {
  310. return renderSourceItem({ ...item, checked, onChange: () => this.onSelectOrRemove(item) });
  311. }
  312. const leftItemCls = cls({
  313. [`${prefixcls}-item`]: true,
  314. [`${prefixcls}-item-disabled`]: item.disabled,
  315. });
  316. return (
  317. <Checkbox
  318. key={index}
  319. disabled={item.disabled || disabled}
  320. className={leftItemCls}
  321. checked={checked}
  322. role="listitem"
  323. onChange={() => this.onSelectOrRemove(item)}
  324. >
  325. {item.label}
  326. </Checkbox>
  327. );
  328. }
  329. renderLeft(locale: Locale['Transfer']) {
  330. const { data, selectedItems, inputValue, searchResult } = this.state;
  331. const { loading, type, emptyContent, renderSourcePanel } = this.props;
  332. const totalToken = locale.total;
  333. const inSearchMode = inputValue !== '';
  334. const showNumber = inSearchMode ? searchResult.size : data.length;
  335. const filterData = inSearchMode ? data.filter(item => searchResult.has(item.key)) : data;
  336. // Whether to select all should be a judgment, whether the filtered data on the left is a subset of the selected items
  337. // For example, the filtered data on the left is 1, 3, 4;
  338. // The selected option is 1,2,3,4, it is true
  339. // The selected option is 2,3,4, then it is false
  340. const leftContainesNotInSelected = Boolean(filterData.find(f => !selectedItems.has(f.key)));
  341. const totalText = totalToken.replace('${total}', `${showNumber}`);
  342. const headerConfig: HeaderConfig = {
  343. totalContent: totalText,
  344. allContent: leftContainesNotInSelected ? locale.selectAll : locale.clearSelectAll,
  345. onAllClick: () => this.foundation.handleAll(leftContainesNotInSelected),
  346. type: 'left',
  347. showButton: type !== strings.TYPE_TREE_TO_LIST,
  348. };
  349. const inputCom = this.renderFilter(locale);
  350. const headerCom = this.renderHeader(headerConfig);
  351. const noMatch = inSearchMode && searchResult.size === 0;
  352. const emptySearch = emptyContent.search ? emptyContent.search : locale.emptySearch;
  353. const emptyLeft = emptyContent.left ? emptyContent.left : locale.emptyLeft;
  354. const emptyCom = this.renderEmpty('left', inputValue ? emptySearch : emptyLeft);
  355. const loadingCom = <Spin />;
  356. let content: React.ReactNode = null;
  357. switch (true) {
  358. case loading:
  359. content = loadingCom;
  360. break;
  361. case noMatch:
  362. content = emptyCom;
  363. break;
  364. case type === strings.TYPE_TREE_TO_LIST:
  365. content = (
  366. <>
  367. {headerCom}
  368. {this.renderLeftTree()}
  369. </>
  370. );
  371. break;
  372. case !noMatch && (type === strings.TYPE_LIST || type === strings.TYPE_GROUP_LIST):
  373. content = (
  374. <>
  375. {headerCom}
  376. {this.renderLeftList(filterData)}
  377. </>
  378. );
  379. break;
  380. default:
  381. content = null;
  382. break;
  383. }
  384. const { values } = this.foundation.getValuesAndItemsFromMap(selectedItems);
  385. const renderProps: SourcePanelProps = {
  386. loading,
  387. noMatch,
  388. filterData,
  389. sourceData: data,
  390. allChecked: !leftContainesNotInSelected,
  391. showNumber,
  392. inputValue,
  393. selectedItems,
  394. value: values,
  395. onSelect: this.foundation.handleSelect.bind(this.foundation),
  396. onAllClick: () => this.foundation.handleAll(leftContainesNotInSelected),
  397. onSearch: this.onInputChange,
  398. onSelectOrRemove: (item: ResolvedDataItem) => this.onSelectOrRemove(item),
  399. };
  400. if (renderSourcePanel) {
  401. return renderSourcePanel(renderProps);
  402. }
  403. return (
  404. <section className={`${prefixcls}-left`}>
  405. {inputCom}
  406. {content}
  407. </section>
  408. );
  409. }
  410. renderGroupTitle(group: GroupItem, index: number) {
  411. const groupCls = cls(`${prefixcls }-group-title`);
  412. return (
  413. <div className={groupCls} key={`title-${index}`}>
  414. {group.title}
  415. </div>
  416. );
  417. }
  418. renderLeftTree() {
  419. const { selectedItems } = this.state;
  420. const { disabled, dataSource, treeProps } = this.props;
  421. const { values } = this.foundation.getValuesAndItemsFromMap(selectedItems);
  422. const onChange = (value: TreeValue) => {
  423. this.foundation.handleSelect(value);
  424. };
  425. const restTreeProps = omit(treeProps, ['value', 'ref', 'onChange']);
  426. return (
  427. <Tree
  428. disabled={disabled}
  429. treeData={dataSource as any}
  430. multiple
  431. disableStrictly
  432. value={values}
  433. defaultExpandAll
  434. leafOnly
  435. ref={tree => this._treeRef = tree}
  436. filterTreeNode
  437. searchRender={false}
  438. searchStyle={{ padding: 0 }}
  439. style={{ flex: 1, overflow: 'overlay' }}
  440. onChange={onChange}
  441. {...restTreeProps}
  442. />
  443. );
  444. }
  445. renderLeftList(visibileItems: Array<ResolvedDataItem>) {
  446. const content = [] as Array<React.ReactNode>;
  447. const groupStatus = new Map();
  448. visibileItems.forEach((item, index) => {
  449. const parentGroup = item._parent;
  450. const optionContent = this.renderLeftItem(item, index);
  451. if (parentGroup && groupStatus.has(parentGroup.title)) {
  452. // group content already insert
  453. content.push(optionContent);
  454. } else if (parentGroup) {
  455. const groupContent = this.renderGroupTitle(parentGroup, index);
  456. groupStatus.set(parentGroup.title, true);
  457. content.push(groupContent);
  458. content.push(optionContent);
  459. } else {
  460. content.push(optionContent);
  461. }
  462. });
  463. return <div className={`${prefixcls}-left-list`} role="list" aria-label="Option list">{content}</div>;
  464. }
  465. renderRightItem(item: ResolvedDataItem): React.ReactNode {
  466. const { renderSelectedItem, draggable, type, showPath } = this.props;
  467. let newItem = item;
  468. if (draggable) {
  469. newItem = { ...item, key: item._optionKey };
  470. delete newItem._optionKey;
  471. }
  472. const onRemove = () => this.foundation.handleSelectOrRemove(newItem);
  473. const rightItemCls = cls({
  474. [`${prefixcls}-item`]: true,
  475. [`${prefixcls}-right-item`]: true,
  476. [`${prefixcls}-right-item-draggable`]: draggable
  477. });
  478. const shouldShowPath = type === strings.TYPE_TREE_TO_LIST && showPath === true;
  479. const label = shouldShowPath ? this.foundation._generatePath(item) : item.label;
  480. if (renderSelectedItem) {
  481. return renderSelectedItem({ ...item, onRemove, sortableHandle: SortableHandle });
  482. }
  483. const DragHandle = SortableHandle(() => (
  484. <IconHandle role="button" aria-label="Drag and sort" className={`${prefixcls}-right-item-drag-handler`} />
  485. ));
  486. return (
  487. // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
  488. <div role="listitem" className={rightItemCls} key={newItem.key}>
  489. {draggable ? <DragHandle /> : null}
  490. <div className={`${prefixcls}-right-item-text`}>{label}</div>
  491. <IconClose
  492. onClick={onRemove}
  493. aria-disabled={item.disabled}
  494. className={cls(`${prefixcls}-item-close-icon`, {
  495. [`${prefixcls}-item-close-icon-disabled`]: item.disabled
  496. })}
  497. />
  498. </div>
  499. );
  500. }
  501. renderEmpty(type: string, emptyText: React.ReactNode) {
  502. const emptyCls = cls({
  503. [`${prefixcls}-empty`]: true,
  504. [`${prefixcls}-right-empty`]: type === 'right',
  505. [`${prefixcls}-left-empty`]: type === 'left',
  506. });
  507. return <div aria-label="empty" className={emptyCls}>{emptyText}</div>;
  508. }
  509. renderRightSortableList(selectedData: Array<ResolvedDataItem>) {
  510. // when choose some items && draggable is true
  511. const SortableItem = SortableElement((
  512. (item: ResolvedDataItem) => this.renderRightItem(item)) as React.SFC<ResolvedDataItem>
  513. );
  514. const SortableList = SortableContainer(({ items }: { items: Array<ResolvedDataItem> }) => (
  515. <div className={`${prefixcls}-right-list`} role="list" aria-label="Selected list">
  516. {items.map((item, index: number) => (
  517. // sortableElement will take over the property 'key', so use another '_optionKey' to pass
  518. <SortableItem key={item.label} index={index} {...item} _optionKey={item.key} />
  519. ))}
  520. </div>
  521. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  522. // @ts-ignore see reasons: https://github.com/clauderic/react-sortable-hoc/issues/206
  523. ), { distance: 10 });
  524. const sortList = <SortableList useDragHandle onSortEnd={this.onSortEnd} items={selectedData} />;
  525. return sortList;
  526. }
  527. renderRight(locale: Locale['Transfer']) {
  528. const { selectedItems } = this.state;
  529. const { emptyContent, renderSelectedPanel, draggable } = this.props;
  530. const selectedData = [...selectedItems.values()];
  531. // when custom render panel
  532. const renderProps: SelectedPanelProps = {
  533. length: selectedData.length,
  534. selectedData,
  535. onClear: () => this.foundation.handleClear(),
  536. onRemove: item => this.foundation.handleSelectOrRemove(item),
  537. onSortEnd: props => this.onSortEnd(props)
  538. };
  539. if (renderSelectedPanel) {
  540. return renderSelectedPanel(renderProps);
  541. }
  542. const selectedToken = locale.selected;
  543. const selectedText = selectedToken.replace('${total}', `${selectedData.length}`);
  544. const headerConfig = {
  545. totalContent: selectedText,
  546. allContent: locale.clear,
  547. onAllClick: () => this.foundation.handleClear(),
  548. type: 'right',
  549. showButton: Boolean(selectedData.length),
  550. };
  551. const headerCom = this.renderHeader(headerConfig);
  552. const emptyCom = this.renderEmpty('right', emptyContent.right ? emptyContent.right : locale.emptyRight);
  553. const panelCls = `${prefixcls}-right`;
  554. let content = null;
  555. switch (true) {
  556. // when empty
  557. case !selectedData.length:
  558. content = emptyCom;
  559. break;
  560. case selectedData.length && !draggable:
  561. const list = (
  562. <div className={`${prefixcls}-right-list`} role="list" aria-label="Selected list">
  563. {selectedData.map(item => this.renderRightItem({ ...item }))}
  564. </div>
  565. );
  566. content = list;
  567. break;
  568. case selectedData.length && draggable:
  569. content = this.renderRightSortableList(selectedData);
  570. break;
  571. default:
  572. break;
  573. }
  574. return (
  575. <section className={panelCls}>
  576. {headerCom}
  577. {content}
  578. </section>
  579. );
  580. }
  581. render() {
  582. const { className, style, disabled, renderSelectedPanel, renderSourcePanel } = this.props;
  583. const transferCls = cls(prefixcls, className, {
  584. [`${prefixcls}-disabled`]: disabled,
  585. [`${prefixcls}-custom-panel`]: renderSelectedPanel && renderSourcePanel,
  586. });
  587. return (
  588. <LocaleConsumer componentName="Transfer">
  589. {(locale: Locale['Transfer']) => (
  590. <div className={transferCls} style={style}>
  591. {this.renderLeft(locale)}
  592. {this.renderRight(locale)}
  593. </div>
  594. )}
  595. </LocaleConsumer>
  596. );
  597. }
  598. }
  599. export default Transfer;