gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
is there a component that contains text and a copy to clipboard button in antDesign?
- User
is there a component that contains text and a copy to clipboard button in antDesign?
- Assistant
Yes, Ant Design has a component called
<Input/>which can contain text, and it also has a feature to add a copy to clipboard button with the help of aaddonAfterproperty. Here's an example:import { Input, message } from 'antd'; import { useState } from 'react'; import { CopyOutlined } from '@ant-design/icons'; const { TextArea } = Input; function CopyToClipboard() { const [value, setValue] = useState(''); const copyToClipboard = () => { navigator.clipboard.writeText(value); message.success('Copied to clipboard!'); } return ( <TextArea value={value} onChange={(e) => setValue(e.target.value)} placeholder="Type some text to copy..." addonAfter={ <span onClick={copyToClipboard}> <CopyOutlined /> </span> } /> ); } export default CopyToClipboard;This will render an input-area with a "Copy" icon/button at the end. When the user clicks on the button, the value of the input area is copied to the clipboard. Note that
navigator.clipboard.writeText(value)will only work in modern browsers.