index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Tree } from 'antd'
  2. import React, { useEffect } from 'react'
  3. import { connect } from 'umi'
  4. const { DirectoryTree } = Tree
  5. import './index.less'
  6. const Organization = props => {
  7. const { dispatch, departments = [], onSelect } = props
  8. const newList = [...departments]
  9. newList.unshift({
  10. children: [],
  11. id: '0',
  12. key: '0',
  13. isLeaf: false,
  14. name: '总全公司',
  15. title: '总全公司'
  16. })
  17. const initData = () => {
  18. dispatch({
  19. type: 'staff/fetchDepartments'
  20. })
  21. }
  22. function renderTreeNode(tree) {
  23. return tree.map(item => {
  24. const newItem = {
  25. ...item,
  26. title: (
  27. <div className="department-node">
  28. <div>{item.title}</div>
  29. </div>
  30. )
  31. }
  32. if (newItem.children?.length) {
  33. newItem.children = renderTreeNode(newItem.children)
  34. }
  35. return newItem
  36. })
  37. }
  38. useEffect(() => {
  39. if (!departments.length) {
  40. initData()
  41. }
  42. }, [])
  43. const saveActiveId = id => {
  44. if (onSelect) {
  45. onSelect(id)
  46. }
  47. }
  48. return (
  49. <div className="w-12rem rounded-4px flex flex-col">
  50. <div className="w-full p-4 border-b-1 border-solid border-black border-opacity-10 bg-hex-f7f9fa flex justify-around items-center flex-row">
  51. <span className="text-[0.9375rem]">组织架构</span>
  52. </div>
  53. <div className="py-4 bg-white flex-1">
  54. {departments.length ? (
  55. <DirectoryTree
  56. multiple
  57. defaultExpandAll
  58. defaultSelectedKeys={['0']}
  59. onSelect={keys => saveActiveId(keys[0])}
  60. showIcon={false}
  61. treeData={renderTreeNode(newList)}
  62. />
  63. ) : null}
  64. </div>
  65. </div>
  66. )
  67. }
  68. export default connect(({ staff }) => ({
  69. departments: staff.departments
  70. }))(Organization)