groupLeft.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { useAjax } from "@/Hook/useAjax"
  2. import { DeleteOutlined, FormOutlined, PlusOutlined } from "@ant-design/icons"
  3. import { App, Button, Input, Popconfirm, Space, Spin } from "antd"
  4. import React, { useEffect, useRef, useState } from "react"
  5. import style from './index.less'
  6. import { addGroupApi, delGroupApi, editGroupApi, getGroupListApi } from "../../API/corpUserManage"
  7. interface Props {
  8. onChange?: (groupId?: number) => void
  9. value?: number
  10. }
  11. /**
  12. * 分组管理Left 菜单
  13. */
  14. const GroupLeft: React.FC<Props> = ({ onChange, value }) => {
  15. /*************************/
  16. const { message } = App.useApp()
  17. const [isNewGrouping, setIsNewGrouping] = useState<boolean>(false); // 是否新增分组
  18. const inputRef = useRef<any>(null);
  19. const [groupData, setGroupData] = useState<{ groupName: string, remark?: string, groupId?: number }>({ groupName: '' }) // 分组添加
  20. const [groupId, setGroupId] = useState<number>(value || 0)
  21. const addGroup = useAjax((params) => addGroupApi(params))
  22. const editGroup = useAjax((params) => editGroupApi(params))
  23. const delGroup = useAjax((params) => delGroupApi(params))
  24. const getGroupList = useAjax(() => getGroupListApi())
  25. /*************************/
  26. useEffect(() => {
  27. getGroupList.run()
  28. }, [])
  29. useEffect(() => {
  30. if (value) {
  31. setGroupId(value || 0)
  32. }
  33. }, [value])
  34. /** 点击新增处理聚焦 */
  35. useEffect(() => {
  36. if (isNewGrouping && inputRef?.current) {
  37. inputRef.current!.focus({
  38. cursor: 'end',
  39. });
  40. }
  41. }, [inputRef, isNewGrouping])
  42. /** 新增修改分组 */
  43. const addEditHandle = () => {
  44. if (groupData?.groupName) {
  45. if (groupData?.groupId) {
  46. editGroup.run(groupData).then((res: any) => {
  47. message.success('修改成功')
  48. setGroupData({ groupName: '' })
  49. setIsNewGrouping(false)
  50. getGroupList.refresh()
  51. })
  52. } else {
  53. addGroup.run(groupData).then((res: any) => {
  54. message.success('添加成功')
  55. setGroupData({ groupName: '' })
  56. setIsNewGrouping(false)
  57. getGroupList.refresh()
  58. })
  59. }
  60. } else {
  61. message.error('请填写分组名称')
  62. }
  63. }
  64. /** 删除分组 */
  65. const delGroupHandle = (id: number) => {
  66. delGroup.run({ groupId: id }).then(res => {
  67. message.success('删除成功')
  68. getGroupList.refresh()
  69. if (groupId === id) {
  70. setGroupId(0)
  71. onChange?.(undefined)
  72. }
  73. })
  74. }
  75. return <div className={style.groupLeft}>
  76. <div className={style.groupLeft_header}>
  77. <div>全部分组</div>
  78. <Button type="link" icon={<PlusOutlined />} className="stop" onClick={(e) => {
  79. e.stopPropagation()
  80. setGroupData({ groupName: '' })
  81. setIsNewGrouping(true)
  82. }}>新增分组</Button>
  83. </div>
  84. <div className={style.groupLeft_content}>
  85. {isNewGrouping && <div className={style.groupLeft_content_item}>
  86. <div className={`${style.edit}`}>
  87. <Space direction="vertical">
  88. <Space>
  89. <div style={{ width: 30 }}>组名</div>
  90. <Input placeholder="输入分组名称(最多10个字)" maxLength={10} ref={inputRef} onChange={(e) => { setGroupData({ ...groupData, groupName: e.target.value }) }} value={groupData?.groupName} />
  91. </Space>
  92. <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
  93. <Space>
  94. <Button size="small" onClick={() => { setIsNewGrouping(false); setGroupData({ groupName: '' }) }}>取消</Button>
  95. <Button type="primary" size="small" onClick={addEditHandle} loading={getGroupList?.loading}>确定</Button>
  96. </Space>
  97. </div>
  98. </Space>
  99. </div>
  100. </div>}
  101. <Spin spinning={getGroupList.loading}>
  102. <div className={style.groupLeft_content_item}>
  103. <div className={`${style.con} ${groupId === 0 ? style.select : ''}`} onClick={() => { setGroupId(0); onChange?.(undefined); }}>
  104. <div className={style.left}>
  105. <div className={style.title}>全部</div>
  106. </div>
  107. </div>
  108. </div>
  109. {getGroupList && Array.isArray(getGroupList?.data?.data) && getGroupList?.data?.data?.map((item: { groupName: string, groupId: number }, index: number) => <div className={style.groupLeft_content_item} key={item.groupId || index}>
  110. {groupData?.groupId && groupData?.groupId === item?.groupId ? <div className={style.edit}>
  111. <Space direction="vertical">
  112. <Space>
  113. <div style={{ width: 40 }}>组名</div>
  114. <Input placeholder="输入分组名称(最多10个字)" maxLength={10} ref={inputRef} onChange={(e) => { setGroupData({ ...groupData, groupName: e.target.value }) }} value={groupData?.groupName} />
  115. </Space>
  116. <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
  117. <Space>
  118. <Button size="small" onClick={() => { setIsNewGrouping(false); setGroupData({ groupName: '' }) }}>取消</Button>
  119. <Button type="primary" size="small" onClick={addEditHandle} loading={editGroup?.loading}>修改</Button>
  120. </Space>
  121. </div>
  122. </Space>
  123. </div> : <div className={`${style.con} ${groupId === item.groupId ? style.select : ''}`} onClick={() => { setGroupId(item.groupId); onChange?.(item.groupId); }} key={item.groupId || index}>
  124. <div className={style.left}>
  125. <div className={style.title}>{item.groupName}</div>
  126. </div>
  127. <div className={style.right}>
  128. <Space>
  129. <FormOutlined onClick={(e) => { e.stopPropagation(); setGroupData({ ...item }); setIsNewGrouping(false) }} />
  130. <Popconfirm
  131. title="确定删除?"
  132. onConfirm={() => { delGroupHandle(item.groupId) }}
  133. okText="确定"
  134. cancelText="取消"
  135. >
  136. <DeleteOutlined style={{ color: 'red' }} />
  137. </Popconfirm>
  138. </Space>
  139. </div>
  140. </div>}
  141. </div>)}
  142. </Spin>
  143. </div>
  144. </div>
  145. }
  146. export default React.memo(GroupLeft)