CopyModal.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 { IViewBase } from '../types'
  24. const FormItem = Form.Item
  25. interface ICopyModalProps extends FormComponentProps<IViewBase> {
  26. visible: boolean
  27. loading: boolean
  28. fromView: IViewBase
  29. onCheckUniqueName: (viewName: string, resolve: () => void, reject: (err: string) => void) => void
  30. onCopy: (view: IViewBase) => void
  31. onCancel: () => void
  32. }
  33. export class CopyModal extends React.PureComponent<ICopyModalProps> {
  34. private formItemStyle = {
  35. labelCol: { span: 6 },
  36. wrapperCol: { span: 18 }
  37. }
  38. private save = () => {
  39. const { form, fromView, onCopy } = this.props
  40. form.validateFieldsAndScroll((err, fieldsValue) => {
  41. if (err) { return }
  42. const copyView: IViewBase = { ...fieldsValue, id: fromView.id }
  43. onCopy(copyView)
  44. })
  45. }
  46. private checkName = (_, value, callback) => {
  47. const { onCheckUniqueName } = this.props
  48. onCheckUniqueName(value, () => {
  49. callback()
  50. }, (err) => {
  51. callback(err)
  52. })
  53. }
  54. private clearFieldsValue = () => {
  55. this.props.form.resetFields()
  56. }
  57. public render () {
  58. const { form, visible, loading, fromView, onCancel } = this.props
  59. const { getFieldDecorator } = form
  60. if (!fromView) { return null }
  61. const modalButtons = [(
  62. <Button
  63. key="back"
  64. size="large"
  65. onClick={onCancel}
  66. >
  67. 取 消
  68. </Button>
  69. ), (
  70. <Button
  71. disabled={loading}
  72. key="submit"
  73. size="large"
  74. type="primary"
  75. onClick={this.save}
  76. >
  77. 保 存
  78. </Button>
  79. )]
  80. return (
  81. <Modal
  82. title="复制 视图"
  83. wrapClassName="ant-modal-small"
  84. visible={visible}
  85. footer={modalButtons}
  86. onCancel={onCancel}
  87. afterClose={this.clearFieldsValue}
  88. >
  89. <Form>
  90. <FormItem label="新名称" {...this.formItemStyle}>
  91. {getFieldDecorator<IViewBase>('name', {
  92. validateFirst: true,
  93. rules: [
  94. { required: true, message: '不能为空' }
  95. ],
  96. initialValue: `${fromView.name}_copy`
  97. })(<Input />)}
  98. </FormItem>
  99. <FormItem label="描述" {...this.formItemStyle}>
  100. {getFieldDecorator<IViewBase>('description', {
  101. initialValue: fromView.description
  102. })(<Input />)}
  103. </FormItem>
  104. </Form>
  105. </Modal>
  106. )
  107. }
  108. }
  109. export default Form.create<ICopyModalProps>()(CopyModal)