Cell.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { EditableCellInputTypes } from './types'
  22. import { Input, Form, InputNumber, Checkbox } from 'antd'
  23. const FormItem = Form.Item
  24. import { WrappedFormUtils } from 'antd/lib/form/Form'
  25. import { EditableContext } from './util'
  26. const renderInputControl = (inputType: EditableCellInputTypes, autoFocus: boolean) => {
  27. switch (inputType) {
  28. case 'input':
  29. return <Input autoFocus={autoFocus} />
  30. case 'inputNumber':
  31. return <InputNumber />
  32. case 'checkbox':
  33. return <Checkbox />
  34. // @TODO other inputType cell render
  35. default:
  36. return <Input autoFocus={autoFocus}/>
  37. }
  38. }
  39. interface IEditableCellProps<T> {
  40. editing: boolean
  41. dataIndex: string
  42. inputType: EditableCellInputTypes
  43. record: T
  44. index: number
  45. autoFocus: boolean
  46. }
  47. const EditableCell: React.FC<IEditableCellProps<object>> = (props) => {
  48. const { editing, dataIndex, inputType, record, index, autoFocus, children, ...restProps } = props
  49. const renderCell = (form: WrappedFormUtils) => (
  50. <td {...restProps}>
  51. {editing ? (
  52. <FormItem style={{ margin: 0 }}>
  53. {form.getFieldDecorator(dataIndex, {
  54. rules: [{ required: true, message: '不能为空' }],
  55. initialValue: record[dataIndex],
  56. valuePropName: inputType === 'checkbox' ? 'checked' : 'value'
  57. })(renderInputControl(inputType, !!autoFocus))}
  58. </FormItem>
  59. ) : children}
  60. </td>
  61. )
  62. return (
  63. <EditableContext.Consumer>
  64. {renderCell}
  65. </EditableContext.Consumer>
  66. )
  67. }
  68. export default EditableCell