index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { Fragment, useEffect, useState } from 'react'
  4. import { Combobox, Listbox, Transition } from '@headlessui/react'
  5. import classNames from 'classnames'
  6. import { CheckIcon, ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/20/solid'
  7. import { useTranslation } from 'react-i18next'
  8. import {
  9. PortalToFollowElem,
  10. PortalToFollowElemContent,
  11. PortalToFollowElemTrigger,
  12. } from '@/app/components/base/portal-to-follow-elem'
  13. const defaultItems = [
  14. { value: 1, name: 'option1' },
  15. { value: 2, name: 'option2' },
  16. { value: 3, name: 'option3' },
  17. { value: 4, name: 'option4' },
  18. { value: 5, name: 'option5' },
  19. { value: 6, name: 'option6' },
  20. { value: 7, name: 'option7' },
  21. ]
  22. export type Item = {
  23. value: number | string
  24. name: string
  25. }
  26. export type ISelectProps = {
  27. className?: string
  28. wrapperClassName?: string
  29. items?: Item[]
  30. defaultValue?: number | string
  31. disabled?: boolean
  32. onSelect: (value: Item) => void
  33. allowSearch?: boolean
  34. bgClassName?: string
  35. placeholder?: string
  36. overlayClassName?: string
  37. optionClassName?: string
  38. }
  39. const Select: FC<ISelectProps> = ({
  40. className,
  41. items = defaultItems,
  42. defaultValue = 1,
  43. disabled = false,
  44. onSelect,
  45. allowSearch = true,
  46. bgClassName = 'bg-gray-100',
  47. overlayClassName,
  48. optionClassName,
  49. }) => {
  50. const [query, setQuery] = useState('')
  51. const [open, setOpen] = useState(false)
  52. const [selectedItem, setSelectedItem] = useState<Item | null>(null)
  53. useEffect(() => {
  54. let defaultSelect = null
  55. const existed = items.find((item: Item) => item.value === defaultValue)
  56. if (existed)
  57. defaultSelect = existed
  58. setSelectedItem(defaultSelect)
  59. }, [defaultValue])
  60. const filteredItems: Item[]
  61. = query === ''
  62. ? items
  63. : items.filter((item) => {
  64. return item.name.toLowerCase().includes(query.toLowerCase())
  65. })
  66. return (
  67. <Combobox
  68. as="div"
  69. disabled={disabled}
  70. value={selectedItem}
  71. className={className}
  72. onChange={(value: Item) => {
  73. if (!disabled) {
  74. setSelectedItem(value)
  75. setOpen(false)
  76. onSelect(value)
  77. }
  78. }}>
  79. <div className={classNames('relative')}>
  80. <div className='group text-gray-800'>
  81. {allowSearch
  82. ? <Combobox.Input
  83. className={`w-full rounded-lg border-0 ${bgClassName} py-1.5 pl-3 pr-10 shadow-sm sm:text-sm sm:leading-6 focus-visible:outline-none focus-visible:bg-gray-200 group-hover:bg-gray-200 cursor-not-allowed`}
  84. onChange={(event) => {
  85. if (!disabled)
  86. setQuery(event.target.value)
  87. }}
  88. displayValue={(item: Item) => item?.name}
  89. />
  90. : <Combobox.Button onClick={
  91. () => {
  92. if (!disabled)
  93. setOpen(!open)
  94. }
  95. } className={classNames(optionClassName, `flex items-center h-9 w-full rounded-lg border-0 ${bgClassName} py-1.5 pl-3 pr-10 shadow-sm sm:text-sm sm:leading-6 focus-visible:outline-none focus-visible:bg-gray-200 group-hover:bg-gray-200`)}>
  96. <div className='w-0 grow text-left truncate' title={selectedItem?.name}>{selectedItem?.name}</div>
  97. </Combobox.Button>}
  98. <Combobox.Button className="absolute inset-y-0 right-0 flex items-center rounded-r-md px-2 focus:outline-none group-hover:bg-gray-200" onClick={
  99. () => {
  100. if (!disabled)
  101. setOpen(!open)
  102. }
  103. }>
  104. {open ? <ChevronUpIcon className="h-5 w-5" /> : <ChevronDownIcon className="h-5 w-5" />}
  105. </Combobox.Button>
  106. </div>
  107. {filteredItems.length > 0 && (
  108. <Combobox.Options className={`absolute z-10 mt-1 px-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm ${overlayClassName}`}>
  109. {filteredItems.map((item: Item) => (
  110. <Combobox.Option
  111. key={item.value}
  112. value={item}
  113. className={({ active }: { active: boolean }) =>
  114. classNames(
  115. optionClassName,
  116. 'relative cursor-default select-none py-2 pl-3 pr-9 rounded-lg hover:bg-gray-100 text-gray-700',
  117. active ? 'bg-gray-100' : '',
  118. )
  119. }
  120. >
  121. {({ /* active, */ selected }) => (
  122. <>
  123. <span className={classNames('block', selected && 'font-normal')}>{item.name}</span>
  124. {selected && (
  125. <span
  126. className={classNames(
  127. 'absolute inset-y-0 right-0 flex items-center pr-4 text-gray-700',
  128. )}
  129. >
  130. <CheckIcon className="h-5 w-5" aria-hidden="true" />
  131. </span>
  132. )}
  133. </>
  134. )}
  135. </Combobox.Option>
  136. ))}
  137. </Combobox.Options>
  138. )}
  139. </div>
  140. </Combobox >
  141. )
  142. }
  143. const SimpleSelect: FC<ISelectProps> = ({
  144. className,
  145. wrapperClassName = '',
  146. items = defaultItems,
  147. defaultValue = 1,
  148. disabled = false,
  149. onSelect,
  150. placeholder,
  151. }) => {
  152. const { t } = useTranslation()
  153. const localPlaceholder = placeholder || t('common.placeholder.select')
  154. const [selectedItem, setSelectedItem] = useState<Item | null>(null)
  155. useEffect(() => {
  156. let defaultSelect = null
  157. const existed = items.find((item: Item) => item.value === defaultValue)
  158. if (existed)
  159. defaultSelect = existed
  160. setSelectedItem(defaultSelect)
  161. }, [defaultValue])
  162. return (
  163. <Listbox
  164. value={selectedItem}
  165. onChange={(value: Item) => {
  166. if (!disabled) {
  167. setSelectedItem(value)
  168. onSelect(value)
  169. }
  170. }}
  171. >
  172. <div className={`relative h-9 ${wrapperClassName}`}>
  173. <Listbox.Button className={`w-full h-full rounded-lg border-0 bg-gray-100 py-1.5 pl-3 pr-10 sm:text-sm sm:leading-6 focus-visible:outline-none focus-visible:bg-gray-200 group-hover:bg-gray-200 ${disabled ? 'cursor-not-allowed' : 'cursor-pointer'} ${className}`}>
  174. <span className={classNames('block truncate text-left', !selectedItem?.name && 'text-gray-400')}>{selectedItem?.name ?? localPlaceholder}</span>
  175. <span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
  176. <ChevronDownIcon
  177. className="h-5 w-5 text-gray-400"
  178. aria-hidden="true"
  179. />
  180. </span>
  181. </Listbox.Button>
  182. {!disabled && (
  183. <Transition
  184. as={Fragment}
  185. leave="transition ease-in duration-100"
  186. leaveFrom="opacity-100"
  187. leaveTo="opacity-0"
  188. >
  189. <Listbox.Options className="absolute z-10 mt-1 px-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm">
  190. {items.map((item: Item) => (
  191. <Listbox.Option
  192. key={item.value}
  193. className={({ active }) =>
  194. `relative cursor-pointer select-none py-2 pl-3 pr-9 rounded-lg hover:bg-gray-100 text-gray-700 ${active ? 'bg-gray-100' : ''
  195. }`
  196. }
  197. value={item}
  198. disabled={disabled}
  199. >
  200. {({ /* active, */ selected }) => (
  201. <>
  202. <span className={classNames('block', selected && 'font-normal')}>{item.name}</span>
  203. {selected && (
  204. <span
  205. className={classNames(
  206. 'absolute inset-y-0 right-0 flex items-center pr-4 text-gray-700',
  207. )}
  208. >
  209. <CheckIcon className="h-5 w-5" aria-hidden="true" />
  210. </span>
  211. )}
  212. </>
  213. )}
  214. </Listbox.Option>
  215. ))}
  216. </Listbox.Options>
  217. </Transition>
  218. )}
  219. </div>
  220. </Listbox>
  221. )
  222. }
  223. type PortalSelectProps = {
  224. value: string | number
  225. onSelect: (value: Item) => void
  226. items: Item[]
  227. placeholder?: string
  228. popupClassName?: string
  229. }
  230. const PortalSelect: FC<PortalSelectProps> = ({
  231. value,
  232. onSelect,
  233. items,
  234. placeholder,
  235. popupClassName,
  236. }) => {
  237. const { t } = useTranslation()
  238. const [open, setOpen] = useState(false)
  239. const localPlaceholder = placeholder || t('common.placeholder.select')
  240. const selectedItem = items.find(item => item.value === value)
  241. return (
  242. <PortalToFollowElem
  243. open={open}
  244. onOpenChange={setOpen}
  245. placement='bottom-start'
  246. offset={4}
  247. >
  248. <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)} className='w-full'>
  249. <div
  250. className={`
  251. flex items-center justify-between px-2.5 h-9 rounded-lg border-0 bg-gray-100 text-sm cursor-pointer
  252. `}
  253. title={selectedItem?.name}
  254. >
  255. <span
  256. className={`
  257. grow truncate
  258. ${!selectedItem?.name && 'text-gray-400'}
  259. `}
  260. >
  261. {selectedItem?.name ?? localPlaceholder}
  262. </span>
  263. <ChevronDownIcon className='shrink-0 h-4 w-4 text-gray-400' />
  264. </div>
  265. </PortalToFollowElemTrigger>
  266. <PortalToFollowElemContent className={`z-20 ${popupClassName}`}>
  267. <div
  268. className='px-1 py-1 max-h-60 overflow-auto rounded-md bg-white text-base shadow-lg border-gray-200 border-[0.5px] focus:outline-none sm:text-sm'
  269. >
  270. {items.map((item: Item) => (
  271. <div
  272. key={item.value}
  273. className={`
  274. flex items-center justify-between px-2.5 h-9 cursor-pointer rounded-lg hover:bg-gray-100 text-gray-700
  275. ${item.value === value && 'bg-gray-100'}
  276. `}
  277. title={item.name}
  278. onClick={() => {
  279. onSelect(item)
  280. setOpen(false)
  281. }}
  282. >
  283. <span
  284. className='w-0 grow truncate'
  285. title={item.name}
  286. >
  287. {item.name}
  288. </span>
  289. {item.value === value && (
  290. <CheckIcon className='shrink-0 h-4 w-4' />
  291. )}
  292. </div>
  293. ))}
  294. </div>
  295. </PortalToFollowElemContent>
  296. </PortalToFollowElem>
  297. )
  298. }
  299. export { SimpleSelect, PortalSelect }
  300. export default React.memo(Select)