DisplayFormModal.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 { Form, Row, Col, Input, Radio, Button, Tabs, Modal, Checkbox } from 'antd'
  22. import { FormComponentProps } from 'antd/lib/form/Form'
  23. const TextArea = Input.TextArea
  24. const FormItem = Form.Item
  25. const RadioGroup = Radio.Group
  26. const TabPane = Tabs.TabPane
  27. import { Display, DisplayFormType } from './types'
  28. const styles = require('containers/Viz/Viz.less')
  29. import {IExludeRoles} from 'containers/Viz/components/PortalList'
  30. const utilStyles = require('assets/less/util.less')
  31. interface IDisplayFormModalProps {
  32. projectId: number
  33. display: Display,
  34. visible: boolean
  35. loading: boolean
  36. type: DisplayFormType
  37. onCheckName: (type, data, resolve, reject) => void
  38. onSave: (display: Display, type: string) => void
  39. onCancel: () => void
  40. exludeRoles?: IExludeRoles[]
  41. onChangePermission: (scope: object, e: any) => any
  42. }
  43. export class DisplayFormModal extends React.PureComponent<IDisplayFormModalProps & FormComponentProps, {}> {
  44. private formTypeTitleMapping = {
  45. add: '新增',
  46. edit: '修改',
  47. copy: '复制'
  48. }
  49. public componentWillReceiveProps (nextProps: IDisplayFormModalProps & FormComponentProps) {
  50. const { form, display } = nextProps
  51. if (display !== this.props.display) {
  52. this.initFormValue(form, display)
  53. }
  54. }
  55. public componentDidMount () {
  56. const { form, display } = this.props
  57. this.initFormValue(form, display)
  58. }
  59. private initFormValue (form, display: Display) {
  60. if (display) {
  61. form.setFieldsValue({ ...display })
  62. } else {
  63. form.resetFields()
  64. }
  65. }
  66. private checkNameUnique = (_, value = '', callback) => {
  67. const { projectId, onCheckName, type, form } = this.props
  68. const id = type === 'edit'
  69. ? { id: form.getFieldsValue().id }
  70. : void 0
  71. const typeName = 'display'
  72. if (!value) {
  73. callback()
  74. }
  75. onCheckName(typeName, {
  76. projectId,
  77. ...id,
  78. name: value
  79. },
  80. () => {
  81. callback()
  82. }, (err) => {
  83. callback(err)
  84. })
  85. }
  86. private onModalOk = () => {
  87. const { type, display, projectId, onSave } = this.props
  88. this.props.form.validateFieldsAndScroll((err, values) => {
  89. if (!err) {
  90. onSave({ ...display, ...values, projectId }, type)
  91. }
  92. })
  93. }
  94. private commonFormItemStyle = {
  95. labelCol: { span: 6 },
  96. wrapperCol: { span: 16 }
  97. }
  98. public render () {
  99. const { type, visible, loading, form, onCancel, exludeRoles } = this.props
  100. const { getFieldDecorator } = form
  101. const authControl = exludeRoles && exludeRoles.length ? exludeRoles.map((role) => (
  102. <div className={styles.excludeList} key={`${role.name}key`}>
  103. <Checkbox checked={role.permission} onChange={this.props.onChangePermission.bind(this, role)}>
  104. {role.name}
  105. </Checkbox>
  106. </div>
  107. )) : []
  108. const modalButtons = [(
  109. <Button
  110. key="back"
  111. size="large"
  112. onClick={onCancel}
  113. >
  114. 取 消
  115. </Button>
  116. ), (
  117. <Button
  118. key="submit"
  119. size="large"
  120. type="primary"
  121. loading={loading}
  122. disabled={loading}
  123. onClick={this.onModalOk}
  124. >
  125. 保 存
  126. </Button>
  127. )]
  128. return (
  129. <Modal
  130. title={`${this.formTypeTitleMapping[type]} 大屏`}
  131. wrapClassName="ant-modal-small"
  132. visible={visible}
  133. footer={modalButtons}
  134. onCancel={onCancel}
  135. >
  136. <Form>
  137. <Row gutter={8}>
  138. <Col span={24}>
  139. <FormItem className={utilStyles.hide}>
  140. {getFieldDecorator('projectId')(
  141. <Input />
  142. )}
  143. </FormItem>
  144. <FormItem className={utilStyles.hide}>
  145. {getFieldDecorator('id')(
  146. <Input />
  147. )}
  148. </FormItem>
  149. <Tabs defaultActiveKey="displayInfo">
  150. <TabPane tab="基本信息" key="displayInfo">
  151. <Col span={24}>
  152. <FormItem label="名称" {...this.commonFormItemStyle}>
  153. {getFieldDecorator('name', {
  154. rules: [{
  155. required: true,
  156. message: '名称 不能为空'
  157. }, {
  158. validator: this.checkNameUnique
  159. }]
  160. })(
  161. <Input placeholder="名称" />
  162. )}
  163. </FormItem>
  164. </Col>
  165. <Col span={24}>
  166. <FormItem label="描述" {...this.commonFormItemStyle}>
  167. {getFieldDecorator('description', {
  168. initialValue: ''
  169. })(
  170. <TextArea
  171. placeholder="描述"
  172. autosize={{minRows: 2, maxRows: 6}}
  173. />
  174. )}
  175. </FormItem>
  176. </Col>
  177. <Col span={24}>
  178. <FormItem label="是否发布" {...this.commonFormItemStyle}>
  179. {getFieldDecorator('publish', {
  180. initialValue: true
  181. })(
  182. <RadioGroup>
  183. <Radio value>发布</Radio>
  184. <Radio value={false}>编辑</Radio>
  185. </RadioGroup>
  186. )}
  187. </FormItem>
  188. <FormItem className={utilStyles.hide}>
  189. {getFieldDecorator('avatar')(
  190. <Input />
  191. )}
  192. </FormItem>
  193. </Col>
  194. </TabPane>
  195. <TabPane tab="权限管理" key="dislayControl">
  196. {
  197. authControl
  198. }
  199. </TabPane>
  200. </Tabs>
  201. </Col>
  202. </Row>
  203. </Form>
  204. </Modal>
  205. )
  206. }
  207. }
  208. export default Form.create<IDisplayFormModalProps & FormComponentProps>()(DisplayFormModal)