DictDataFormModal.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 { Modal, Form, Button, Input, Switch } from 'antd'
  22. import { FormComponentProps } from 'antd/lib/form'
  23. import { IDictData } from '../types'
  24. const FormItem = Form.Item
  25. interface IDictTypeFormModalProps extends FormComponentProps<IDictData> {
  26. visible: boolean
  27. loading: boolean
  28. fromView: IDictData
  29. onSave: (view: IDictData) => void
  30. onCancel: () => void
  31. }
  32. export class DictTypeFormModal extends React.PureComponent<IDictTypeFormModalProps> {
  33. private formItemStyle = {
  34. labelCol: { span: 6 },
  35. wrapperCol: { span: 18 }
  36. }
  37. private save = () => {
  38. const { form, fromView, onSave } = this.props
  39. form.validateFieldsAndScroll((err, fieldsValue) => {
  40. if (err) {
  41. return
  42. }
  43. const copyView: IDictData = { ...fieldsValue }
  44. onSave(copyView)
  45. })
  46. }
  47. private clearFieldsValue = () => {
  48. this.props.form.resetFields()
  49. }
  50. public render() {
  51. const { form, visible, loading, fromView, onCancel } = this.props
  52. const { getFieldDecorator } = form
  53. const modalButtons = [
  54. <Button key="back" size="large" onClick={onCancel}>
  55. 取 消
  56. </Button>,
  57. <Button
  58. disabled={loading}
  59. key="submit"
  60. size="large"
  61. type="primary"
  62. onClick={this.save}
  63. >
  64. 保 存
  65. </Button>
  66. ]
  67. return (
  68. <Modal
  69. title={fromView ? '编辑' : '新增'}
  70. visible={visible}
  71. footer={modalButtons}
  72. onCancel={onCancel}
  73. afterClose={this.clearFieldsValue}
  74. destroyOnClose
  75. >
  76. <Form>
  77. {/*<FormItem label='字典编码' {...this.formItemStyle}>*/}
  78. {/* {getFieldDecorator<IDictData>('dictCode', {*/}
  79. {/* validateFirst: true,*/}
  80. {/* rules: [*/}
  81. {/* { required: true, message: '不能为空' }*/}
  82. {/* ],*/}
  83. {/* initialValue: fromView?.dictCode*/}
  84. {/* })(<Input />)}*/}
  85. {/*</FormItem>*/}
  86. <FormItem label="字典标签" {...this.formItemStyle}>
  87. {getFieldDecorator<IDictData>('dictLabel', {
  88. initialValue: fromView?.dictLabel
  89. })(<Input />)}
  90. </FormItem>
  91. <FormItem label="字典键值" {...this.formItemStyle}>
  92. {getFieldDecorator<IDictData>('dictValue', {
  93. initialValue: fromView?.dictValue
  94. })(<Input />)}
  95. </FormItem>
  96. <FormItem label="状态" {...this.formItemStyle}>
  97. {getFieldDecorator<IDictData>('status', {
  98. valuePropName: 'checked',
  99. initialValue: !fromView ? true : fromView?.status === 0
  100. })(
  101. <Switch
  102. checkedChildren="正常"
  103. unCheckedChildren="停用"
  104. defaultChecked
  105. />
  106. )}
  107. </FormItem>
  108. <FormItem label="备注" {...this.formItemStyle}>
  109. {getFieldDecorator<IDictData>('remark', {
  110. initialValue: fromView?.remark
  111. })(<Input.TextArea />)}
  112. </FormItem>
  113. </Form>
  114. </Modal>
  115. )
  116. }
  117. }
  118. export default Form.create<IDictTypeFormModalProps>()(DictTypeFormModal)