index.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. <Checkbox
  325. key={index}
  326. disabled={item.disabled || disabled}
  327. className={leftItemCls}
  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. );
  336. }
  337. renderLeft(locale: Locale['Transfer']) {
  338. const { data, selectedItems, inputValue, searchResult } = this.state;
  339. const { loading, type, emptyContent, renderSourcePanel, dataSource } = this.props;
  340. const totalToken = locale.total;
  341. const inSearchMode = inputValue !== '';
  342. const showNumber = inSearchMode ? searchResult.size : data.length;
  343. const filterData = inSearchMode ? data.filter(item => searchResult.has(item.key)) : data;
  344. // Whether to select all should be a judgment, whether the filtered data on the left is a subset of the selected items
  345. // For example, the filtered data on the left is 1, 3, 4;
  346. // The selected option is 1,2,3,4, it is true
  347. // The selected option is 2,3,4, then it is false
  348. const leftContainesNotInSelected = Boolean(filterData.find(f => !selectedItems.has(f.key)));
  349. const totalText = totalToken.replace('${total}', `${showNumber}`);
  350. const headerConfig: HeaderConfig = {
  351. totalContent: totalText,
  352. allContent: leftContainesNotInSelected ? locale.selectAll : locale.clearSelectAll,
  353. onAllClick: () => this.foundation.handleAll(leftContainesNotInSelected),
  354. type: 'left',
  355. showButton: type !== strings.TYPE_TREE_TO_LIST,
  356. };
  357. const inputCom = this.renderFilter(locale);
  358. const headerCom = this.renderHeader(headerConfig);
  359. const noMatch = inSearchMode && searchResult.size === 0;
  360. const emptySearch = emptyContent.search ? emptyContent.search : locale.emptySearch;
  361. const emptyLeft = emptyContent.left ? emptyContent.left : locale.emptyLeft;
  362. const emptyDataCom = this.renderEmpty('left', emptyLeft);
  363. const emptySearchCom = this.renderEmpty('left', emptySearch);
  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 = emptySearchCom;
  372. break;
  373. case data.length === 0:
  374. content = emptyDataCom;
  375. break;
  376. case type === strings.TYPE_TREE_TO_LIST:
  377. content = (
  378. <>
  379. {headerCom}
  380. {this.renderLeftTree()}
  381. </>
  382. );
  383. break;
  384. case !noMatch && (type === strings.TYPE_LIST || type === strings.TYPE_GROUP_LIST):
  385. content = (
  386. <>
  387. {headerCom}
  388. {this.renderLeftList(filterData)}
  389. </>
  390. );
  391. break;
  392. default:
  393. content = null;
  394. break;
  395. }
  396. const { values } = this.foundation.getValuesAndItemsFromMap(selectedItems);
  397. const renderProps: SourcePanelProps = {
  398. loading,
  399. noMatch,
  400. filterData,
  401. sourceData: data,
  402. propsDataSource: dataSource,
  403. allChecked: !leftContainesNotInSelected,
  404. showNumber,
  405. inputValue,
  406. selectedItems,
  407. value: values,
  408. onSelect: this.foundation.handleSelect.bind(this.foundation),
  409. onAllClick: () => this.foundation.handleAll(leftContainesNotInSelected),
  410. onSearch: this.onInputChange,
  411. onSelectOrRemove: (item: ResolvedDataItem) => this.onSelectOrRemove(item),
  412. };
  413. if (renderSourcePanel) {
  414. return renderSourcePanel(renderProps);
  415. }
  416. return (
  417. <section className={`${prefixcls}-left`}>
  418. {inputCom}
  419. {content}
  420. </section>
  421. );
  422. }
  423. renderGroupTitle(group: GroupItem, index: number) {
  424. const groupCls = cls(`${prefixcls }-group-title`);
  425. return (
  426. <div className={groupCls} key={`title-${index}`}>
  427. {group.title}
  428. </div>
  429. );
  430. }
  431. renderLeftTree() {
  432. const { selectedItems } = this.state;
  433. const { disabled, dataSource, treeProps } = this.props;
  434. const { values } = this.foundation.getValuesAndItemsFromMap(selectedItems);
  435. const onChange = (value: TreeValue) => {
  436. this.foundation.handleSelect(value);
  437. };
  438. const restTreeProps = omit(treeProps, ['value', 'ref', 'onChange']);
  439. return (
  440. <Tree
  441. disabled={disabled}
  442. treeData={dataSource as any}
  443. multiple
  444. disableStrictly
  445. value={values}
  446. defaultExpandAll
  447. leafOnly
  448. ref={tree => this._treeRef = tree}
  449. filterTreeNode
  450. searchRender={false}
  451. searchStyle={{ padding: 0 }}
  452. style={{ flex: 1, overflow: 'overlay' }}
  453. onChange={onChange}
  454. {...restTreeProps}
  455. />
  456. );
  457. }
  458. renderLeftList(visibileItems: Array<ResolvedDataItem>) {
  459. const content = [] as Array<React.ReactNode>;
  460. const groupStatus = new Map();
  461. visibileItems.forEach((item, index) => {
  462. const parentGroup = item._parent;
  463. const optionContent = this.renderLeftItem(item, index);
  464. if (parentGroup && groupStatus.has(parentGroup.title)) {
  465. // group content already insert
  466. content.push(optionContent);
  467. } else if (parentGroup) {
  468. const groupContent = this.renderGroupTitle(parentGroup, index);
  469. groupStatus.set(parentGroup.title, true);
  470. content.push(groupContent);
  471. content.push(optionContent);
  472. } else {
  473. content.push(optionContent);
  474. }
  475. });
  476. return <div className={`${prefixcls}-left-list`} role="list" aria-label="Option list">{content}</div>;
  477. }
  478. renderRightItem(item: ResolvedDataItem): React.ReactNode {
  479. const { renderSelectedItem, draggable, type, showPath } = this.props;
  480. const onRemove = () => this.foundation.handleSelectOrRemove(item);
  481. const rightItemCls = cls({
  482. [`${prefixcls}-item`]: true,
  483. [`${prefixcls}-right-item`]: true,
  484. [`${prefixcls}-right-item-draggable`]: draggable
  485. });
  486. const shouldShowPath = type === strings.TYPE_TREE_TO_LIST && showPath === true;
  487. const label = shouldShowPath ? this.foundation._generatePath(item) : item.label;
  488. if (renderSelectedItem) {
  489. return renderSelectedItem({ ...item, onRemove, sortableHandle: SortableHandle });
  490. }
  491. const DragHandle = SortableHandle(() => (
  492. <IconHandle role="button" aria-label="Drag and sort" className={`${prefixcls}-right-item-drag-handler`} />
  493. ));
  494. return (
  495. // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
  496. <div role="listitem" className={rightItemCls} key={item.key}>
  497. {draggable ? <DragHandle /> : null}
  498. <div className={`${prefixcls}-right-item-text`}>{label}</div>
  499. <IconClose
  500. onClick={onRemove}
  501. aria-disabled={item.disabled}
  502. className={cls(`${prefixcls}-item-close-icon`, {
  503. [`${prefixcls}-item-close-icon-disabled`]: item.disabled
  504. })}
  505. />
  506. </div>
  507. );
  508. }
  509. renderEmpty(type: string, emptyText: React.ReactNode) {
  510. const emptyCls = cls({
  511. [`${prefixcls}-empty`]: true,
  512. [`${prefixcls}-right-empty`]: type === 'right',
  513. [`${prefixcls}-left-empty`]: type === 'left',
  514. });
  515. return <div aria-label="empty" className={emptyCls}>{emptyText}</div>;
  516. }
  517. renderRightSortableList(selectedData: Array<ResolvedDataItem>) {
  518. // when choose some items && draggable is true
  519. const SortableItem = SortableElement((
  520. (props: DraggableResolvedDataItem) => this.renderRightItem(props.item)) as React.FC<DraggableResolvedDataItem>
  521. );
  522. const SortableList = SortableContainer(({ items }: { items: Array<ResolvedDataItem> }) => (
  523. <div className={`${prefixcls}-right-list`} role="list" aria-label="Selected list">
  524. {items.map((item, index: number) => (
  525. // @ts-ignore skip SortableItem type check
  526. <SortableItem key={item.label} index={index} item={item} />
  527. ))}
  528. </div>
  529. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  530. // @ts-ignore see reasons: https://github.com/clauderic/react-sortable-hoc/issues/206
  531. ), { distance: 10 });
  532. // @ts-ignore skip SortableItem type check
  533. const sortList = <SortableList useDragHandle onSortEnd={this.onSortEnd} items={selectedData} />;
  534. return sortList;
  535. }
  536. renderRight(locale: Locale['Transfer']) {
  537. const { selectedItems } = this.state;
  538. const { emptyContent, renderSelectedPanel, draggable } = this.props;
  539. const selectedData = [...selectedItems.values()];
  540. // when custom render panel
  541. const renderProps: SelectedPanelProps = {
  542. length: selectedData.length,
  543. selectedData,
  544. onClear: () => this.foundation.handleClear(),
  545. onRemove: item => this.foundation.handleSelectOrRemove(item),
  546. onSortEnd: props => this.onSortEnd(props)
  547. };
  548. if (renderSelectedPanel) {
  549. return renderSelectedPanel(renderProps);
  550. }
  551. const selectedToken = locale.selected;
  552. const selectedText = selectedToken.replace('${total}', `${selectedData.length}`);
  553. const headerConfig = {
  554. totalContent: selectedText,
  555. allContent: locale.clear,
  556. onAllClick: () => this.foundation.handleClear(),
  557. type: 'right',
  558. showButton: Boolean(selectedData.length),
  559. };
  560. const headerCom = this.renderHeader(headerConfig);
  561. const emptyCom = this.renderEmpty('right', emptyContent.right ? emptyContent.right : locale.emptyRight);
  562. const panelCls = `${prefixcls}-right`;
  563. let content = null;
  564. switch (true) {
  565. // when empty
  566. case !selectedData.length:
  567. content = emptyCom;
  568. break;
  569. case selectedData.length && !draggable:
  570. const list = (
  571. <div className={`${prefixcls}-right-list`} role="list" aria-label="Selected list">
  572. {selectedData.map(item => this.renderRightItem({ ...item }))}
  573. </div>
  574. );
  575. content = list;
  576. break;
  577. case selectedData.length && draggable:
  578. content = this.renderRightSortableList(selectedData);
  579. break;
  580. default:
  581. break;
  582. }
  583. return (
  584. <section className={panelCls}>
  585. {headerCom}
  586. {content}
  587. </section>
  588. );
  589. }
  590. render() {
  591. const { className, style, disabled, renderSelectedPanel, renderSourcePanel } = this.props;
  592. const transferCls = cls(prefixcls, className, {
  593. [`${prefixcls}-disabled`]: disabled,
  594. [`${prefixcls}-custom-panel`]: renderSelectedPanel && renderSourcePanel,
  595. });
  596. return (
  597. <LocaleConsumer componentName="Transfer">
  598. {(locale: Locale['Transfer']) => (
  599. <div className={transferCls} style={style}>
  600. {this.renderLeft(locale)}
  601. {this.renderRight(locale)}
  602. </div>
  603. )}
  604. </LocaleConsumer>
  605. );
  606. }
  607. }
  608. export default Transfer;