You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

input-copy.tsx 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import copy from 'copy-to-clipboard'
  4. import { t } from 'i18next'
  5. import Tooltip from '@/app/components/base/tooltip'
  6. import CopyFeedback from '@/app/components/base/copy-feedback'
  7. type IInputCopyProps = {
  8. value?: string
  9. className?: string
  10. children?: React.ReactNode
  11. }
  12. const InputCopy = ({
  13. value = '',
  14. className,
  15. children,
  16. }: IInputCopyProps) => {
  17. const [isCopied, setIsCopied] = useState(false)
  18. useEffect(() => {
  19. if (isCopied) {
  20. const timeout = setTimeout(() => {
  21. setIsCopied(false)
  22. }, 1000)
  23. return () => {
  24. clearTimeout(timeout)
  25. }
  26. }
  27. }, [isCopied])
  28. return (
  29. <div className={`flex items-center rounded-lg bg-components-input-bg-normal py-2 hover:bg-state-base-hover ${className}`}>
  30. <div className="flex h-5 grow items-center">
  31. {children}
  32. <div className='relative h-full grow text-[13px]'>
  33. <div className='r-0 absolute left-0 top-0 w-full cursor-pointer truncate pl-2 pr-2' onClick={() => {
  34. copy(value)
  35. setIsCopied(true)
  36. }}>
  37. <Tooltip
  38. popupContent={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
  39. position='bottom'
  40. >
  41. <span className='text-text-secondary'>{value}</span>
  42. </Tooltip>
  43. </div>
  44. </div>
  45. <div className="h-4 w-px shrink-0 bg-divider-regular" />
  46. <div className='mx-1'><CopyFeedback content={value} /></div>
  47. </div>
  48. </div>
  49. )
  50. }
  51. export default InputCopy