Skip to content

Commit

Permalink
Merge pull request #112 from MacPaw/feat/add-on-copy-event-for-input
Browse files Browse the repository at this point in the history
add onCopyEvent for input
  • Loading branch information
hraboviyvadim authored May 24, 2023
2 parents 8dac5ac + 3e020f7 commit 5d27a4b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@macpaw/macpaw-ui",
"version": "4.9.1",
"version": "4.10.0",
"main": "lib/ui.js",
"scripts": {
"dev": "next -p 1234",
Expand Down
8 changes: 6 additions & 2 deletions pages/input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,19 @@ const [value, setValue] = useState(10);
<div style={{ width: 800 }}>
<FormRow split>
<Input type="text" name="key" placeholder="Your key" label="Key" clipboard />
<Input type="text" name="link" name="Your link" placeholder="Your link" label="Key" clipboard="copy" />
<Input type="text" name="link" name="Your link" placeholder="Your link" label="Key" clipboard="copy" onCopyEvent={(value) => {
console.log(`Copied with value: ${value}`);
}} />
</FormRow>
</div>

```
<div style={{ width: 800 }}>
<FormRow split>
<Input type="text" name="key" placeholder="Your key" label="Key" clipboard />
<Input type="text" name="link" name="Your link" placeholder="Your link" label="Key" clipboard="copy" />
<Input type="text" name="link" name="Your link" placeholder="Your link" label="Key" clipboard="copy" onCopyEvent={(value) => {
console.log(`Copied with value: ${value}`);
}} />
</FormRow>
</div>
```
5 changes: 4 additions & 1 deletion src/Clipboard/Clipboard.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { MutableRefObject, useEffect, useRef, useState } from 'react';
import { CheckCircleIcon, CopyIcon as CopyIconUi } from '../Icons/jsx';
import Tooltip from '../Tooltip/Tooltip';
import { InputValueType } from '../types';

interface ClipboardProps {
copy?: React.ReactElement | string;
element: MutableRefObject<HTMLInputElement | undefined>;
onCopyEvent?: (value: InputValueType) => void;
}

const Clipboard: React.FC<ClipboardProps> = ({ copy, element }) => {
const Clipboard: React.FC<ClipboardProps> = ({ copy, element, onCopyEvent }) => {
const [canBeCopied, setCanBeCopied] = useState(true);
const timerRef = useRef<NodeJS.Timeout>();

Expand All @@ -19,6 +21,7 @@ const Clipboard: React.FC<ClipboardProps> = ({ copy, element }) => {

const iconHandler = () => {
if (element.current?.value) {
onCopyEvent?.(element.current?.value);
navigator.clipboard.writeText(element.current?.value);
setCanBeCopied(false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface InputProps extends Omit<InputHTMLAttributes<HTMLInputElement>,
formatOnEvent?: 'blur' | 'input';
format?: (value: InputValueType) => InputValueType;
onChange?: (value:InputValueType, event?: React.ChangeEvent<HTMLInputElement>) => void;
onCopyEvent?: (value: InputValueType) => void;
onAutofill?: () => void;
clipboard?: boolean | string | React.ReactElement;
}
Expand All @@ -46,6 +47,7 @@ const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
icon,
value,
onChange,
onCopyEvent,
formatOnEvent = '',
format,
onAutofill,
Expand Down Expand Up @@ -141,7 +143,7 @@ const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
/>
{action && <span className="input-action">{action}</span>}
{currency && <span className="input-currency">{currency}</span>}
{clipboard && <Clipboard element={inputRef} copy={typeof clipboard !== 'boolean' ? clipboard : undefined} />}
{clipboard && <Clipboard element={inputRef} copy={typeof clipboard !== 'boolean' ? clipboard : undefined} onCopyEvent={onCopyEvent} />}
</span>
{showHintError && <Hint error style={{ marginTop: 6 }}>{error}</Hint>}
</label>
Expand Down

0 comments on commit 5d27a4b

Please sign in to comment.