input-copy.tsx 2.0 KB

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