index.tsx 25 KB

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