input-copy.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use client'
  2. import React, { useEffect, useRef, useState } from 'react'
  3. import copy from 'copy-to-clipboard'
  4. import { t } from 'i18next'
  5. import s from './style.module.css'
  6. import { randomString } from '@/utils'
  7. import Tooltip from '@/app/components/base/tooltip'
  8. type IInputCopyProps = {
  9. value?: string
  10. className?: string
  11. readOnly?: boolean
  12. children?: React.ReactNode
  13. }
  14. const InputCopy = ({
  15. value = '',
  16. className,
  17. readOnly = true,
  18. children,
  19. }: IInputCopyProps) => {
  20. const [isCopied, setIsCopied] = useState(false)
  21. const selector = useRef(`input-tooltip-${randomString(4)}`)
  22. useEffect(() => {
  23. if (isCopied) {
  24. const timeout = setTimeout(() => {
  25. setIsCopied(false)
  26. }, 1000)
  27. return () => {
  28. clearTimeout(timeout)
  29. }
  30. }
  31. }, [isCopied])
  32. return (
  33. <div className={`flex rounded-lg bg-gray-50 hover:bg-gray-50 py-2 items-center ${className}`}>
  34. <div className="flex items-center flex-grow h-5">
  35. {children}
  36. <div className='flex-grow bg-gray-50 text-[13px] relative h-full'>
  37. <Tooltip
  38. selector={selector.current}
  39. content={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  40. className='z-10'
  41. >
  42. <div className='absolute top-0 left-0 w-full pl-2 pr-2 truncate cursor-pointer r-0' onClick={() => {
  43. copy(value)
  44. setIsCopied(true)
  45. }}>{value}</div>
  46. </Tooltip>
  47. </div>
  48. <div className="flex-shrink-0 h-4 bg-gray-200 border" />
  49. <Tooltip
  50. selector={selector.current}
  51. content={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  52. className='z-10'
  53. >
  54. <div className="px-0.5 flex-shrink-0">
  55. <div className={`box-border w-[30px] h-[30px] flex items-center justify-center rounded-lg hover:bg-gray-100 cursor-pointer ${s.copyIcon} ${isCopied ? s.copied : ''}`} onClick={() => {
  56. copy(value)
  57. setIsCopied(true)
  58. }}>
  59. </div>
  60. </div>
  61. </Tooltip>
  62. </div>
  63. </div>
  64. )
  65. }
  66. export default InputCopy