index.jsx 2.9 KB

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