OptionSelect.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. loading: false,
  43. optionType: optionType || (multiple ? statisticOptionMap.STAFF : statisticOptionMap.SINGLE_STAFF),
  44. dataIds: null,
  45. department: [],
  46. staff: []
  47. })
  48. useEffect(() => {
  49. setState({ ...state, loading: true })
  50. const queryStaffAndDepartment = async () => {
  51. const { code = -1, data: { department = [], staff = [] } = {} } = await queryUsageSelectOptions()
  52. if (code === consts.RET_CODE.SUCCESS) {
  53. setState({
  54. ...state,
  55. department,
  56. staff,
  57. dataIds,
  58. loading: false
  59. })
  60. }
  61. }
  62. queryStaffAndDepartment()
  63. if (optionType && dataIds?.length) {
  64. onChange(optionType, dataIds)
  65. }
  66. }, [])
  67. const { run: trySetOpIds } = useDebounceFn(
  68. ids => {
  69. setState({ ...state, dataIds: ids })
  70. onChange(state.optionType, ids)
  71. setAuthCache(STATISTIC_OPTION_KEY, {
  72. ...authOptions,
  73. [type]: { optionType: state.optionType, dataIds: ids }
  74. })
  75. },
  76. { wait: 500 }
  77. )
  78. const treeOptions = useMemo(() => {
  79. const base = {
  80. className: 'min-w-45',
  81. dropdownMatchSelectWidth: true,
  82. onChange: value => trySetOpIds(value),
  83. treeDefaultExpandAll: true,
  84. treeNodeFilterProp: 'title',
  85. maxTagCount: 5
  86. }
  87. if ([statisticOptionMap.STAFF, statisticOptionMap.DEPARTMENT].includes(state.optionType)) {
  88. base.multiple = true
  89. }
  90. if ([statisticOptionMap.STAFF, statisticOptionMap.SINGLE_STAFF].includes(state.optionType)) {
  91. base.showCheckedStrategy = 'SHOW_PARENT'
  92. base.treeData =
  93. state.optionType === statisticOptionMap.SINGLE_STAFF ? generateTree(state.staff) : state.staff
  94. }
  95. if ([statisticOptionMap.DEPARTMENT, statisticOptionMap.SINGLE_DEPARTMENT].includes(state.optionType)) {
  96. base.treeCheckable = state.optionType === statisticOptionMap.SINGLE_DEPARTMENT ? false : true
  97. base.treeData = state.department
  98. }
  99. return base
  100. }, [state.optionType, state.department, state.staff])
  101. return (
  102. <div>
  103. <span className="mr-2">
  104. <Select
  105. defaultValue={state.optionType}
  106. options={options.filter(item => {
  107. if (!multiple) {
  108. return ['singleDepartment', 'singleStaff'].includes(item.value)
  109. } else {
  110. return ['department', 'staff'].includes(item.value)
  111. }
  112. })}
  113. onChange={val => setState({ ...state, optionType: val, dataIds: [] })}
  114. />
  115. </span>
  116. <TreeSelect value={state.dataIds} {...treeOptions} loading={state.loading} />
  117. </div>
  118. )
  119. }
  120. export default OptionSelect