CatalogueModal.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 } from 'antd'
  22. import { FormComponentProps } from 'antd/lib/form'
  23. import { ICatalogue, IViewBase } from '../types'
  24. const FormItem = Form.Item
  25. interface ICopyModalProps extends FormComponentProps<ICatalogue> {
  26. visible: boolean
  27. loading: boolean
  28. fromView: ICatalogue
  29. onCheckUniqueName: (viewName: string, resolve: () => void, reject: (err: string) => void) => void
  30. onSave: (view: ICatalogue) => void
  31. onCancel: () => void
  32. }
  33. export class CatalogueModal extends React.PureComponent<ICopyModalProps> {
  34. private formItemStyle = {
  35. labelCol: { span: 6 },
  36. wrapperCol: { span: 18 }
  37. }
  38. private save = () => {
  39. const { form, fromView, onSave } = this.props
  40. form.validateFieldsAndScroll((err, fieldsValue) => {
  41. if (err) {
  42. return
  43. }
  44. const copyView = { ...fieldsValue, id: fromView?.id }
  45. onSave(copyView)
  46. })
  47. }
  48. private checkName = (_, value, callback) => {
  49. const { onCheckUniqueName } = this.props
  50. onCheckUniqueName(value, () => {
  51. callback()
  52. }, (err) => {
  53. callback(err)
  54. })
  55. }
  56. private clearFieldsValue = () => {
  57. this.props.form.resetFields()
  58. }
  59. public render() {
  60. console.log(this.props)
  61. const { form, visible, loading, fromView, onCancel } = this.props
  62. const { getFieldDecorator } = form
  63. // if (!fromView) { return null }
  64. const modalButtons = [(
  65. <Button
  66. key="back"
  67. size="large"
  68. onClick={onCancel}
  69. >
  70. 取 消
  71. </Button>
  72. ), (
  73. <Button
  74. disabled={loading}
  75. key="submit"
  76. size="large"
  77. type="primary"
  78. onClick={this.save}
  79. >
  80. 保 存
  81. </Button>
  82. )]
  83. return (
  84. <Modal
  85. title={fromView ? '编辑' : '新增'}
  86. wrapClassName="ant-modal-small"
  87. visible={visible}
  88. footer={modalButtons}
  89. onCancel={onCancel}
  90. afterClose={this.clearFieldsValue}
  91. >
  92. <Form>
  93. <FormItem label="资源名称" {...this.formItemStyle}>
  94. {getFieldDecorator<ICatalogue>('name', {
  95. validateFirst: true,
  96. rules: [
  97. { required: true, message: '不能为空' },
  98. { validator: this.checkName }
  99. ],
  100. initialValue: fromView?.name
  101. })(<Input autoComplete={'off'} />)}
  102. </FormItem>
  103. <FormItem label="行业分类" {...this.formItemStyle}>
  104. {getFieldDecorator<ICatalogue>('industry', {
  105. initialValue: fromView?.industry
  106. })(<Input />)}
  107. </FormItem>
  108. <FormItem label="来源部门" {...this.formItemStyle}>
  109. {getFieldDecorator<ICatalogue>('originDept', {
  110. initialValue: fromView?.originDept
  111. })(<Input />)}
  112. </FormItem>
  113. <FormItem label="来源系统" {...this.formItemStyle}>
  114. {getFieldDecorator<ICatalogue>('originSystem', {
  115. initialValue: fromView?.originSystem
  116. })(<Input />)}
  117. </FormItem>
  118. <FormItem label="扩展json" {...this.formItemStyle}>
  119. {getFieldDecorator<ICatalogue>('extConfig', {
  120. initialValue: fromView?.extConfig
  121. })(<Input />)}
  122. </FormItem>
  123. <FormItem label="描述" {...this.formItemStyle}>
  124. {getFieldDecorator<ICatalogue>('description', {
  125. initialValue: fromView?.description
  126. })(<Input />)}
  127. </FormItem>
  128. </Form>
  129. </Modal>
  130. )
  131. }
  132. }
  133. export default Form.create<ICopyModalProps>()(CatalogueModal)