2022-08-25 19:21:03 -07:00
|
|
|
import React, { useRef } from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
|
|
|
|
import { Button, HStack, Input } from './ui';
|
|
|
|
|
|
|
|
interface ICopyableInput {
|
|
|
|
/** Text to be copied. */
|
2023-02-15 13:26:27 -08:00
|
|
|
value: string
|
2022-08-25 19:21:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** An input with copy abilities. */
|
|
|
|
const CopyableInput: React.FC<ICopyableInput> = ({ value }) => {
|
|
|
|
const input = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
const selectInput = () => {
|
|
|
|
input.current?.select();
|
2022-08-25 19:28:20 -07:00
|
|
|
|
|
|
|
if (navigator.clipboard) {
|
|
|
|
navigator.clipboard.writeText(value);
|
|
|
|
} else {
|
|
|
|
document.execCommand('copy');
|
|
|
|
}
|
2022-08-25 19:21:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<HStack alignItems='center'>
|
|
|
|
<Input
|
|
|
|
ref={input}
|
|
|
|
type='text'
|
|
|
|
value={value}
|
2022-11-25 09:04:11 -08:00
|
|
|
className='rounded-r-none rtl:rounded-l-none rtl:rounded-r-lg'
|
2023-02-06 11:28:18 -08:00
|
|
|
outerClassName='grow'
|
2022-08-25 19:21:03 -07:00
|
|
|
onClick={selectInput}
|
|
|
|
readOnly
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
theme='primary'
|
2022-11-25 09:04:11 -08:00
|
|
|
className='mt-1 h-full rounded-l-none rounded-r-lg rtl:rounded-l-lg rtl:rounded-r-none'
|
2022-08-25 19:21:03 -07:00
|
|
|
onClick={selectInput}
|
|
|
|
>
|
|
|
|
<FormattedMessage id='input.copy' defaultMessage='Copy' />
|
|
|
|
</Button>
|
|
|
|
</HStack>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CopyableInput;
|