selector.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import type { FC } from 'react'
  2. import { useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import { useDebounceFn, useMount } from 'ahooks'
  6. import { RiArrowDownSLine } from '@remixicon/react'
  7. import { useStore as useLabelStore } from './store'
  8. import cn from '@/utils/classnames'
  9. import {
  10. PortalToFollowElem,
  11. PortalToFollowElemContent,
  12. PortalToFollowElemTrigger,
  13. } from '@/app/components/base/portal-to-follow-elem'
  14. import Input from '@/app/components/base/input'
  15. import { Tag03 } from '@/app/components/base/icons/src/vender/line/financeAndECommerce'
  16. import Checkbox from '@/app/components/base/checkbox'
  17. import type { Label } from '@/app/components/tools/labels/constant'
  18. import { fetchLabelList } from '@/service/tools'
  19. import I18n from '@/context/i18n'
  20. import { getLanguage } from '@/i18n/language'
  21. type LabelSelectorProps = {
  22. value: string[]
  23. onChange: (v: string[]) => void
  24. }
  25. const LabelSelector: FC<LabelSelectorProps> = ({
  26. value,
  27. onChange,
  28. }) => {
  29. const { t } = useTranslation()
  30. const { locale } = useContext(I18n)
  31. const language = getLanguage(locale)
  32. const [open, setOpen] = useState(false)
  33. const labelList = useLabelStore(s => s.labelList)
  34. const setLabelList = useLabelStore(s => s.setLabelList)
  35. const [keywords, setKeywords] = useState('')
  36. const [searchKeywords, setSearchKeywords] = useState('')
  37. const { run: handleSearch } = useDebounceFn(() => {
  38. setSearchKeywords(keywords)
  39. }, { wait: 500 })
  40. const handleKeywordsChange = (value: string) => {
  41. setKeywords(value)
  42. handleSearch()
  43. }
  44. const filteredLabelList = useMemo(() => {
  45. return labelList.filter(label => label.name.includes(searchKeywords))
  46. }, [labelList, searchKeywords])
  47. const selectedLabels = useMemo(() => {
  48. return value.map(v => labelList.find(l => l.name === v)?.label[language]).join(', ')
  49. }, [value, labelList, language])
  50. const selectLabel = (label: Label) => {
  51. if (value.includes(label.name))
  52. onChange(value.filter(v => v !== label.name))
  53. else
  54. onChange([...value, label.name])
  55. }
  56. useMount(() => {
  57. fetchLabelList().then((res) => {
  58. setLabelList(res)
  59. })
  60. })
  61. return (
  62. <PortalToFollowElem
  63. open={open}
  64. onOpenChange={setOpen}
  65. placement='bottom-start'
  66. offset={4}
  67. >
  68. <div className='relative'>
  69. <PortalToFollowElemTrigger
  70. onClick={() => setOpen(v => !v)}
  71. className='block'
  72. >
  73. <div className={cn(
  74. 'flex items-center gap-1 px-3 h-10 rounded-lg border-[0.5px] border-transparent bg-gray-100 cursor-pointer hover:bg-gray-200',
  75. open && '!bg-gray-200 hover:bg-gray-200',
  76. )}>
  77. <div title={value.length > 0 ? selectedLabels : ''} className={cn('grow text-[13px] leading-[18px] text-gray-700 truncate', !value.length && '!text-gray-400')}>
  78. {!value.length && t('tools.createTool.toolInput.labelPlaceholder')}
  79. {!!value.length && selectedLabels}
  80. </div>
  81. <div className='shrink-0 ml-1 text-gray-700 opacity-60'>
  82. <RiArrowDownSLine className='h-4 w-4' />
  83. </div>
  84. </div>
  85. </PortalToFollowElemTrigger>
  86. <PortalToFollowElemContent className='z-[1040]'>
  87. <div className='relative w-[591px] bg-white rounded-lg border-[0.5px] border-gray-200 shadow-lg'>
  88. <div className='p-2 border-b-[0.5px] border-black/5'>
  89. <Input
  90. showLeftIcon
  91. showClearIcon
  92. value={keywords}
  93. onChange={e => handleKeywordsChange(e.target.value)}
  94. onClear={() => handleKeywordsChange('')}
  95. />
  96. </div>
  97. <div className='p-1 max-h-[264px] overflow-y-auto'>
  98. {filteredLabelList.map(label => (
  99. <div
  100. key={label.name}
  101. className='flex items-center gap-2 pl-3 py-[6px] pr-2 rounded-lg cursor-pointer hover:bg-gray-100'
  102. onClick={() => selectLabel(label)}
  103. >
  104. <Checkbox
  105. className='shrink-0'
  106. checked={value.includes(label.name)}
  107. onCheck={() => { }}
  108. />
  109. <div title={label.label[language]} className='grow text-sm text-gray-700 leading-5 truncate'>{label.label[language]}</div>
  110. </div>
  111. ))}
  112. {!filteredLabelList.length && (
  113. <div className='p-3 flex flex-col items-center gap-1'>
  114. <Tag03 className='h-6 w-6 text-gray-300' />
  115. <div className='text-gray-500 text-xs leading-[14px]'>{t('common.tag.noTag')}</div>
  116. </div>
  117. )}
  118. </div>
  119. </div>
  120. </PortalToFollowElemContent>
  121. </div>
  122. </PortalToFollowElem>
  123. )
  124. }
  125. export default LabelSelector