import * as React from 'react'; import { Button } from 'antd'; import './Transfer.less'; import TransferList from './TransferList'; import { createGetClassName } from '../../util/util'; const getClassName = createGetClassName('transfer'); export interface TransferProps { dataSource: TransferDataSource[]; checkedKeys: any[]; onChange: (moveKeys: any, direction: 1 | -1) => void; titles: string[]; operationTexts?: string[]; otherOperations?: { key: string; render: React.ReactNode; }[]; disabled?: boolean; listStyle?: React.CSSProperties; targetDrag?: boolean; onTargetDragEnd?: (fromIndex: number, toIndex: number) => void; search?: boolean; } export interface TransferDataSource { key: any; text: string; } export interface MyState { originCheckedKeys: any[]; targetCheckedKeys: any[]; searchValue: string; } export default class Transfer extends React.Component> { static defaultProps = { dataSource: [], checkedKeys: [], operations: null, otherOperations: [], disabled: false, targetDrag: false, search: false, }; constructor(props) { super(props); this.state = { originCheckedKeys: [], targetCheckedKeys: [], searchValue: null, }; } handleOriginSelectAll = checked => { const { originDataSource } = this.getDataSource(); this.setState({ originCheckedKeys: checked ? originDataSource.map(item => item.key) : [], }); } handleOriginSelect = (key, checked) => { let newOriginCheckedKeys = []; if (checked) { newOriginCheckedKeys = this.state.originCheckedKeys.concat([key]); } else { newOriginCheckedKeys = this.state.originCheckedKeys.filter(item => item !== key); } this.setState({ originCheckedKeys: newOriginCheckedKeys, }); } handleTargetSelect = (key, checked) => { let newTargetCheckedKeys = []; if (checked) { newTargetCheckedKeys = this.state.targetCheckedKeys.concat([key]); } else { newTargetCheckedKeys = this.state.targetCheckedKeys.filter(item => item !== key); } this.setState({ targetCheckedKeys: newTargetCheckedKeys, }); } handleTargetSelectAll = checked => { const { targetDataSource } = this.getDataSource(); this.setState({ targetCheckedKeys: checked ? targetDataSource.map(item => item.key) : [], }); } handleSearch = value => { this.setState({ searchValue: value }); } getDataSource = () => { const originDataSource = []; const targetDataSource = []; const { dataSource, checkedKeys } = this.props; dataSource.forEach(item => { if (checkedKeys.indexOf(item.key) >= 0) { targetDataSource.push(item); } else { if (this.state.searchValue) { if (item.text.indexOf(this.state.searchValue) > -1) { originDataSource.push(item); } } else { originDataSource.push(item); } } }); targetDataSource.sort((a, b) => checkedKeys.indexOf(a.key) - checkedKeys.indexOf(b.key)); return { originDataSource, targetDataSource, }; } handleLeftClick = () => { this.props.onChange(this.state.originCheckedKeys, 1); this.setState({ originCheckedKeys: [], }); } handleRightClick = () => { this.props.onChange(this.state.targetCheckedKeys, -1); this.setState({ targetCheckedKeys: [], }); } renderOtherOperations = () => { return this.props.otherOperations.map(item => { return (
{item.render}
); }); } render() { const { originDataSource, targetDataSource } = this.getDataSource(); return (
{this.renderOtherOperations()}
); } }