Z1add.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { Button, Cascader, Form, FormInstance, Input, InputNumber, Modal } from 'antd'
  4. import MyPopconfirm from '@/components/MyPopconfirm'
  5. import { useSelector } from 'react-redux'
  6. import { RootState } from '@/store'
  7. import { MessageFu } from '@/utils/message'
  8. import { TypeZ1dict } from './type'
  9. import { Z1_APIgetInfo, Z1_APIsave } from '@/store/action/Z1dict'
  10. export type Z1AddInfoType = {
  11. id: string
  12. txt: '新增' | '编辑'
  13. acInfo: TypeZ1dict
  14. }
  15. type Props = {
  16. acShuTxt: string
  17. addInfo: Z1AddInfoType
  18. addFu: () => void
  19. closeFu: () => void
  20. isNoAcIds: string[] //没有数据或者删除了的时候-既右侧没有操作的时候用到
  21. }
  22. function Z1add({ addInfo, addFu, closeFu, isNoAcIds, acShuTxt }: Props) {
  23. const { dictList: treeData } = useSelector((state: RootState) => state.Z1dict)
  24. // 级联选择器改变的时候 筛选当前级联的 信息出来
  25. const [acCardInfo, setAcCardInfo] = useState({} as TypeZ1dict)
  26. const [parentIdArr, setParentIdArr] = useState<string[] | null>(null)
  27. // useEffect(() => {
  28. // console.log(acCardInfo, parentIdArr)
  29. // }, [acCardInfo, parentIdArr])
  30. const saveIdsRef = useRef<string[]>([])
  31. useEffect(() => {
  32. setAcCardInfo(addInfo.acInfo)
  33. let ids: string[] | null = addInfo.acInfo.id ? [addInfo.acInfo.id] : null
  34. if (addInfo.acInfo.ancestor) ids = [...addInfo.acInfo.ancestor.split(','), addInfo.acInfo.id]
  35. let idsRes = ids
  36. if (ids && ids.length >= 1 && addInfo.txt === '编辑') {
  37. ids.pop()
  38. }
  39. if (idsRes) {
  40. // 保存的时候需要补前面3个级别
  41. saveIdsRef.current = idsRes.filter((v, i) => i <= 2)
  42. // 去掉0和前2级
  43. idsRes = idsRes.filter((v, i) => i > 2)
  44. }
  45. setParentIdArr(idsRes)
  46. }, [addInfo.acInfo, addInfo.txt])
  47. const cardChange = useCallback((aa: any, bb: any) => {
  48. setParentIdArr(aa)
  49. if (bb && bb.length) setAcCardInfo(bb[bb.length - 1])
  50. else setAcCardInfo({} as TypeZ1dict)
  51. }, [])
  52. const getInfoFu = useCallback(async (id: string) => {
  53. const res = await Z1_APIgetInfo(id)
  54. if (res.code === 0) {
  55. FormBoxRef.current?.setFieldsValue({
  56. ...res.data
  57. })
  58. }
  59. }, [])
  60. useEffect(() => {
  61. if (addInfo.txt === '编辑') getInfoFu(addInfo.id)
  62. else {
  63. FormBoxRef.current?.setFieldsValue({
  64. sort: 50000
  65. })
  66. }
  67. }, [addInfo.id, addInfo.txt, getInfoFu])
  68. // 设置表单初始数据(区分编辑和新增)
  69. const FormBoxRef = useRef<FormInstance>(null)
  70. // 没有通过校验
  71. const onFinishFailed = useCallback(() => {
  72. // return MessageFu.warning("有表单不符号规则!");
  73. }, [])
  74. // 通过校验点击确定
  75. const onFinish = useCallback(
  76. async (values: any) => {
  77. let ancestor = ''
  78. let parentId: string | null = null
  79. if (parentIdArr && parentIdArr.length >= 5 && addInfo.txt === '新增')
  80. return MessageFu.warning('最多支持五级!')
  81. if (acCardInfo) parentId = addInfo.txt === '编辑' ? acCardInfo.parentId : acCardInfo.id
  82. if (parentIdArr && parentId) {
  83. let arrTemp = [...saveIdsRef.current, ...parentIdArr.filter(v => v !== addInfo.id)]
  84. ancestor = arrTemp.join(',')
  85. }
  86. // 新增并且没有父级
  87. if (addInfo.txt === '新增' && !parentId && !ancestor) {
  88. // console.log('xxx', saveIdsRef.current)
  89. parentId = saveIdsRef.current[saveIdsRef.current.length - 1]
  90. ancestor = saveIdsRef.current.join(',')
  91. // console.log(123, parentId, ancestor)
  92. // 外层没有选中
  93. if (!addInfo.acInfo.id) {
  94. parentId = isNoAcIds[isNoAcIds.length - 1]
  95. ancestor = isNoAcIds.join(',')
  96. }
  97. }
  98. // let level = 1
  99. // if (parentIdArr) {
  100. // level = addInfo.txt === '新增' ? acCardInfo.level + 1 : acCardInfo.level
  101. // }
  102. const obj = {
  103. ...values,
  104. id: addInfo.txt === '编辑' ? addInfo.id : null,
  105. ancestor,
  106. // level,
  107. parentId,
  108. type: 'dict'
  109. }
  110. // console.log(123, obj)
  111. // if (1 + 1 === 2) {
  112. // return
  113. // }
  114. const res = await Z1_APIsave(obj)
  115. if (res.code === 0) {
  116. MessageFu.success(addInfo.txt + '成功!')
  117. addFu()
  118. closeFu()
  119. }
  120. },
  121. [acCardInfo, addFu, addInfo.acInfo.id, addInfo.id, addInfo.txt, closeFu, isNoAcIds, parentIdArr]
  122. )
  123. return (
  124. <Modal
  125. wrapClassName={styles.Z1add}
  126. open={true}
  127. title={addInfo.txt}
  128. footer={
  129. [] // 设置footer为空,去掉 取消 确定默认按钮
  130. }
  131. >
  132. <div className='Z1aMain'>
  133. <Form
  134. scrollToFirstError={true}
  135. ref={FormBoxRef}
  136. name='basic'
  137. labelCol={{ span: 3 }}
  138. onFinish={onFinish}
  139. onFinishFailed={onFinishFailed}
  140. autoComplete='off'
  141. >
  142. <div className='fromRow'>
  143. <div className='fromRowll'>上级字典:</div>
  144. <div className='fromRowrr'>
  145. <Cascader
  146. style={{ width: 658 }}
  147. disabled={addInfo.txt === '编辑'}
  148. changeOnSelect
  149. fieldNames={{ label: 'name', value: 'id', children: 'children' }}
  150. options={treeData}
  151. // placeholder={addInfo.txt === '编辑' ? '空' : '请选择'}
  152. placeholder={acShuTxt}
  153. value={parentIdArr ? [...parentIdArr] : []}
  154. onChange={cardChange}
  155. />
  156. </div>
  157. </div>
  158. <Form.Item
  159. label='字典值'
  160. name='name'
  161. rules={[{ required: true, message: '请输入字典值!' }]}
  162. getValueFromEvent={e => e.target.value.replace(/\s+/g, '')}
  163. >
  164. <Input maxLength={30} showCount placeholder='请输入内容' />
  165. </Form.Item>
  166. <div className='fromRow2'>
  167. <Form.Item
  168. label='排序值'
  169. name='sort'
  170. rules={[{ required: true, message: '请输入排序值!' }]}
  171. >
  172. <InputNumber min={1} max={50000} precision={0} placeholder='请输入' />
  173. </Form.Item>
  174. <div className='fromRowTit'>
  175. 请输入1~50000的数字。数字越小,排序越靠前。数字相同时,更新发布的内容排在前面
  176. </div>
  177. </div>
  178. {/* 确定和取消按钮 */}
  179. <br />
  180. <Form.Item wrapperCol={{ offset: 9, span: 16 }}>
  181. <Button type='primary' htmlType='submit'>
  182. 提交
  183. </Button>
  184. &emsp;
  185. <MyPopconfirm txtK='取消' onConfirm={closeFu} />
  186. </Form.Item>
  187. </Form>
  188. </div>
  189. </Modal>
  190. )
  191. }
  192. const MemoZ1add = React.memo(Z1add)
  193. export default MemoZ1add