Skip to content

Commit 28a78f1

Browse files
authored
fix: Paste logic (#517)
1 parent 502a7ec commit 28a78f1

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/Selector/index.tsx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import * as React from 'react';
12+
import { useRef } from 'react';
1213
import KeyCode from 'rc-util/lib/KeyCode';
1314
import MultipleSelector from './MultipleSelector';
1415
import SingleSelector from './SingleSelector';
@@ -72,6 +73,9 @@ export interface SelectorProps {
7273
maxTagPlaceholder?: React.ReactNode | ((omittedValues: LabelValueType[]) => React.ReactNode);
7374
tagRender?: (props: CustomTagProps) => React.ReactElement;
7475

76+
/** Check if `tokenSeparators` contains `\n` */
77+
tokenWithEnter?: boolean;
78+
7579
// Motion
7680
choiceTransitionName?: string;
7781

@@ -90,15 +94,16 @@ export interface SelectorProps {
9094
}
9195

9296
const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> = (props, ref) => {
93-
const inputRef = React.useRef<HTMLInputElement>(null);
94-
const compositionStatusRef = React.useRef<boolean>(false);
97+
const inputRef = useRef<HTMLInputElement>(null);
98+
const compositionStatusRef = useRef<boolean>(false);
9599

96100
const {
97101
prefixCls,
98102
multiple,
99103
open,
100104
mode,
101105
showSearch,
106+
tokenWithEnter,
102107

103108
onSearch,
104109
onSearchSubmit,
@@ -152,7 +157,7 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
152157
};
153158

154159
// When paste come, ignore next onChange
155-
const pasteClearRef = React.useRef(false);
160+
const pastedTextRef = useRef<string>(null);
156161

157162
const triggerOnSearch = (value: string) => {
158163
if (onSearch(value, true, compositionStatusRef.current) !== false) {
@@ -168,30 +173,27 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
168173
compositionStatusRef.current = false;
169174
};
170175

171-
const onInputChange = ({ target: { value } }) => {
172-
if (pasteClearRef.current) {
173-
pasteClearRef.current = false;
174-
return;
176+
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = event => {
177+
let {
178+
target: { value },
179+
} = event;
180+
181+
// Pasted text should replace back to origin content
182+
if (tokenWithEnter && pastedTextRef.current && /[\r\n]/.test(pastedTextRef.current)) {
183+
const replacedText = pastedTextRef.current.replace(/[\r\n]/g, ' ');
184+
value = value.replace(replacedText, pastedTextRef.current);
175185
}
176186

187+
pastedTextRef.current = null;
188+
177189
triggerOnSearch(value);
178190
};
179191

180192
const onInputPaste: React.ClipboardEventHandler = e => {
181-
// github.com/ant-design/ant-design/issues/24461
182-
if ((e.target as HTMLInputElement).value) {
183-
return;
184-
}
185193
const { clipboardData } = e;
186194
const value = clipboardData.getData('text');
187195

188-
// Block next onChange
189-
pasteClearRef.current = true;
190-
setTimeout(() => {
191-
pasteClearRef.current = false;
192-
});
193-
194-
triggerOnSearch(value);
196+
pastedTextRef.current = value;
195197
};
196198

197199
// ====================== Focus ======================

src/generate.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ export default function generateSelector<
322322
const selectorRef = useRef<RefSelectorProps>(null);
323323
const listRef = useRef<RefOptionListProps>(null);
324324

325+
const tokenWithEnter = useMemo(() => (tokenSeparators || []).includes('\n'), [tokenSeparators]);
326+
325327
/** Used for component focused management */
326328
const [mockFocused, setMockFocused, cancelSetMockFocused] = useDelayReset();
327329

@@ -1028,6 +1030,7 @@ export default function generateSelector<
10281030
onSearch={triggerSearch}
10291031
onSearchSubmit={onSearchSubmit}
10301032
onSelect={onInternalSelectionSelect}
1033+
tokenWithEnter={tokenWithEnter}
10311034
/>
10321035
</SelectTrigger>
10331036

0 commit comments

Comments
 (0)