CatalogueModal.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, Spin } 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. confirmLoading={loading}
  89. visible={visible}
  90. footer={modalButtons}
  91. onCancel={onCancel}
  92. afterClose={this.clearFieldsValue}
  93. >
  94. <Spin spinning={loading}>
  95. <Form>
  96. <FormItem label='资源名称' {...this.formItemStyle}>
  97. {getFieldDecorator<ICatalogue>('name', {
  98. validateFirst: true,
  99. rules: [
  100. { required: true, message: '不能为空' },
  101. { validator: this.checkName }
  102. ],
  103. initialValue: fromView?.name
  104. })(<Input autoComplete={'off'} />)}
  105. </FormItem>
  106. <FormItem label='行业分类' {...this.formItemStyle}>
  107. {getFieldDecorator<ICatalogue>('industry', {
  108. initialValue: fromView?.industry
  109. })(<Input />)}
  110. </FormItem>
  111. <FormItem label='来源部门' {...this.formItemStyle}>
  112. {getFieldDecorator<ICatalogue>('originDept', {
  113. initialValue: fromView?.originDept
  114. })(<Input />)}
  115. </FormItem>
  116. <FormItem label='来源系统' {...this.formItemStyle}>
  117. {getFieldDecorator<ICatalogue>('originSystem', {
  118. initialValue: fromView?.originSystem
  119. })(<Input />)}
  120. </FormItem>
  121. <FormItem label='扩展json' {...this.formItemStyle}>
  122. {getFieldDecorator<ICatalogue>('extConfig', {
  123. initialValue: fromView?.extConfig
  124. })(<Input />)}
  125. </FormItem>
  126. <FormItem label='描述' {...this.formItemStyle}>
  127. {getFieldDecorator<ICatalogue>('description', {
  128. initialValue: fromView?.description
  129. })(<Input />)}
  130. </FormItem>
  131. </Form>
  132. </Spin>
  133. </Modal>
  134. )
  135. }
  136. }
  137. export default Form.create<ICopyModalProps>()(CatalogueModal)