index.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React, { useEffect } from 'react'
  2. import { Button, message, Tree, TreeSelect, Form } from 'antd'
  3. import { PlusOutlined } from '@ant-design/icons'
  4. import consts from '@/consts'
  5. import { apiaddDepartment } from '@/services/department'
  6. import { connect } from 'dva'
  7. import { ModalForm, ProFormText } from '@ant-design/pro-form'
  8. const RoleMenu = props => {
  9. const { dispatch, departments = [], onSelect } = props
  10. const newList = [...departments]
  11. newList.unshift({
  12. children: [],
  13. id: '0',
  14. key: '0',
  15. isLeaf: false,
  16. name: '总全公司',
  17. title: '总全公司'
  18. })
  19. const { DirectoryTree } = Tree
  20. const initData = () => {
  21. dispatch({
  22. type: 'staff/fetchDepartments'
  23. })
  24. }
  25. const handleAddDepartment = async values => {
  26. const { code = -1 } = await apiaddDepartment({ ...values })
  27. if (code === consts.RET_CODE.SUCCESS) {
  28. // message.success('新增成功')
  29. initData()
  30. }
  31. }
  32. useEffect(() => {
  33. if (!departments.length) {
  34. initData()
  35. }
  36. }, [])
  37. const saveActiveId = id => {
  38. // console.log(keys)
  39. if (onSelect) {
  40. onSelect(id)
  41. }
  42. }
  43. const onExpand = () => {}
  44. return (
  45. <div className="h-full w-max-234px rounded-4px">
  46. <div className="p-4 pb-3 border-b-1 border-solid border-black border-opacity-10 bg-[#f7f9fa] flex justify-around items-center">
  47. <span className="text-[0.9375rem]">组织架构</span>
  48. <ModalForm
  49. title="创建部门"
  50. width="500px"
  51. trigger={
  52. <Button size="small" type="primary" ghost>
  53. <PlusOutlined />
  54. 创建部门
  55. </Button>
  56. }
  57. onFinish={async values => {
  58. await handleAddDepartment(values)
  59. message.success('添加成功')
  60. return true
  61. }}>
  62. <ProFormText name="backstageMenuId" hidden />
  63. <ProFormText name="name" label="新增部门" rules={[{ message: '请输入部门名称' }]} />
  64. <Form.Item label="上级部门" name="parentId">
  65. <TreeSelect
  66. style={{ width: '100%' }}
  67. placeholder="请选择上司"
  68. treeDefaultExpandAll
  69. treeData={departments}
  70. allowClear
  71. />
  72. </Form.Item>
  73. {/* <ProFormTreeSelect
  74. width="md"
  75. options={departments.map(item => ({ label: item.name, value: item.id }))}
  76. name="parentId"
  77. label="上级部门"
  78. /> */}
  79. </ModalForm>
  80. </div>
  81. <div className="p-4 bg-white" style={{ height: 'calc(100% - 1rem*2 - 20px)' }}>
  82. {departments.length && (
  83. <DirectoryTree
  84. multiple
  85. defaultExpandAll
  86. defaultSelectedKeys={['0']}
  87. onSelect={keys => saveActiveId(keys[0])}
  88. onExpand={onExpand}
  89. showIcon={false}
  90. treeData={newList}
  91. />
  92. )}
  93. </div>
  94. </div>
  95. )
  96. }
  97. // export default RoleMenu
  98. export default connect(({ staff }) => ({
  99. departments: staff.departments
  100. }))(RoleMenu)