Transfer.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import { Form, Row, Col, Transfer } from 'antd'
  22. import { FormComponentProps } from 'antd/lib/form/Form'
  23. interface ICommonTransferProps {
  24. groupSource?: any[]
  25. groupTarget?: any[]
  26. onGroupChange?: (targets) => any
  27. dataSource?: any[]
  28. optionTitle: (item) => any
  29. adminTargetKeys: any[]
  30. targetKeys: any[]
  31. setTargetKeys: (newTargetKeys: any) => any
  32. rowKeys: (item) => any
  33. }
  34. interface ICommonTransferState {
  35. dataSource: any[]
  36. targetKeys: any[]
  37. }
  38. export class CommonTransfer extends React.PureComponent<ICommonTransferProps & FormComponentProps, ICommonTransferState> {
  39. constructor (props) {
  40. super(props)
  41. this.state = {
  42. dataSource: [],
  43. targetKeys: []
  44. }
  45. }
  46. public componentDidMount () {
  47. const {dataSource} = this.props
  48. this.setState({
  49. dataSource: this.addKeyInDatasource(dataSource as any[])
  50. })
  51. }
  52. private addKeyInDatasource (dataSource) {
  53. return dataSource.map((o, index) => ({...o, ...{key: `dataSource${index}`}}))
  54. }
  55. public componentWillReceiveProps (nextProps) {
  56. const {dataSource} = nextProps
  57. if (dataSource !== this.props.dataSource) {
  58. this.setState({
  59. dataSource: this.addKeyInDatasource(dataSource as any[])
  60. })
  61. }
  62. }
  63. private handleChange = (targetKeys) => {
  64. this.props.setTargetKeys(targetKeys)
  65. }
  66. public render () {
  67. return (
  68. <Row>
  69. <Col span={24}>
  70. <Transfer
  71. showSearch
  72. titles={['列表', '已选']}
  73. listStyle={{width: '210px'}}
  74. rowKey={this.props.rowKeys}
  75. dataSource={this.state.dataSource}
  76. targetKeys={this.props.targetKeys}
  77. onChange={this.handleChange}
  78. // onSearch={this.handleSearch}
  79. render={this.props.optionTitle}
  80. />
  81. </Col>
  82. </Row>
  83. )
  84. }
  85. }
  86. export default Form.create<ICommonTransferProps & FormComponentProps>()(CommonTransfer)