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 items-center rounded-lg bg-components-input-bg-normal py-2 hover:bg-state-base-hover ${className}`}>
  32. <div className="flex h-5 grow items-center">
  33. {children}
  34. <div className='relative h-full grow text-[13px]'>
  35. <div className='r-0 absolute left-0 top-0 w-full cursor-pointer truncate pl-2 pr-2' 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="h-4 shrink-0 border bg-divider-regular" />
  48. <Tooltip
  49. popupContent={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  50. position='bottom'
  51. >
  52. <div className="shrink-0 px-0.5">
  53. <div className={`box-border flex h-[30px] w-[30px] cursor-pointer items-center justify-center rounded-lg hover:bg-state-base-hover ${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