71 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-05-15 08:51:32 +08:00
'use client'
2024-08-26 13:00:02 +08:00
import React, { useEffect, useState } from 'react'
2023-05-24 16:11:25 +08:00
import copy from 'copy-to-clipboard'
2023-05-15 08:51:32 +08:00
import { t } from 'i18next'
import s from './style.module.css'
import Tooltip from '@/app/components/base/tooltip'
2023-05-15 08:51:32 +08:00
type IInputCopyProps = {
value?: string
className?: string
readOnly?: boolean
children?: React.ReactNode
}
const InputCopy = ({
value = '',
2023-05-15 08:51:32 +08:00
className,
readOnly = true,
children,
}: IInputCopyProps) => {
const [isCopied, setIsCopied] = useState(false)
useEffect(() => {
if (isCopied) {
const timeout = setTimeout(() => {
setIsCopied(false)
}, 1000)
return () => {
clearTimeout(timeout)
}
}
}, [isCopied])
return (
<div className={`flex items-center rounded-lg bg-components-input-bg-normal py-2 hover:bg-state-base-hover ${className}`}>
<div className="flex h-5 grow items-center">
2023-05-15 08:51:32 +08:00
{children}
<div className='relative h-full grow text-[13px]'>
<div className='r-0 absolute left-0 top-0 w-full cursor-pointer truncate pl-2 pr-2' onClick={() => {
2024-08-26 13:00:02 +08:00
copy(value)
setIsCopied(true)
}}>
<Tooltip
popupContent={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
position='bottom'
>
{value}
</Tooltip>
</div>
2023-05-15 08:51:32 +08:00
</div>
<div className="h-4 shrink-0 border bg-divider-regular" />
2023-05-15 08:51:32 +08:00
<Tooltip
2024-08-26 13:00:02 +08:00
popupContent={isCopied ? `${t('appApi.copied')}` : `${t('appApi.copy')}`}
position='bottom'
2023-05-15 08:51:32 +08:00
>
<div className="shrink-0 px-0.5">
<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={() => {
2023-05-15 08:51:32 +08:00
copy(value)
setIsCopied(true)
}}>
</div>
</div>
</Tooltip>
</div>
</div>
)
}
export default InputCopy