index.tsx 10 KB

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