index.tsx 25 KB

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