index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { ZhButton } from '@/components/Button'
  2. import { apiGetRule } from '@/pages/Safe/Content/List/api'
  3. import { tenderStore } from '@/store/mobx'
  4. import { ruleOption } from '@/types/rule'
  5. import consts from '@/utils/consts'
  6. import { dayjsFomrat } from '@/utils/util'
  7. import { Form, Input, InputNumber, Modal, Select, Tabs, Tag } from 'antd'
  8. import { TweenOneGroup } from 'rc-tween-one'
  9. import React, { useEffect, useState } from 'react'
  10. import styles from './index.module.scss'
  11. const { TabPane } = Tabs
  12. const { Option } = Select
  13. interface iTags {
  14. ruleArr: ruleOption[]
  15. setRuleArr: (arr: ruleOption[]) => void
  16. }
  17. const RenderTags:React.FC<iTags> = ({ ruleArr, setRuleArr }) => {
  18. const handleClose = (removedTag: ruleOption) => {
  19. const tags = ruleArr.filter(tag => tag.value !== removedTag.value)
  20. setRuleArr(tags)
  21. }
  22. return (
  23. <div>
  24. <TweenOneGroup
  25. enter={{
  26. scale: 0.8,
  27. opacity: 0,
  28. type: 'from',
  29. duration: 100
  30. // onComplete: (e: ) => {
  31. // e.target.style = ''
  32. // }
  33. }}
  34. leave={{ opacity: 0, width: 0, scale: 0, duration: 200 }}
  35. appear={false}
  36. >
  37. {
  38. ruleArr.map((tag, idx) => {
  39. return (
  40. <span key={idx} style={{ display: 'inline-block' }}>
  41. <Tag closable onClose={(e: Event) => {e.preventDefault();handleClose(tag)}}>
  42. {tag.value}
  43. </Tag>
  44. </span>
  45. )
  46. })
  47. }
  48. </TweenOneGroup>
  49. </div>
  50. )
  51. }
  52. interface iSafeCreateFormProps {
  53. type: string;
  54. visible: boolean;
  55. loading: boolean;
  56. title: string;
  57. onCreate: (values: any) => void;
  58. onCancel: () => void;
  59. positionTab?: React.ReactNode
  60. }
  61. const SafeCreateForm: React.FC<iSafeCreateFormProps> = ({
  62. type,
  63. visible,
  64. loading,
  65. onCreate,
  66. title,
  67. onCancel,
  68. positionTab
  69. }) => {
  70. const [ form ] = Form.useForm()
  71. const [ ruleType, setRuleType ] = useState<string>('3')
  72. const [ ruleArr, setRuleArr ] = useState<ruleOption[]>([])
  73. useEffect(() => {
  74. form.setFieldsValue({ bidsectionId: tenderStore.tender.bidsectionId })
  75. }, [ visible, type ])
  76. const ruleHandler = () => {
  77. switch (ruleType) {
  78. case '0':
  79. setRuleArr([ ...ruleArr, { type: 'name', value: tenderStore.tender.name } ])
  80. break
  81. case '1':
  82. setRuleArr([ ...ruleArr, { type: 'text', value: form.getFieldValue('ruleText') } ])
  83. break
  84. case '2':
  85. setRuleArr([ ...ruleArr, { type: 'date', value: dayjsFomrat(new Date(), 'YYYYMM') } ])
  86. break
  87. default:
  88. setRuleArr([ ...ruleArr, { type: 'code', value: form.getFieldValue('initCode') } ])
  89. break
  90. }
  91. }
  92. // 处理自动位数编号 -> 联动起始编号
  93. const digitHandler = (value: any) => {
  94. if (value) {
  95. const length = form.getFieldValue('digits')
  96. const code = parseInt(form.getFieldValue('initCode'))
  97. const newCode = (Array(length).join('0') + code).slice(-length)
  98. form.setFieldsValue({ initCode: newCode })
  99. }
  100. }
  101. // 初始化
  102. const ruleModalIniter = async () => {
  103. const { code, data } = await apiGetRule(tenderStore.tender.bidsectionId)
  104. if (code === consts.RET_CODE.SUCCESS) {
  105. if (data[type]) {
  106. const obj = JSON.parse(data[type])
  107. const newRule = []
  108. delete obj.eg
  109. for (const key in obj) {
  110. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  111. let value = obj[key]
  112. if (key === 'code') {
  113. // const code = parseInt(form.getFieldValue('initCode'))
  114. value = (Array(value.length).join('0') + parseInt(value)).slice(-value.length)
  115. }
  116. newRule.push({ type: key, value })
  117. }
  118. }
  119. setRuleArr(newRule)
  120. }
  121. }
  122. }
  123. useEffect(() => {
  124. if (visible) {
  125. ruleModalIniter()
  126. }
  127. }, [ visible, type ])
  128. // useActivate(() => ruleModalIniter())
  129. return (
  130. <Modal
  131. getContainer={false}
  132. visible={visible}
  133. title={title}
  134. okText="确认添加"
  135. cancelText="取消"
  136. onCancel={onCancel}
  137. confirmLoading={loading}
  138. okButtonProps={{ size: 'small' }}
  139. cancelButtonProps={{ size: 'small' }}
  140. onOk={() => {
  141. form
  142. .validateFields()
  143. .then(() => {
  144. form.resetFields()
  145. onCreate(ruleArr)
  146. })
  147. .catch(info => {
  148. console.log('Validate Failed:', info)
  149. })
  150. }}
  151. >
  152. <Form form={form} layout="vertical" size="middle" className={styles.SafeModalForm}>
  153. <Tabs defaultActiveKey="1" type="card" size="small">
  154. <TabPane tab="编号规则设置" key="1">
  155. <div className={styles.ruleContaniner}>
  156. <h5 className={styles.ruleText}>当前规则:{ruleArr.map(item => item.value).join('-')}</h5>
  157. <div><RenderTags ruleArr={ruleArr} setRuleArr={setRuleArr}></RenderTags></div>
  158. </div>
  159. <Form.Item label="添加新组建规则" name="ruleType" initialValue="3">
  160. <Select onChange={(value: string) => setRuleType(value)}>
  161. <Option value="0">标段名</Option>
  162. <Option value="1">文本</Option>
  163. <Option value="2">当前年月</Option>
  164. <Option value="3">自动编号</Option>
  165. </Select>
  166. </Form.Item>
  167. {
  168. ruleType === '3' ?
  169. <>
  170. <Form.Item label="自动编号位数" name="digits" initialValue="3">
  171. <InputNumber size="small" min={1} style={{ width: '100%' }} onChange={(value) => digitHandler(value)}></InputNumber>
  172. </Form.Item>
  173. <Form.Item label="起始编号" name="initCode" initialValue="001">
  174. <Input type="number"></Input>
  175. </Form.Item>
  176. </>
  177. : ''
  178. }
  179. {
  180. ruleType === '1' ?
  181. <>
  182. <Form.Item label="规则文本" name="ruleText">
  183. <Input></Input>
  184. </Form.Item>
  185. </>
  186. : ''
  187. }
  188. <ZhButton size="small" onClick={() => ruleHandler()}>添加组件</ZhButton>
  189. </TabPane>
  190. {
  191. positionTab ? { positionTab } : ''
  192. }
  193. {/* <TabPane tab="部位设置" key="2"></TabPane> */}
  194. </Tabs>
  195. </Form>
  196. </Modal>
  197. )
  198. }
  199. export default SafeCreateForm