image.png
    input 有 select() 方法,用于选中文字
    JS 提供了用于复制的API:document.execCommand("copy")
    结合起来就能实现点击按钮复制文本了

    1. const onlineUrlRef = useRef() as MutableRefObject<HTMLInputElement>
    2. const handleCopy = (ref) => {
    3. ref.select()
    4. document.execCommand('copy')
    5. message.success('复制成功')
    6. }
    1. <input
    2. type="text"
    3. className="filename"
    4. value={ImageStore.serverFile.attributes.url.attributes.url}
    5. ref={onlineUrlRef}
    6. readOnly
    7. />
    8. <Button
    9. type="default"
    10. size="small"
    11. onClick={() => handleCopy(onlineUrlRef.current)}
    12. >
    13. 复制
    14. </Button>