PermSelect.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { useModal } from '@/components/Modal'
  2. import { useModel } from 'umi'
  3. import { Select, Tooltip } from 'antd'
  4. import CustomPerm from './CustomPerm'
  5. import { getAuthCache, setAuthCache } from '@/utils/auth'
  6. import { PERMISSION_SELECT_KEY } from '@/utils/cache/cacheEnum'
  7. import type { SelectProps } from 'antd'
  8. import type { MainMenuKeyEnum } from '@/utils/cache/cacheEnum'
  9. import { SettingOutlined } from '@ant-design/icons'
  10. import { useState } from 'react'
  11. export const PermDataTypeEunm = {
  12. CUSTOMER: 'customer',
  13. PRODUCT: 'product',
  14. HR: 'hr',
  15. WORKBENCH: 'workbench'
  16. }
  17. interface PermSelectProps extends SelectProps {
  18. dataType: MainMenuKeyEnum
  19. onConfirm: () => void
  20. }
  21. const PermSelect: React.FC<PermSelectProps> = ({ dataType, onConfirm, ...resetProps }) => {
  22. const { initialState: { permData } = { permData: {} } } = useModel('@@initialState')
  23. const { toggleModal, setModalProps } = useModal()
  24. const [state, setState] = useState({
  25. staffIds: [],
  26. showSetting: false
  27. })
  28. let defaultValue = 'oneself'
  29. const options = [...(permData[dataType] || [])]
  30. const permAuthCache = getAuthCache(PERMISSION_SELECT_KEY)
  31. if (permAuthCache && permAuthCache[dataType]) {
  32. defaultValue = permAuthCache[dataType]
  33. }
  34. const handleOnChange = e => {
  35. if (e === 'custom') {
  36. setModalProps({
  37. width: '60vw',
  38. modalRender: () => (
  39. <CustomPerm
  40. defaultValues={state.staffIds}
  41. onConfirm={({ staffIds, dataPermission }) => {
  42. setState({ ...state, staffIds, showSetting: true })
  43. onConfirm({ staffIds, dataPermission })
  44. }}
  45. />
  46. ),
  47. onCancel: () => toggleModal()
  48. })
  49. toggleModal()
  50. return
  51. }
  52. onConfirm({ staffIds: null, dataPermission: e })
  53. setAuthCache(PERMISSION_SELECT_KEY, { ...permAuthCache, [dataType]: e })
  54. // 将setting icon隐藏,非自定义option下不显示
  55. state.showSetting && setState({ ...state, showSetting: false })
  56. }
  57. return (
  58. <>
  59. <Select
  60. {...resetProps}
  61. defaultValue={defaultValue}
  62. dropdownMatchSelectWidth={false}
  63. onChange={handleOnChange}
  64. options={options}
  65. />
  66. {state.showSetting ? (
  67. <Tooltip title="自定义权限">
  68. {/* <Button
  69. type="primary"
  70. shape="circle"
  71. className="ml-3"
  72. icon={}
  73. /> */}
  74. <SettingOutlined className="ml-3" onClick={() => handleOnChange('custom')} />
  75. </Tooltip>
  76. ) : null}
  77. </>
  78. )
  79. }
  80. export default PermSelect