import React, { Component, PropTypes } from 'react'; import List from './list.jsx'; import Button from '../button'; function noop() { } let mockData = []; for (let i = 0; i < 10; i++) { mockData.push({ title: '内容' + (i + 1), value: (i + 1), description: '内容' + (i + 1) + '的描述', chosen: Math.random() * 2 > 1 }); } class Transfer extends Component { constructor(props) { super(props); this.state = { dataSource: props.dataSource, }; } moveTo(direction) { // TODO: 讨论是否要将新移动的元素置顶 const { filterKey } = this.props; let { dataSource } = this.state; if ( direction === 'right' ) { dataSource.forEach((data) => { // 左边向右移动 if ( !data[filterKey] && data.checked ) { data[filterKey] = true; data.checked = false; } }); } else { dataSource.forEach((data) => { if ( data[filterKey] && data.checked ) { data[filterKey] = false; data.checked = false; } }); } this.setState({ dataSource: dataSource, }); } handleSelectAll(direction, globalCheckStatus) { const { filterKey } = this.props; const { dataSource } = this.state; switch ( globalCheckStatus ) { // 选中部分,则全选 case 'part': case 'none': dataSource.forEach((data)=> { // data[filterKey] true 时,在右侧 if ( direction === 'right' && data[filterKey] || direction === 'left' && !data[filterKey] ) { data.checked = true; } }); break; case 'all': dataSource.forEach((data)=> { if ( direction === 'right' && data[filterKey] || direction === 'left' && !data[filterKey] ) { data.checked = false; } }); break; default: break; } this.setState({ dataSource: dataSource, }); } handleSelect(selectedItem, checked) { const { dataSource } = this.state; dataSource.forEach((data)=> { if ( data.value === selectedItem.value ) { data.checked = checked; } }); this.setState({ dataSource: dataSource, }); } render() { const { prefixCls, leftConfig, rightConfig, filterKey } = this.props; const { dataSource } = this.state; let leftDataSource = []; let rightDataSource = []; dataSource.map((item)=> { if ( item[filterKey] ) { rightDataSource.push(item); } else { leftDataSource.push(item); } }); return