@ -8,6 +8,7 @@ import warning from '../_util/warning';
import LocaleReceiver from '../locale-provider/LocaleReceiver' ;
import defaultLocale from '../locale-provider/default' ;
import { ConfigConsumer , ConfigConsumerProps , RenderEmptyHandler } from '../config-provider' ;
import { polyfill } from 'react-lifecycles-compat' ;
export { TransferListProps } from './list' ;
export { TransferOperationProps } from './operation' ;
@ -62,7 +63,7 @@ export interface TransferLocale {
itemsUnit : string ;
}
export default class Transfer extends React . Component < TransferProps , any > {
class Transfer extends React . Component < TransferProps , any > {
// For high-level customized Transfer @dqaria
static List = List ;
static Operation = Operation ;
@ -100,6 +101,17 @@ export default class Transfer extends React.Component<TransferProps, any> {
lazy : PropTypes.oneOfType ( [ PropTypes . object , PropTypes . bool ] ) ,
} ;
static getDerivedStateFromProps ( nextProps : TransferProps ) {
if ( nextProps . selectedKeys ) {
const targetKeys = nextProps . targetKeys || [ ] ;
return {
sourceSelectedKeys : nextProps.selectedKeys.filter ( key = > ! targetKeys . includes ( key ) ) ,
targetSelectedKeys : nextProps.selectedKeys.filter ( key = > targetKeys . includes ( key ) ) ,
} ;
}
return null ;
}
separatedDataSource : {
leftDataSource : TransferItem [ ] ;
rightDataSource : TransferItem [ ] ;
@ -124,24 +136,23 @@ export default class Transfer extends React.Component<TransferProps, any> {
} ;
}
componentWillReceiveProps ( nextProps : TransferProps ) {
const { sourceSelectedKeys , targetSelectedKeys } = this . state ;
componentDidUpdate = ( prevProps : TransferProps , prevState : any ) = > {
const { sourceSelectedKeys , targetSelectedKeys } = prevState ;
if (
next Props. targetKeys !== this . props . targetKeys ||
next Props. dataSource !== this . props . dataSource
prev Props. targetKeys !== this . props . targetKeys ||
prev Props. dataSource !== this . props . dataSource
) {
// clear cached separated dataSource
this . separatedDataSource = null ;
if ( ! next Props. selectedKeys ) {
if ( ! prev Props. selectedKeys ) {
// clear key no longer existed
// clear checkedKeys according to targetKeys
const { dataSource , targetKeys = [ ] } = next Props;
const { dataSource , targetKeys = [ ] } = prev Props;
const newSourceSelectedKeys : String [ ] = [ ] ;
const newTargetSelectedKeys : String [ ] = [ ] ;
dataSource . forEach ( ( { key } ) = > {
dataSource . forEach ( ( { key } : { key : string } ) = > {
if ( sourceSelectedKeys . includes ( key ) && ! targetKeys . includes ( key ) ) {
newSourceSelectedKeys . push ( key ) ;
}
@ -155,15 +166,7 @@ export default class Transfer extends React.Component<TransferProps, any> {
} ) ;
}
}
if ( nextProps . selectedKeys ) {
const targetKeys = nextProps . targetKeys || [ ] ;
this . setState ( {
sourceSelectedKeys : nextProps.selectedKeys.filter ( key = > ! targetKeys . includes ( key ) ) ,
targetSelectedKeys : nextProps.selectedKeys.filter ( key = > targetKeys . includes ( key ) ) ,
} ) ;
}
}
} ;
separateDataSource ( props : TransferProps ) {
if ( this . separatedDataSource ) {
@ -464,3 +467,7 @@ export default class Transfer extends React.Component<TransferProps, any> {
) ;
}
}
polyfill ( Transfer ) ;
export default Transfer ;