CopyModal.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, Input } from 'antd'
  22. import { FormComponentProps } from 'antd/lib/form'
  23. import { IWidgetBase } from '../types'
  24. const FormItem = Form.Item
  25. interface ICopyModalProps extends FormComponentProps<IWidgetBase> {
  26. visible: boolean
  27. loading: boolean
  28. fromWidget: IWidgetBase
  29. onCheckUniqueName: (widgetName: string, resolve: () => void, reject: (err: string) => void) => void
  30. onCopy: (view: IWidgetBase) => 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, fromWidget, onCopy } = this.props
  40. form.validateFieldsAndScroll((err, fieldsValue) => {
  41. if (err) { return }
  42. const copyWidget: IWidgetBase = { ...fromWidget, ...fieldsValue }
  43. onCopy(copyWidget)
  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, fromWidget, onCancel } = this.props
  59. const { getFieldDecorator } = form
  60. if (!fromWidget) { return null }
  61. return (
  62. <Modal
  63. title="复制 可视化组件"
  64. wrapClassName="ant-modal-small"
  65. visible={visible}
  66. onCancel={onCancel}
  67. onOk={this.save}
  68. afterClose={this.clearFieldsValue}
  69. confirmLoading={loading}
  70. >
  71. <Form>
  72. <FormItem label="新名称" {...this.formItemStyle}>
  73. {getFieldDecorator<IWidgetBase>('name', {
  74. validateFirst: true,
  75. rules: [
  76. { required: true, message: '不能为空' },
  77. { validator: this.checkName }
  78. ],
  79. initialValue: `${fromWidget.name}_copy`
  80. })(<Input />)}
  81. </FormItem>
  82. <FormItem label="描述" {...this.formItemStyle}>
  83. {getFieldDecorator<IWidgetBase>('description', {
  84. initialValue: fromWidget.description
  85. })(<Input />)}
  86. </FormItem>
  87. </Form>
  88. </Modal>
  89. )
  90. }
  91. }
  92. export default Form.create<ICopyModalProps>()(CopyModal)