index.tsx 2.7 KB

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