使用 Chakra UI 的 useClipboard 钩子

huangapple go评论59阅读模式
英文:

Using Chakra UI's useClipboard hook

问题

I have multiple input fields populated with data gotten from redux store. I need help to implement a copy to clipboard function for each input field. Tried doing this but no luck:

import { useClipboard } from "@chakra-ui/react";

const { sandboxKey, token } = useSelector((state) => state.apikeys);

const { onCopy, value, setValue, hasCopied } = useClipboard("");

return (
  <InputGroup>
    <Input 
      value={token}
      onChange={(e) => {
        setValue(e.target.value);
      }}
    />
    <InputRightElement>
      <Button onClick={onCopy}>
        {hasCopied ? "Copied!" : "Copy"}
      </Button>
    </InputRightElement>
  </InputGroup>

  <InputGroup>
    <Input 
      value={prodkey.prodKey}
      onChange={(e) => {
        setValue(e.target.value);
      }}
    />
    <InputRightElement>
      <Button onClick={onCopy}>
        {hasCopied ? "Copied!" : "Copy"}
      </Button>
    </InputRightElement>
  </InputGroup>
)

Note: I've provided the translated JavaScript code as requested.

英文:

I have multiple input fields populated with data gotten from redux store. I need help to implement a copy to clipboard function for each input field. Tried doing this but no luck:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { useClipboard } from &quot;@chakra-ui/react&quot;;

const { sandboxKey, token } = useSelector((state) =&gt; state.apikeys);
  
const { onCopy, value, setValue, hasCopied } = useClipboard(&quot;&quot;);

return (
 &lt;InputGroup&gt;
   &lt;Input 
     value={token}
     onChange={(e) =&gt; {
     setValue(e.target.value);
     }}
     /&gt;
   &lt;InputRightElement&gt;
     &lt;Button onClick={onCopy}&gt;
      {hasCopied ? &quot;Copied!&quot; : &quot;Copy&quot;}
     &lt;/Button&gt;
   &lt;/InputRightElement&gt;
 &lt;/InputGroup&gt;
 
 &lt;InputGroup&gt;
   &lt;Input 
     value={prodkey.prodKey}
     onChange={(e) =&gt; {
     setValue(e.target.value);
     }}
     /&gt;
   &lt;InputRightElement&gt;
     &lt;Button onClick={onCopy}&gt;
      {hasCopied ? &quot;Copied!&quot; : &quot;Copy&quot;}
     &lt;/Button&gt;
   &lt;/InputRightElement&gt;
 &lt;/InputGroup&gt;
)

<!-- end snippet -->

答案1

得分: 0

你应该调用useClipboard钩子两次,分别从两个不同的输入中复制。此外,你应该将钩子返回的值传递给Input的value,而不是始终传递相同的值。

const tokenCopy = useClipboard(token);
const prodKeyCopy = useClipboard(prodkey.prodKey);
return (
	<>
		<InputGroup>
			<Input 
			 value={tokenCopy.value}
			 onChange={e => tokenCopy.setValue(e.target.value)}
			 />
			<InputRightElement>
			 <Button onClick={tokenCopy.onCopy}>
			  {tokenCopy.hasCopied ? "已复制!" : "复制"}
			 </Button>
			</InputRightElement>
		</InputGroup>
		<InputGroup>
			<Input 
			 value={prodKeyCopy.value}
			 onChange={e => prodKeyCopy.setValue(e.target.value)}
			 />
			<InputRightElement>
			 <Button onClick={prodKeyCopy.onCopy}>
			  {prodKeyCopy.hasCopied ? "已复制!" : "复制"}
			 </Button>
			</InputRightElement>
		</InputGroup>
	</>
);
英文:

You should call the useClipboard hook twice for copying from two distinct inputs. In addition, you should pass the value returned by the hook to the Input's value instead of always passing the same value.

const tokenCopy = useClipboard(token);
const prodKeyCopy = useClipboard(prodkey.prodKey);
return (
	&lt;&gt;
		&lt;InputGroup&gt;
			&lt;Input 
			 value={tokenCopy.value}
			 onChange={e =&gt; tokenCopy.setValue(e.target.value)}
			 /&gt;
			&lt;InputRightElement&gt;
			 &lt;Button onClick={tokenCopy.onCopy}&gt;
			  {tokenCopy.hasCopied ? &quot;Copied!&quot; : &quot;Copy&quot;}
			 &lt;/Button&gt;
			&lt;/InputRightElement&gt;
		&lt;/InputGroup&gt;
		&lt;InputGroup&gt;
			&lt;Input 
			 value={prodKeyCopy.value}
			 onChange={e =&gt; prodKeyCopy.setValue(e.target.value)}
			 /&gt;
			&lt;InputRightElement&gt;
			 &lt;Button onClick={prodKeyCopy.onCopy}&gt;
			  {prodKeyCopy.hasCopied ? &quot;Copied!&quot; : &quot;Copy&quot;}
			 &lt;/Button&gt;
			&lt;/InputRightElement&gt;
		&lt;/InputGroup&gt;
	&lt;/&gt;
);

</details>



huangapple
  • 本文由 发表于 2023年5月17日 06:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定