feat(transfer): add custom render support

This commit is contained in:
jasonslyvia 2016-05-12 11:09:27 +08:00
parent 1d5e84f15c
commit f4a3b64e14
2 changed files with 44 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import React, { PropTypes } from 'react'; import React, { PropTypes } from 'react';
import List from './list'; import List, { isRenderResultPlainObject } from './list';
import Operation from './operation'; import Operation from './operation';
import Search from './search'; import Search from './search';
import classNames from 'classnames'; import classNames from 'classnames';
@ -23,7 +23,7 @@ export default class Transfer extends React.Component {
showSearch: false, showSearch: false,
body: noop, body: noop,
footer: noop, footer: noop,
} };
static propTypes = { static propTypes = {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
@ -41,7 +41,7 @@ export default class Transfer extends React.Component {
notFoundContent: PropTypes.node, notFoundContent: PropTypes.node,
body: PropTypes.func, body: PropTypes.func,
footer: PropTypes.func, footer: PropTypes.func,
} };
constructor(props) { constructor(props) {
super(props); super(props);
@ -129,7 +129,14 @@ export default class Transfer extends React.Component {
filterDataSource(dataSource, filter) { filterDataSource(dataSource, filter) {
return dataSource.filter(item => { return dataSource.filter(item => {
const itemText = this.props.render(item); const renderResult = this.props.render(item);
let itemText;
if (isRenderResultPlainObject(renderResult)) {
itemText = renderResult.value;
} else {
itemText = renderResult;
}
return this.matchFilter(itemText, filter); return this.matchFilter(itemText, filter);
}); });
} }
@ -204,6 +211,7 @@ export default class Transfer extends React.Component {
const { const {
prefixCls, titles, operations, showSearch, notFoundContent, prefixCls, titles, operations, showSearch, notFoundContent,
searchPlaceholder, body, footer, listStyle, className, searchPlaceholder, body, footer, listStyle, className,
render,
} = this.props; } = this.props;
const { leftFilter, rightFilter, leftCheckedKeys, rightCheckedKeys } = this.state; const { leftFilter, rightFilter, leftCheckedKeys, rightCheckedKeys } = this.state;
@ -232,7 +240,7 @@ export default class Transfer extends React.Component {
handleSelect={this.handleLeftSelect} handleSelect={this.handleLeftSelect}
handleSelectAll={this.handleLeftSelectAll} handleSelectAll={this.handleLeftSelectAll}
position="left" position="left"
render={this.props.render} render={render}
showSearch={showSearch} showSearch={showSearch}
searchPlaceholder={searchPlaceholder} searchPlaceholder={searchPlaceholder}
notFoundContent={notFoundContent} notFoundContent={notFoundContent}
@ -257,7 +265,7 @@ export default class Transfer extends React.Component {
handleSelect={this.handleRightSelect} handleSelect={this.handleRightSelect}
handleSelectAll={this.handleRightSelectAll} handleSelectAll={this.handleRightSelectAll}
position="right" position="right"
render={this.props.render} render={render}
showSearch={showSearch} showSearch={showSearch}
searchPlaceholder={searchPlaceholder} searchPlaceholder={searchPlaceholder}
notFoundContent={notFoundContent} notFoundContent={notFoundContent}

View File

@ -7,6 +7,11 @@ import Animate from 'rc-animate';
function noop() { function noop() {
} }
export function isRenderResultPlainObject(result) {
return result && !React.isValidElement(result) &&
Object.prototype.toString.call(result) === '[object Object]';
}
export default class TransferList extends React.Component { export default class TransferList extends React.Component {
static defaultProps = { static defaultProps = {
dataSource: [], dataSource: [],
@ -19,7 +24,7 @@ export default class TransferList extends React.Component {
// advanced // advanced
body: noop, body: noop,
footer: noop, footer: noop,
} };
static propTypes = { static propTypes = {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
@ -34,11 +39,11 @@ export default class TransferList extends React.Component {
render: PropTypes.func, render: PropTypes.func,
body: PropTypes.func, body: PropTypes.func,
footer: PropTypes.func, footer: PropTypes.func,
} };
static contextTypes = { static contextTypes = {
antLocale: React.PropTypes.object, antLocale: React.PropTypes.object,
} };
constructor(props) { constructor(props) {
super(props); super(props);
@ -101,7 +106,7 @@ export default class TransferList extends React.Component {
render() { render() {
const { prefixCls, dataSource, titleText, filter, checkedKeys, const { prefixCls, dataSource, titleText, filter, checkedKeys,
checkStatus, body, footer, showSearch } = this.props; checkStatus, body, footer, showSearch, render } = this.props;
let { searchPlaceholder, notFoundContent } = this.props; let { searchPlaceholder, notFoundContent } = this.props;
@ -115,15 +120,33 @@ export default class TransferList extends React.Component {
}); });
const showItems = dataSource.filter((item) => { const showItems = dataSource.filter((item) => {
const itemText = this.props.render(item); const renderResult = render(item);
let itemText;
if (isRenderResultPlainObject(renderResult)) {
itemText = renderResult.value;
} else {
itemText = renderResult;
}
const filterResult = this.matchFilter(itemText, filter); const filterResult = this.matchFilter(itemText, filter);
return !!filterResult; return !!filterResult;
}).map((item) => { }).map((item) => {
const renderedText = this.props.render(item); const renderResult = render(item);
let renderedText;
let renderedEl;
if (isRenderResultPlainObject(renderResult)) {
renderedText = renderResult.value;
renderedEl = renderResult.label;
} else {
renderedText = renderResult;
renderedEl = renderResult;
}
return ( return (
<li onClick={() => { this.handleSelect(item); }} key={item.key} title={renderedText}> <li onClick={() => { this.handleSelect(item); }} key={item.key} title={renderedText}>
<Checkbox checked={checkedKeys.some(key => key === item.key)} /> <Checkbox checked={checkedKeys.some(key => key === item.key)} />
<span>{renderedText}</span> <span>{renderedEl}</span>
</li> </li>
); );
}); });