OptionSelect.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import consts from '@/consts'
  2. import { queryUsageSelectOptions } from '@/services/statistic'
  3. import { getAuthCache, setAuthCache } from '@/utils/auth'
  4. import { STATISTIC_OPTION_KEY } from '@/utils/cache/cacheEnum'
  5. import { useDebounceFn } from 'ahooks'
  6. import { Select, TreeSelect } from 'antd'
  7. import React, { useState, useEffect, useMemo } from 'react'
  8. export enum statisticOptionMap {
  9. STAFF = 'staff',
  10. DEPARTMENT = 'department',
  11. SINGLE_STAFF = 'singleStaff',
  12. SINGLE_DEPARTMENT = 'singleDepartment'
  13. }
  14. type OptionSelectProps = {
  15. /** @name 是否多选 */
  16. multiple?: boolean
  17. type: string
  18. onChange: (type: 'staff' | 'department', value) => void
  19. }
  20. const options = [
  21. { label: '按部门', value: 'department' },
  22. { label: '按员工', value: 'staff' },
  23. { label: '指定部门', value: 'singleDepartment' },
  24. { label: '指定员工', value: 'singleStaff' }
  25. ]
  26. function generateTree(tree) {
  27. return tree.map(item => {
  28. const newItem = { ...item }
  29. if (item.children?.length) {
  30. newItem.children = generateTree(item.children)
  31. }
  32. if (!item.isLeaf) {
  33. newItem.disabled = true
  34. }
  35. return newItem
  36. })
  37. }
  38. const OptionSelect: React.FC<OptionSelectProps> = ({ multiple = true, type, onChange }) => {
  39. const authOptions = getAuthCache(STATISTIC_OPTION_KEY)
  40. const { optionType, dataIds } = authOptions?.[type] || {}
  41. const [state, setState] = useState({
  42. optionType: optionType || (multiple ? statisticOptionMap.STAFF : statisticOptionMap.SINGLE_STAFF),
  43. dataIds,
  44. department: [],
  45. staff: []
  46. })
  47. useEffect(() => {
  48. const queryStaffAndDepartment = async () => {
  49. const { code = -1, data: { department = [], staff = [] } = {} } = await queryUsageSelectOptions()
  50. if (code === consts.RET_CODE.SUCCESS) {
  51. setState({
  52. ...state,
  53. department,
  54. staff
  55. })
  56. }
  57. }
  58. queryStaffAndDepartment()
  59. if (optionType && dataIds?.length) {
  60. onChange(optionType, dataIds)
  61. }
  62. }, [])
  63. const { run: trySetOpIds } = useDebounceFn(
  64. ids => {
  65. setState({ ...state, dataIds: ids })
  66. onChange(state.optionType, ids)
  67. setAuthCache(STATISTIC_OPTION_KEY, {
  68. ...authOptions,
  69. [type]: { optionType: state.optionType, dataIds: ids }
  70. })
  71. },
  72. { wait: 500 }
  73. )
  74. const treeOptions = useMemo(() => {
  75. const base = {
  76. className: 'min-w-180px',
  77. dropdownMatchSelectWidth: true,
  78. onChange: value => trySetOpIds(value),
  79. treeDefaultExpandAll: true,
  80. treeNodeFilterProp: 'title',
  81. maxTagCount: 5
  82. }
  83. if ([statisticOptionMap.STAFF, statisticOptionMap.DEPARTMENT].includes(state.optionType)) {
  84. base.multiple = true
  85. }
  86. if ([statisticOptionMap.STAFF, statisticOptionMap.SINGLE_STAFF].includes(state.optionType)) {
  87. base.showCheckedStrategy = 'SHOW_PARENT'
  88. base.treeData = state.optionType === statisticOptionMap.SINGLE_STAFF ? generateTree(state.staff) : state.staff
  89. }
  90. if ([statisticOptionMap.DEPARTMENT, statisticOptionMap.SINGLE_DEPARTMENT].includes(state.optionType)) {
  91. base.treeCheckable = state.optionType === statisticOptionMap.SINGLE_DEPARTMENT ? false : true
  92. base.treeData = state.department
  93. }
  94. return base
  95. }, [state.optionType, state.department, state.staff])
  96. return (
  97. <div>
  98. <span className="mr-2">
  99. <Select
  100. defaultValue={state.optionType}
  101. options={options.filter(item => {
  102. if (!multiple) {
  103. return ['singleDepartment', 'singleStaff'].includes(item.value)
  104. } else {
  105. return ['department', 'staff'].includes(item.value)
  106. }
  107. })}
  108. onChange={val => setState({ ...state, optionType: val, dataIds: [] })}
  109. />
  110. </span>
  111. <TreeSelect value={state.dataIds} {...treeOptions} />
  112. </div>
  113. )
  114. }
  115. export default OptionSelect