|
|
@@ -4,23 +4,49 @@ import { getAuthCache, setAuthCache } from '@/utils/auth'
|
|
|
import { STATISTIC_OPTION_KEY } from '@/utils/cache/cacheEnum'
|
|
|
import { useDebounceFn } from 'ahooks'
|
|
|
import { Select, TreeSelect } from 'antd'
|
|
|
-import React, { useState, useEffect } from 'react'
|
|
|
+import React, { useState, useEffect, useMemo } from 'react'
|
|
|
|
|
|
export enum statisticOptionMap {
|
|
|
STAFF = 'staff',
|
|
|
- DEPARTMENT = 'department'
|
|
|
+ DEPARTMENT = 'department',
|
|
|
+ SINGLE_STAFF = 'singleStaff',
|
|
|
+ SINGLE_DEPARTMENT = 'singleDepartment'
|
|
|
}
|
|
|
|
|
|
type OptionSelectProps = {
|
|
|
+ /** @name 是否多选 */
|
|
|
+ multiple?: boolean
|
|
|
type: string
|
|
|
onChange: (type: 'staff' | 'department', value) => void
|
|
|
}
|
|
|
-const OptionSelect: React.FC<OptionSelectProps> = ({ type, onChange }) => {
|
|
|
+
|
|
|
+const options = [
|
|
|
+ { label: '按部门', value: 'department' },
|
|
|
+ { label: '按员工', value: 'staff' },
|
|
|
+ { label: '指定部门', value: 'singleDepartment' },
|
|
|
+ { label: '指定员工', value: 'singleStaff' }
|
|
|
+]
|
|
|
+
|
|
|
+function generateTree(tree) {
|
|
|
+ return tree.map(item => {
|
|
|
+ const newItem = { ...item }
|
|
|
+ if (item.children?.length) {
|
|
|
+ newItem.children = generateTree(item.children)
|
|
|
+ }
|
|
|
+ if (!item.isLeaf) {
|
|
|
+ newItem.disabled = true
|
|
|
+ }
|
|
|
+ return newItem
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+const OptionSelect: React.FC<OptionSelectProps> = ({ multiple = true, type, onChange }) => {
|
|
|
const authOptions = getAuthCache(STATISTIC_OPTION_KEY)
|
|
|
const { optionType, dataIds } = authOptions?.[type] || {}
|
|
|
|
|
|
const [state, setState] = useState({
|
|
|
- optionType: optionType || statisticOptionMap.STAFF,
|
|
|
+ optionType:
|
|
|
+ optionType || (multiple ? statisticOptionMap.STAFF : statisticOptionMap.SINGLE_STAFF),
|
|
|
dataIds: dataIds || [],
|
|
|
department: [],
|
|
|
staff: []
|
|
|
@@ -55,40 +81,56 @@ const OptionSelect: React.FC<OptionSelectProps> = ({ type, onChange }) => {
|
|
|
},
|
|
|
{ wait: 500 }
|
|
|
)
|
|
|
+
|
|
|
+ const treeOptions = useMemo(() => {
|
|
|
+ const base = {
|
|
|
+ className: 'min-w-180px',
|
|
|
+ dropdownMatchSelectWidth: true,
|
|
|
+ onChange: value => trySetOpIds(value)
|
|
|
+ }
|
|
|
+ if ([statisticOptionMap.STAFF, statisticOptionMap.DEPARTMENT].includes(state.optionType)) {
|
|
|
+ base.multiple = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if ([statisticOptionMap.STAFF, statisticOptionMap.SINGLE_STAFF].includes(state.optionType)) {
|
|
|
+ base.showCheckedStrategy = 'SHOW_PARENT'
|
|
|
+
|
|
|
+ base.treeData =
|
|
|
+ state.optionType === statisticOptionMap.SINGLE_STAFF
|
|
|
+ ? generateTree(state.staff)
|
|
|
+ : state.staff
|
|
|
+ }
|
|
|
+
|
|
|
+ if (
|
|
|
+ [statisticOptionMap.DEPARTMENT, statisticOptionMap.SINGLE_DEPARTMENT].includes(
|
|
|
+ state.optionType
|
|
|
+ )
|
|
|
+ ) {
|
|
|
+ base.treeCheckable = state.optionType === statisticOptionMap.SINGLE_DEPARTMENT ? false : true
|
|
|
+ base.treeData = state.department
|
|
|
+ }
|
|
|
+
|
|
|
+ return base
|
|
|
+ }, [state.optionType, state.department, state.staff])
|
|
|
+
|
|
|
+ console.log('options', options)
|
|
|
+
|
|
|
return (
|
|
|
<div>
|
|
|
<span className="mr-2">
|
|
|
<Select
|
|
|
defaultValue={state.optionType}
|
|
|
- options={[
|
|
|
- { label: '按部门', value: 'department' },
|
|
|
- { label: '按员工', value: 'staff' }
|
|
|
- ]}
|
|
|
+ options={options.filter(item => {
|
|
|
+ if (!multiple) {
|
|
|
+ return ['singleDepartment', 'singleStaff'].includes(item.value)
|
|
|
+ } else {
|
|
|
+ return ['department', 'staff'].includes(item.value)
|
|
|
+ }
|
|
|
+ })}
|
|
|
onChange={val => setState({ ...state, optionType: val, dataIds: [] })}
|
|
|
/>
|
|
|
</span>
|
|
|
- {state.optionType === statisticOptionMap.STAFF ? (
|
|
|
- <TreeSelect
|
|
|
- value={state.dataIds}
|
|
|
- className="min-w-180px"
|
|
|
- // defaultOpen
|
|
|
- treeCheckable
|
|
|
- showCheckedStrategy="SHOW_PARENT"
|
|
|
- dropdownMatchSelectWidth
|
|
|
- treeData={state.staff}
|
|
|
- onChange={value => trySetOpIds(value)}
|
|
|
- />
|
|
|
- ) : (
|
|
|
- <TreeSelect
|
|
|
- value={state.dataIds}
|
|
|
- className="min-w-180px"
|
|
|
- // defaultOpen
|
|
|
- treeData={state.department}
|
|
|
- dropdownMatchSelectWidth
|
|
|
- multiple
|
|
|
- onChange={value => trySetOpIds(value)}
|
|
|
- />
|
|
|
- )}
|
|
|
+ <TreeSelect value={state.dataIds} {...treeOptions} />
|
|
|
</div>
|
|
|
)
|
|
|
}
|