index.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useState, useMemo, useCallback } from 'react';
  2. import { IconUser, IconStar, IconUserGroup, IconEdit, IconApps, IconSetting } from '@douyinfe/semi-icons';
  3. import { Nav, Button } from '../../../index';
  4. export default function Demo() {
  5. const [selectedKeys, setSelectedKeys] = useState(['人员管理']);
  6. const navItems = useMemo(
  7. () => [
  8. { itemKey: 'user', text: '用户管理', icon: <IconUser />, link: '#' },
  9. { itemKey: 'union', text: '公会中心', icon: <IconStar /> },
  10. {
  11. itemKey: 'union-management',
  12. link: '#',
  13. text: '公会管理',
  14. icon: <IconUserGroup />,
  15. items: ['公告设置', '公会查询', '信息录入'],
  16. },
  17. {
  18. itemKey: 'approve-management',
  19. text: '审批管理',
  20. icon: <IconEdit />,
  21. items: [
  22. '入驻审核',
  23. {
  24. itemKey: 'operation-management',
  25. text: '运营管理',
  26. items: ['人员管理', '人员变更'],
  27. },
  28. ],
  29. },
  30. {
  31. text: '任务平台',
  32. icon: <IconSetting />,
  33. itemKey: 'job',
  34. items: ['任务管理', '用户任务查询'],
  35. },
  36. ],
  37. []
  38. );
  39. const navItemKeys = useMemo(
  40. () =>
  41. navItems.reduce((prev, item) => {
  42. const que = [item];
  43. while (que.length) {
  44. const cur = que.pop();
  45. if (cur) {
  46. if (typeof cur === 'object') {
  47. if (Array.isArray(cur.items) && cur.items.length) {
  48. que.push(...cur.items);
  49. } else if (cur.itemKey) {
  50. prev.push(cur.itemKey);
  51. }
  52. } else {
  53. prev.push(cur);
  54. }
  55. }
  56. }
  57. return prev;
  58. }, []),
  59. [navItems]
  60. );
  61. const randomSelect = useCallback(() => {
  62. const randomIndex = Math.floor(Math.random() * navItemKeys.length);
  63. setSelectedKeys([navItemKeys[randomIndex]]);
  64. }, [navItemKeys]);
  65. return (
  66. <div style={{ height: '100vh', display: 'inline-block' }}>
  67. <Button onClick={randomSelect}>随机选中</Button>
  68. <div>
  69. <Nav
  70. style={{ height: 800 }}
  71. header={{
  72. logo: 'bytedance_logo',
  73. text: 'Bytedance',
  74. link: '#',
  75. linkOptions: {
  76. target: '__blank',
  77. },
  78. }}
  79. onSelect={(...args) => console.log(...args)}
  80. defaultSelectedKeys={selectedKeys}
  81. items={navItems}
  82. footer={{ collapseButton: true }}
  83. />
  84. </div>
  85. </div>
  86. );
  87. }