index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use client'
  2. import { useCallback } from 'react'
  3. import { ChevronDownIcon, PlusIcon } from '@heroicons/react/24/solid'
  4. import { Menu } from '@headlessui/react'
  5. import { useRouter } from 'next/navigation'
  6. import { debounce } from 'lodash-es'
  7. import Indicator from '../../indicator'
  8. import AppIcon from '@/app/components/base/app-icon'
  9. type NavItem = {
  10. id: string
  11. name: string
  12. link: string
  13. icon: string
  14. icon_background: string
  15. }
  16. export type INavSelectorProps = {
  17. navs: NavItem[]
  18. curNav?: Omit<NavItem, 'link'>
  19. createText: string
  20. onCreate: () => void
  21. onLoadmore?: () => void
  22. }
  23. const itemClassName = `
  24. flex items-center w-full h-10 px-3 text-gray-700 text-[14px]
  25. rounded-lg font-normal hover:bg-gray-100 cursor-pointer truncate
  26. `
  27. const NavSelector = ({ curNav, navs, createText, onCreate, onLoadmore }: INavSelectorProps) => {
  28. const router = useRouter()
  29. const handleScroll = useCallback(debounce((e) => {
  30. if (typeof onLoadmore === 'function') {
  31. const { clientHeight, scrollHeight, scrollTop } = e.target
  32. if (clientHeight + scrollTop > scrollHeight - 50)
  33. onLoadmore()
  34. }
  35. }, 50), [])
  36. return (
  37. <div className="">
  38. <Menu as="div" className="relative inline-block text-left">
  39. <div>
  40. <Menu.Button
  41. className="
  42. inline-flex items-center w-full h-7 justify-center
  43. rounded-[10px] pl-2 pr-2.5 text-[14px] font-semibold
  44. text-[#1C64F2] hover:bg-[#EBF5FF]
  45. "
  46. >
  47. <div className='max-w-[180px] truncate' title={curNav?.name}>{curNav?.name}</div>
  48. <ChevronDownIcon
  49. className="shrink-0 w-3 h-3 ml-1"
  50. aria-hidden="true"
  51. />
  52. </Menu.Button>
  53. </div>
  54. <Menu.Items
  55. className="
  56. absolute -left-11 right-0 mt-1.5 w-60 max-w-80
  57. divide-y divide-gray-100 origin-top-right rounded-lg bg-white
  58. shadow-[0_10px_15px_-3px_rgba(0,0,0,0.1),0_4px_6px_rgba(0,0,0,0.05)]
  59. "
  60. >
  61. <div className="px-1 py-1 overflow-auto" style={{ maxHeight: '50vh' }} onScroll={handleScroll}>
  62. {
  63. navs.map(nav => (
  64. <Menu.Item key={nav.id}>
  65. <div className={itemClassName} onClick={() => router.push(nav.link)} title={nav.name}>
  66. <div className='relative w-6 h-6 mr-2 bg-[#D5F5F6] rounded-[6px]'>
  67. <AppIcon size='tiny' icon={nav.icon} background={nav.icon_background}/>
  68. <div className='flex justify-center items-center absolute -right-0.5 -bottom-0.5 w-2.5 h-2.5 bg-white rounded'>
  69. <Indicator />
  70. </div>
  71. </div>
  72. {nav.name}
  73. </div>
  74. </Menu.Item>
  75. ))
  76. }
  77. </div>
  78. <Menu.Item>
  79. <div className='p-1' onClick={onCreate}>
  80. <div
  81. className='flex items-center h-12 rounded-lg cursor-pointer hover:bg-gray-100'
  82. >
  83. <div
  84. className='
  85. flex justify-center items-center
  86. ml-4 mr-2 w-6 h-6 bg-gray-100 rounded-[6px]
  87. border-[0.5px] border-gray-200 border-dashed
  88. '
  89. >
  90. <PlusIcon className='w-4 h-4 text-gray-500' />
  91. </div>
  92. <div className='font-normal text-[14px] text-gray-700'>{createText}</div>
  93. </div>
  94. </div>
  95. </Menu.Item>
  96. </Menu.Items>
  97. </Menu>
  98. </div>
  99. )
  100. }
  101. export default NavSelector