index.tsx 6.1 KB

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