index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* eslint-disable multiline-ternary */
  2. 'use client'
  3. import type { ChangeEvent, FC } from 'react'
  4. import React, { useState } from 'react'
  5. import data from '@emoji-mart/data'
  6. import { SearchIndex, init } from 'emoji-mart'
  7. import cn from 'classnames'
  8. import {
  9. MagnifyingGlassIcon,
  10. } from '@heroicons/react/24/outline'
  11. import { useTranslation } from 'react-i18next'
  12. import s from './style.module.css'
  13. import Divider from '@/app/components/base/divider'
  14. import Button from '@/app/components/base/button'
  15. import Modal from '@/app/components/base/modal'
  16. declare global {
  17. namespace JSX {
  18. // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
  19. interface IntrinsicElements {
  20. 'em-emoji': React.DetailedHTMLProps<
  21. React.HTMLAttributes<HTMLElement>,
  22. HTMLElement
  23. >
  24. }
  25. }
  26. }
  27. init({ data })
  28. async function search(value: string) {
  29. const emojis = await SearchIndex.search(value) || []
  30. const results = emojis.map((emoji: any) => {
  31. return emoji.skins[0].native
  32. })
  33. return results
  34. }
  35. const backgroundColors = [
  36. '#FFEAD5',
  37. '#E4FBCC',
  38. '#D3F8DF',
  39. '#E0F2FE',
  40. '#E0EAFF',
  41. '#EFF1F5',
  42. '#FBE8FF',
  43. '#FCE7F6',
  44. '#FEF7C3',
  45. '#E6F4D7',
  46. '#D5F5F6',
  47. '#D1E9FF',
  48. '#D1E0FF',
  49. '#D5D9EB',
  50. '#ECE9FE',
  51. '#FFE4E8',
  52. ]
  53. type IEmojiPickerProps = {
  54. isModal?: boolean
  55. onSelect?: (emoji: string, background: string) => void
  56. onClose?: () => void
  57. }
  58. const EmojiPicker: FC<IEmojiPickerProps> = ({
  59. isModal = true,
  60. onSelect,
  61. onClose,
  62. }) => {
  63. const { t } = useTranslation()
  64. const { categories } = data as any
  65. const [selectedEmoji, setSelectedEmoji] = useState('')
  66. const [selectedBackground, setSelectedBackground] = useState(backgroundColors[0])
  67. const [searchedEmojis, setSearchedEmojis] = useState([])
  68. const [isSearching, setIsSearching] = useState(false)
  69. return isModal ? <Modal
  70. onClose={() => { }}
  71. isShow
  72. closable={false}
  73. wrapperClassName='!z-40'
  74. className={cn(s.container, '!w-[362px] !p-0')}
  75. >
  76. <div className='flex flex-col items-center w-full p-3'>
  77. <div className="relative w-full">
  78. <div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
  79. <MagnifyingGlassIcon className="w-5 h-5 text-gray-400" aria-hidden="true" />
  80. </div>
  81. <input
  82. type="search"
  83. id="search"
  84. className='block w-full h-10 px-3 pl-10 text-sm font-normal bg-gray-100 rounded-lg'
  85. placeholder="Search emojis..."
  86. onChange={async (e: ChangeEvent<HTMLInputElement>) => {
  87. if (e.target.value === '') {
  88. setIsSearching(false)
  89. }
  90. else {
  91. setIsSearching(true)
  92. const emojis = await search(e.target.value)
  93. setSearchedEmojis(emojis)
  94. }
  95. }}
  96. />
  97. </div>
  98. </div>
  99. <Divider className='m-0 mb-3' />
  100. <div className="w-full max-h-[200px] overflow-x-hidden overflow-y-auto px-3">
  101. {isSearching && <>
  102. <div key={'category-search'} className='flex flex-col'>
  103. <p className='font-medium uppercase text-xs text-[#101828] mb-1'>Search</p>
  104. <div className='w-full h-full grid grid-cols-8 gap-1'>
  105. {searchedEmojis.map((emoji: string, index: number) => {
  106. return <div
  107. key={`emoji-search-${index}`}
  108. className='inline-flex w-10 h-10 rounded-lg items-center justify-center'
  109. onClick={() => {
  110. setSelectedEmoji(emoji)
  111. }}
  112. >
  113. <div className='cursor-pointer w-8 h-8 p-1 flex items-center justify-center rounded-lg hover:ring-1 ring-offset-1 ring-gray-300'>
  114. <em-emoji id={emoji} />
  115. </div>
  116. </div>
  117. })}
  118. </div>
  119. </div>
  120. </>}
  121. {categories.map((category: any, index: number) => {
  122. return <div key={`category-${index}`} className='flex flex-col'>
  123. <p className='font-medium uppercase text-xs text-[#101828] mb-1'>{category.id}</p>
  124. <div className='w-full h-full grid grid-cols-8 gap-1'>
  125. {category.emojis.map((emoji: string, index: number) => {
  126. return <div
  127. key={`emoji-${index}`}
  128. className='inline-flex w-10 h-10 rounded-lg items-center justify-center'
  129. onClick={() => {
  130. setSelectedEmoji(emoji)
  131. }}
  132. >
  133. <div className='cursor-pointer w-8 h-8 p-1 flex items-center justify-center rounded-lg hover:ring-1 ring-offset-1 ring-gray-300'>
  134. <em-emoji id={emoji} />
  135. </div>
  136. </div>
  137. })}
  138. </div>
  139. </div>
  140. })}
  141. </div>
  142. {/* Color Select */}
  143. <div className={cn('p-3 ', selectedEmoji === '' ? 'opacity-25' : '')}>
  144. <p className='font-medium uppercase text-xs text-[#101828] mb-2'>Choose Style</p>
  145. <div className='w-full h-full grid grid-cols-8 gap-1'>
  146. {backgroundColors.map((color) => {
  147. return <div
  148. key={color}
  149. className={
  150. cn(
  151. 'cursor-pointer',
  152. 'hover:ring-1 ring-offset-1',
  153. 'inline-flex w-10 h-10 rounded-lg items-center justify-center',
  154. color === selectedBackground ? 'ring-1 ring-gray-300' : '',
  155. )}
  156. onClick={() => {
  157. setSelectedBackground(color)
  158. }}
  159. >
  160. <div className={cn(
  161. 'w-8 h-8 p-1 flex items-center justify-center rounded-lg',
  162. )
  163. } style={{ background: color }}>
  164. {selectedEmoji !== '' && <em-emoji id={selectedEmoji} />}
  165. </div>
  166. </div>
  167. })}
  168. </div>
  169. </div>
  170. <Divider className='m-0' />
  171. <div className='w-full flex items-center justify-center p-3 gap-2'>
  172. <Button type="default" className='w-full' onClick={() => {
  173. onClose && onClose()
  174. }}>
  175. {t('app.emoji.cancel')}
  176. </Button>
  177. <Button
  178. disabled={selectedEmoji === ''}
  179. type="primary"
  180. className='w-full'
  181. onClick={() => {
  182. onSelect && onSelect(selectedEmoji, selectedBackground)
  183. }}>
  184. {t('app.emoji.ok')}
  185. </Button>
  186. </div>
  187. </Modal> : <>
  188. </>
  189. }
  190. export default EmojiPicker