index.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. const defaultItems = [
  9. { value: 1, name: 'option1' },
  10. { value: 2, name: 'option2' },
  11. { value: 3, name: 'option3' },
  12. { value: 4, name: 'option4' },
  13. { value: 5, name: 'option5' },
  14. { value: 6, name: 'option6' },
  15. { value: 7, name: 'option7' },
  16. ]
  17. export type Item = {
  18. value: number | string
  19. name: string
  20. }
  21. export type ISelectProps = {
  22. className?: string
  23. wrapperClassName?: string
  24. items?: Item[]
  25. defaultValue?: number | string
  26. disabled?: boolean
  27. onSelect: (value: Item) => void
  28. allowSearch?: boolean
  29. bgClassName?: string
  30. placeholder?: string
  31. }
  32. const Select: FC<ISelectProps> = ({
  33. className,
  34. items = defaultItems,
  35. defaultValue = 1,
  36. disabled = false,
  37. onSelect,
  38. allowSearch = true,
  39. bgClassName = 'bg-gray-100',
  40. }) => {
  41. const [query, setQuery] = useState('')
  42. const [open, setOpen] = useState(false)
  43. const [selectedItem, setSelectedItem] = useState<Item | null>(null)
  44. useEffect(() => {
  45. let defaultSelect = null
  46. const existed = items.find((item: Item) => item.value === defaultValue)
  47. if (existed) {
  48. defaultSelect = existed
  49. }
  50. setSelectedItem(defaultSelect)
  51. }, [defaultValue])
  52. const filteredItems: Item[]
  53. = query === ''
  54. ? items
  55. : items.filter((item) => {
  56. return item.name.toLowerCase().includes(query.toLowerCase())
  57. })
  58. return (
  59. <Combobox
  60. as="div"
  61. disabled={disabled}
  62. value={selectedItem}
  63. className={className}
  64. onChange={(value: Item) => {
  65. if (!disabled) {
  66. setSelectedItem(value)
  67. setOpen(false)
  68. onSelect(value)
  69. }
  70. }}>
  71. <div className={classNames('relative')}>
  72. <div className='group text-gray-800'>
  73. {allowSearch
  74. ? <Combobox.Input
  75. 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`}
  76. onChange={(event) => {
  77. if (!disabled)
  78. setQuery(event.target.value)
  79. }}
  80. displayValue={(item: Item) => item?.name}
  81. />
  82. : <Combobox.Button onClick={
  83. () => {
  84. if (!disabled)
  85. setOpen(!open)
  86. }
  87. } 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`}>
  88. {selectedItem?.name}
  89. </Combobox.Button>}
  90. <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={
  91. () => {
  92. if (!disabled)
  93. setOpen(!open)
  94. }
  95. }>
  96. {open ? <ChevronUpIcon className="h-5 w-5" /> : <ChevronDownIcon className="h-5 w-5" />}
  97. </Combobox.Button>
  98. </div>
  99. {filteredItems.length > 0 && (
  100. <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">
  101. {filteredItems.map((item: Item) => (
  102. <Combobox.Option
  103. key={item.value}
  104. value={item}
  105. className={({ active }: { active: boolean }) =>
  106. classNames(
  107. 'relative cursor-default select-none py-2 pl-3 pr-9 rounded-lg hover:bg-gray-100 text-gray-700',
  108. active ? 'bg-gray-100' : '',
  109. )
  110. }
  111. >
  112. {({ /* active, */ selected }) => (
  113. <>
  114. <span className={classNames('block truncate', selected && 'font-normal')}>{item.name}</span>
  115. {selected && (
  116. <span
  117. className={classNames(
  118. 'absolute inset-y-0 right-0 flex items-center pr-4 text-gray-700',
  119. )}
  120. >
  121. <CheckIcon className="h-5 w-5" aria-hidden="true" />
  122. </span>
  123. )}
  124. </>
  125. )}
  126. </Combobox.Option>
  127. ))}
  128. </Combobox.Options>
  129. )}
  130. </div>
  131. </Combobox >
  132. )
  133. }
  134. const SimpleSelect: FC<ISelectProps> = ({
  135. className,
  136. wrapperClassName,
  137. items = defaultItems,
  138. defaultValue = 1,
  139. disabled = false,
  140. onSelect,
  141. placeholder,
  142. }) => {
  143. const { t } = useTranslation()
  144. const localPlaceholder = placeholder || t('common.placeholder.select')
  145. const [selectedItem, setSelectedItem] = useState<Item | null>(null)
  146. useEffect(() => {
  147. let defaultSelect = null
  148. const existed = items.find((item: Item) => item.value === defaultValue)
  149. if (existed) {
  150. defaultSelect = existed
  151. }
  152. setSelectedItem(defaultSelect)
  153. }, [defaultValue])
  154. return (
  155. <Listbox
  156. value={selectedItem}
  157. onChange={(value: Item) => {
  158. if (!disabled) {
  159. setSelectedItem(value)
  160. onSelect(value)
  161. }
  162. }}
  163. >
  164. <div className={`relative h-9 ${wrapperClassName}`}>
  165. <Listbox.Button className={`w-full h-full rounded-lg border-0 bg-gray-100 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-pointer ${className}`}>
  166. <span className={classNames("block truncate text-left", !selectedItem?.name && 'text-gray-400')}>{selectedItem?.name ?? localPlaceholder}</span>
  167. <span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
  168. <ChevronDownIcon
  169. className="h-5 w-5 text-gray-400"
  170. aria-hidden="true"
  171. />
  172. </span>
  173. </Listbox.Button>
  174. <Transition
  175. as={Fragment}
  176. leave="transition ease-in duration-100"
  177. leaveFrom="opacity-100"
  178. leaveTo="opacity-0"
  179. >
  180. <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">
  181. {items.map((item: Item) => (
  182. <Listbox.Option
  183. key={item.value}
  184. className={({ active }) =>
  185. `relative cursor-pointer select-none py-2 pl-3 pr-9 rounded-lg hover:bg-gray-100 text-gray-700 ${active ? 'bg-gray-100' : ''
  186. }`
  187. }
  188. value={item}
  189. disabled={disabled}
  190. >
  191. {({ /* active, */ selected }) => (
  192. <>
  193. <span className={classNames('block truncate', selected && 'font-normal')}>{item.name}</span>
  194. {selected && (
  195. <span
  196. className={classNames(
  197. 'absolute inset-y-0 right-0 flex items-center pr-4 text-gray-700',
  198. )}
  199. >
  200. <CheckIcon className="h-5 w-5" aria-hidden="true" />
  201. </span>
  202. )}
  203. </>
  204. )}
  205. </Listbox.Option>
  206. ))}
  207. </Listbox.Options>
  208. </Transition>
  209. </div>
  210. </Listbox>
  211. )
  212. }
  213. export { SimpleSelect }
  214. export default React.memo(Select)