ProjectForm.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, {
  21. useImperativeHandle,
  22. useMemo,
  23. forwardRef
  24. } from 'react'
  25. import classnames from 'classnames'
  26. import { Form, Row, Col, Input, Tag, Button, Select } from 'antd'
  27. import { FormComponentProps } from 'antd/lib/form'
  28. const TextArea = Input.TextArea
  29. const Option = Select.Option
  30. const FormItem = Form.Item
  31. const styles = require('../../Organizations/Project.less')
  32. import Avatar from 'components/Avatar'
  33. const utilStyles = require('assets/less/util.less')
  34. import { IProjectFormFieldProps, IProjectsFormProps, FormType } from '../types'
  35. import {uuid} from 'utils/util'
  36. const EnhanceButton: React.FC<Partial<IProjectsFormProps>> = ({
  37. onModalOk, modalLoading, onTransfer, type
  38. }) => {
  39. const getType = useMemo(() => {
  40. if (type === 'transfer') {
  41. return '移 交'
  42. }
  43. return '保 存'
  44. }, [type])
  45. return (
  46. <Button
  47. key={`submit${uuid(8, 16)}`}
  48. size="large"
  49. type="primary"
  50. htmlType="submit"
  51. onClick={type==='transfer' ? onTransfer : onModalOk }
  52. loading={modalLoading}
  53. disabled={modalLoading}
  54. >
  55. {getType}
  56. </Button>
  57. )
  58. }
  59. const ProjectsForm: React.FC<IProjectsFormProps & FormComponentProps> = ({
  60. form, type, organizations, modalLoading, onCheckUniqueName,
  61. onModalOk, onTransfer, currentPro
  62. }, ref) => {
  63. const commonFormItemStyle = useMemo(() => (
  64. {
  65. labelCol: { span: 3 },
  66. wrapperCol: { span: 24 }
  67. }
  68. ), ['nf'])
  69. const isShowOrganization = useMemo(() => {
  70. return classnames({
  71. [utilStyles.hide]: (type === 'organizationProject') || (type === 'edit')
  72. })
  73. }, [type])
  74. const isShowDesc = useMemo(() => {
  75. return classnames({
  76. [utilStyles.hide]: type === 'transfer'
  77. })
  78. }, [type])
  79. const getModalTitle = useMemo(() => {
  80. return FormType[type]
  81. }, [type])
  82. useImperativeHandle(ref, () => ({form}))
  83. return (
  84. <div className={styles.formWrapper}>
  85. <div className={styles.header}>
  86. <div className={styles.title}>
  87. {getModalTitle}项目
  88. </div>
  89. <div className={styles.desc}>
  90. 项目属于组织,可以在项目中连接数据源,生成可视化图表
  91. </div>
  92. </div>
  93. <div className={styles.body}>
  94. <Form>
  95. <Row gutter={8}>
  96. <Col span={24}>
  97. {type !== 'add' && (
  98. <FormItem className={utilStyles.hide}>
  99. {form.getFieldDecorator<IProjectFormFieldProps>('id', {
  100. initialValue: currentPro.id
  101. })(
  102. <Input />
  103. )}
  104. </FormItem>
  105. )}
  106. {type === 'transfer' && (
  107. <FormItem className={utilStyles.hide}>
  108. {form.getFieldDecorator<IProjectFormFieldProps>('orgId_hc', {
  109. initialValue: `${currentPro.orgId}`
  110. })(
  111. <Input />
  112. )}
  113. </FormItem>
  114. )}
  115. {type !== 'organizationProject' && (
  116. <FormItem label="组织" {...commonFormItemStyle} className={isShowOrganization}>
  117. {form.getFieldDecorator<IProjectFormFieldProps>('orgId', {
  118. initialValue: `${type !== 'transfer' ? currentPro.orgId ? currentPro.orgId : '' : ''}`,
  119. rules: [{
  120. required: true,
  121. message: '组织名称 不能为空'
  122. }]
  123. })(
  124. <Select
  125. placeholder="Please select a organization"
  126. >
  127. {
  128. organizations ? organizations.map((o) => {
  129. const orgId = form.getFieldValue('orgId_hc')
  130. if (type === 'transfer' && o.id === Number(orgId)) {
  131. return []
  132. }
  133. const disabled = o.allowCreateProject === false
  134. return (
  135. <Option key={o.id} value={`${o.id}`} disabled={disabled} className={styles.selectOption}>
  136. <div className={styles.title}>
  137. <span className={styles.owner} style={{color: disabled ? '#ccc' : '#444444'}}>{o.name}</span>
  138. {`${o.id}` !== form.getFieldValue('orgId')
  139. ? o.role === 1 ? <Tag color={`${ disabled ? '#ccc' : '#108ee9'}`}>Owner</Tag> : ''
  140. : ''}
  141. </div>
  142. {`${o.id}` !== form.getFieldValue('orgId')
  143. ? (<Avatar size="small" path={o.avatar}/>)
  144. : ''}
  145. </Option>
  146. )
  147. }) : []
  148. }
  149. </Select>
  150. )}
  151. </FormItem>
  152. )}
  153. {type !== 'transfer' && (
  154. <FormItem label="名称" {...commonFormItemStyle} className={isShowDesc}>
  155. {form.getFieldDecorator<IProjectFormFieldProps>('name', {
  156. initialValue: currentPro.name,
  157. rules: [{
  158. required: true,
  159. message: 'Name 不能为空'
  160. }, {
  161. validator: onCheckUniqueName
  162. }],
  163. validateFirst: true
  164. })(
  165. <Input placeholder="Name" />
  166. )}
  167. </FormItem>
  168. )}
  169. </Col>
  170. <Col span={24}>
  171. {type !== 'transfer' && (
  172. <FormItem label="描述" {...commonFormItemStyle} className={isShowDesc}>
  173. {form.getFieldDecorator<IProjectFormFieldProps>('description', {
  174. initialValue: currentPro.description
  175. })(
  176. <TextArea
  177. placeholder="Description"
  178. autosize={{minRows: 2, maxRows: 6}}
  179. />
  180. )}
  181. </FormItem>
  182. )}
  183. </Col>
  184. <Col span={24}>
  185. <FormItem label="可见" {...commonFormItemStyle}>
  186. {form.getFieldDecorator<IProjectFormFieldProps>('visibility', {
  187. initialValue: type !== 'add' ? `${currentPro.visibility}` : 'true'
  188. })(
  189. <Select>
  190. <Option key="visibility" value="true">
  191. 公开
  192. </Option>
  193. <Option key="hidden" value="false">
  194. 授权
  195. </Option>
  196. </Select>
  197. )}
  198. </FormItem>
  199. </Col>
  200. <Col span={24}>
  201. {type !== 'add' && type !== 'transfer' && (
  202. <FormItem className={utilStyles.hide}>
  203. {form.getFieldDecorator<IProjectFormFieldProps>('pic', {
  204. initialValue: currentPro.pic
  205. })(
  206. <Input />
  207. )}
  208. </FormItem>
  209. )}
  210. </Col>
  211. </Row>
  212. </Form>
  213. </div>
  214. <div className={styles.footer}>
  215. <EnhanceButton
  216. type={type}
  217. modalLoading={modalLoading}
  218. onModalOk={onModalOk}
  219. onTransfer={onTransfer}
  220. />
  221. </div>
  222. </div>
  223. )
  224. }
  225. export default Form.create<IProjectsFormProps & FormComponentProps>()(forwardRef(ProjectsForm))