index.tsx 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { FC } from 'react'
  2. import { useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiArrowDownSLine, RiCheckLine, RiSortAsc, RiSortDesc } from '@remixicon/react'
  5. import cn from '@/utils/classnames'
  6. import {
  7. PortalToFollowElem,
  8. PortalToFollowElemContent,
  9. PortalToFollowElemTrigger,
  10. } from '@/app/components/base/portal-to-follow-elem'
  11. export type Item = {
  12. value: number | string
  13. name: string
  14. } & Record<string, any>
  15. type Props = {
  16. order?: string
  17. value: number | string
  18. items: Item[]
  19. onSelect: (item: any) => void
  20. }
  21. const Sort: FC<Props> = ({
  22. order,
  23. value,
  24. items,
  25. onSelect,
  26. }) => {
  27. const { t } = useTranslation()
  28. const [open, setOpen] = useState(false)
  29. const triggerContent = useMemo(() => {
  30. return items.find(item => item.value === value)?.name || ''
  31. }, [items, value])
  32. return (
  33. <div className='inline-flex items-center'>
  34. <PortalToFollowElem
  35. open={open}
  36. onOpenChange={setOpen}
  37. placement='bottom-start'
  38. offset={4}
  39. >
  40. <div className='relative'>
  41. <PortalToFollowElemTrigger
  42. onClick={() => setOpen(v => !v)}
  43. className='block'
  44. >
  45. <div className={cn(
  46. 'flex items-center px-2 py-1 rounded-l-lg bg-components-input-bg-normal cursor-pointer hover:bg-state-base-hover-alt',
  47. open && '!bg-state-base-hover-alt hover:bg-state-base-hover-alt',
  48. )}>
  49. <div className='p-1 flex items-center gap-0.5'>
  50. <div className='text-text-tertiary system-sm-regular'>{t('appLog.filter.sortBy')}</div>
  51. <div className={cn('system-sm-regular text-text-tertiary', !!value && 'text-text-secondary')}>
  52. {triggerContent}
  53. </div>
  54. </div>
  55. <RiArrowDownSLine className='h-4 w-4 text-text-tertiary' />
  56. </div>
  57. </PortalToFollowElemTrigger>
  58. <PortalToFollowElemContent className='z-[1002]'>
  59. <div className='relative w-[240px] bg-components-panel-bg-blur rounded-xl border-[0.5px] border-components-panel-border shadow-lg'>
  60. <div className='p-1 max-h-72 overflow-auto'>
  61. {items.map(item => (
  62. <div
  63. key={item.value}
  64. className='flex items-center gap-2 pl-3 py-[6px] px-2 rounded-lg cursor-pointer hover:bg-state-base-hover'
  65. onClick={() => {
  66. onSelect(`${order}${item.value}`)
  67. setOpen(false)
  68. }}
  69. >
  70. <div title={item.name} className='grow text-text-secondary system-sm-medium truncate'>{item.name}</div>
  71. {value === item.value && <RiCheckLine className='shrink-0 w-4 h-4 text-util-colors-blue-light-blue-light-600' />}
  72. </div>
  73. ))}
  74. </div>
  75. </div>
  76. </PortalToFollowElemContent>
  77. </div>
  78. </PortalToFollowElem>
  79. <div className='ml-px p-2 rounded-r-lg bg-components-button-tertiary-bg hover:bg-components-button-tertiary-bg-hover cursor-pointer' onClick={() => onSelect(`${order ? '' : '-'}${value}`)}>
  80. {!order && <RiSortAsc className='w-4 h-4 text-components-button-tertiary-text' />}
  81. {order && <RiSortDesc className='w-4 h-4 text-components-button-tertiary-text' />}
  82. </div>
  83. </div>
  84. )
  85. }
  86. export default Sort