CatalogueModal.tsx 4.1 KB

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