index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type { FC } from 'react'
  2. import { useMemo, useState } from 'react'
  3. import { RiArrowDownSLine, RiCheckLine, RiCloseCircleFill, RiFilter3Line } from '@remixicon/react'
  4. import cn from '@/utils/classnames'
  5. import {
  6. PortalToFollowElem,
  7. PortalToFollowElemContent,
  8. PortalToFollowElemTrigger,
  9. } from '@/app/components/base/portal-to-follow-elem'
  10. export type Item = {
  11. value: number | string
  12. name: string
  13. } & Record<string, any>
  14. type Props = {
  15. className?: string
  16. panelClassName?: string
  17. showLeftIcon?: boolean
  18. leftIcon?: any
  19. value: number | string
  20. items: Item[]
  21. onSelect: (item: any) => void
  22. onClear: () => void
  23. }
  24. const Chip: FC<Props> = ({
  25. className,
  26. panelClassName,
  27. showLeftIcon = true,
  28. leftIcon,
  29. value,
  30. items,
  31. onSelect,
  32. onClear,
  33. }) => {
  34. const [open, setOpen] = useState(false)
  35. const triggerContent = useMemo(() => {
  36. return items.find(item => item.value === value)?.name || ''
  37. }, [items, value])
  38. return (
  39. <PortalToFollowElem
  40. open={open}
  41. onOpenChange={setOpen}
  42. placement='bottom-start'
  43. offset={4}
  44. >
  45. <div className='relative'>
  46. <PortalToFollowElemTrigger
  47. onClick={() => setOpen(v => !v)}
  48. className='block'
  49. >
  50. <div className={cn(
  51. 'flex items-center min-h-8 px-2 py-1 rounded-lg border-[0.5px] border-transparent bg-components-input-bg-normal cursor-pointer hover:bg-state-base-hover-alt',
  52. open && !value && '!bg-state-base-hover-alt hover:bg-state-base-hover-alt',
  53. !open && !!value && '!bg-components-button-secondary-bg shadow-xs !border-components-button-secondary-border hover:!bg-components-button-secondary-bg-hover hover:border-components-button-secondary-border-hover',
  54. open && !!value && '!bg-components-button-secondary-bg-hover !border-components-button-secondary-border-hover shadow-xs hover:!bg-components-button-secondary-bg-hover hover:border-components-button-secondary-border-hover',
  55. className,
  56. )}>
  57. {showLeftIcon && (
  58. <div className='p-0.5'>
  59. {leftIcon || (
  60. <RiFilter3Line className={cn('h-4 w-4 text-text-tertiary', !!value && 'text-text-secondary')} />
  61. )}
  62. </div>
  63. )}
  64. <div className='grow first-line:p-1 flex items-center gap-0.5'>
  65. <div className={cn('system-sm-regular text-text-tertiary', !!value && 'text-text-secondary')}>
  66. {triggerContent}
  67. </div>
  68. </div>
  69. {!value && <RiArrowDownSLine className='h-4 w-4 text-text-tertiary' />}
  70. {!!value && (
  71. <div className='p-[1px] cursor-pointer group/clear' onClick={(e) => {
  72. e.stopPropagation()
  73. onClear()
  74. }}>
  75. <RiCloseCircleFill className='h-3.5 w-3.5 text-text-quaternary group-hover/clear:text-text-tertiary' />
  76. </div>
  77. )}
  78. </div>
  79. </PortalToFollowElemTrigger>
  80. <PortalToFollowElemContent className='z-[1002]'>
  81. <div className={cn('relative w-[240px] bg-components-panel-bg-blur rounded-xl border-[0.5px] border-components-panel-border shadow-lg', panelClassName)}>
  82. <div className='p-1 max-h-72 overflow-auto'>
  83. {items.map(item => (
  84. <div
  85. key={item.value}
  86. className='flex items-center gap-2 pl-3 py-[6px] px-2 rounded-lg cursor-pointer hover:bg-state-base-hover'
  87. onClick={() => {
  88. onSelect(item)
  89. setOpen(false)
  90. }}
  91. >
  92. <div title={item.name} className='grow text-text-secondary system-sm-medium truncate'>{item.name}</div>
  93. {value === item.value && <RiCheckLine className='shrink-0 w-4 h-4 text-util-colors-blue-light-blue-light-600' />}
  94. </div>
  95. ))}
  96. </div>
  97. </div>
  98. </PortalToFollowElemContent>
  99. </div>
  100. </PortalToFollowElem>
  101. )
  102. }
  103. export default Chip