Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support copy feature #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/demo/allow-copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: allow-copy
nav:
title: Demo
path: /demo
---

<code src="../examples/allow-copy.tsx"></code>
15 changes: 15 additions & 0 deletions docs/examples/allow-copy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Input from 'rc-input';
import type { FC } from 'react';
import React from 'react';
import '../../assets/index.less';

const Demo: FC = () => (
<>
<Input prefixCls="rc-input" style={{ marginLeft: 200 }} allowCopy />
<br />
<br />
<Input prefixCls="rc-input" addonAfter="MB" allowCopy />
</>
);

export default Demo;
52 changes: 50 additions & 2 deletions src/BaseInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import clsx from 'classnames';
import type { FC, ReactElement } from 'react';
import React, { cloneElement, useRef } from 'react';
import DefaultCopyIcon from './DefaultCopyIcon';
import type { BaseInputProps } from './interface';
import { hasAddon, hasPrefixSuffix } from './utils/commonUtils';

const copyText = (text?: string): void => {
if (!text) {
return;
}
const textarea = document.createElement('textarea');
textarea.setAttribute('readonly', 'readonly');
textarea.innerHTML = text;
document.body.appendChild(textarea);
textarea.select();
if (document.execCommand('copy')) {
document.execCommand('copy');
}
document.body.removeChild(textarea);
};

const BaseInput: FC<BaseInputProps> = (props) => {
const {
inputElement,
Expand All @@ -26,6 +42,8 @@ const BaseInput: FC<BaseInputProps> = (props) => {
classNames,
dataAttrs,
styles,
allowCopy,
copyCallback,
} = props;

const containerRef = useRef<HTMLSpanElement>(null);
Expand Down Expand Up @@ -76,7 +94,9 @@ const BaseInput: FC<BaseInputProps> = (props) => {
) || null,
style: {
...inputElement.props?.style,
...(!hasPrefixSuffix(props) && !hasAddon(props) ? style : {}),
...(!hasPrefixSuffix(props) && !hasAddon(props) && !allowCopy
? style
: {}),
},
});

Expand Down Expand Up @@ -132,6 +152,20 @@ const BaseInput: FC<BaseInputProps> = (props) => {
);
}

const CopyIcon = React.createElement(
'span',
{
onClick: () => {
if (typeof copyCallback === 'function') {
copyCallback(value, inputElement);
} else {
copyText(value?.toString());
}
},
},
DefaultCopyIcon,
);

// ================== Addon ================== //
if (hasAddon(props)) {
const wrapperCls = `${prefixCls}-group`;
Expand Down Expand Up @@ -159,11 +193,25 @@ const BaseInput: FC<BaseInputProps> = (props) => {
hidden: null,
})}
{addonAfter && <span className={addonCls}>{addonAfter}</span>}
{allowCopy ? CopyIcon : null}
</span>
</span>
);
}
return element;
return (
<>
{CopyIcon ? (
<div
style={{ display: 'flex', alignItems: 'center', ...(style || {}) }}
>
{element}
{allowCopy ? CopyIcon : null}
</div>
) : (
element
)}
</>
);
};

export default BaseInput;
22 changes: 22 additions & 0 deletions src/DefaultCopyIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { ReactNode } from 'react';
import React from 'react';

const DefaultCopyIcon: ReactNode = (
<span
style={{ margin: '8px', color: 'rgba(0, 0, 0, 0.65)', cursor: 'pointer' }}
>
<svg
viewBox="64 64 896 896"
width="16"
height="16"
focusable="false"
data-icon="copy"
fill="currentColor"
aria-hidden="true"
>
<path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z" />
</svg>
</span>
);

export default DefaultCopyIcon;
5 changes: 5 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import type { InputFocusOptions } from './utils/commonUtils';
import type { LiteralUnion } from './utils/types';

export interface CommonInputProps {
allowCopy?: ReactNode | boolean;
copyCallback?: (
v: InputHTMLAttributes<HTMLInputElement>['value'],
ele: ReactElement<any, string | React.JSXElementConstructor<any>>,
) => void;
prefix?: ReactNode;
suffix?: ReactNode;
addonBefore?: ReactNode;
Expand Down