Skip to content
Merged
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
38 changes: 20 additions & 18 deletions src/Selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

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

/** Check if `tokenSeparators` contains `\n` */
tokenWithEnter?: boolean;

// Motion
choiceTransitionName?: string;

Expand All @@ -90,15 +94,16 @@ export interface SelectorProps {
}

const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> = (props, ref) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const compositionStatusRef = React.useRef<boolean>(false);
const inputRef = useRef<HTMLInputElement>(null);
const compositionStatusRef = useRef<boolean>(false);

const {
prefixCls,
multiple,
open,
mode,
showSearch,
tokenWithEnter,

onSearch,
onSearchSubmit,
Expand Down Expand Up @@ -152,7 +157,7 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
};

// When paste come, ignore next onChange
const pasteClearRef = React.useRef(false);
const pastedTextRef = useRef<string>(null);

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

const onInputChange = ({ target: { value } }) => {
if (pasteClearRef.current) {
pasteClearRef.current = false;
return;
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = event => {
let {
target: { value },
} = event;

// Pasted text should replace back to origin content
if (tokenWithEnter && pastedTextRef.current && /[\r\n]/.test(pastedTextRef.current)) {
const replacedText = pastedTextRef.current.replace(/[\r\n]/g, ' ');
value = value.replace(replacedText, pastedTextRef.current);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

黑科技越来越多~

}

pastedTextRef.current = null;

triggerOnSearch(value);
};

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

// Block next onChange
pasteClearRef.current = true;
setTimeout(() => {
pasteClearRef.current = false;
});

triggerOnSearch(value);
pastedTextRef.current = value;
};

// ====================== Focus ======================
Expand Down
3 changes: 3 additions & 0 deletions src/generate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ export default function generateSelector<
const selectorRef = useRef<RefSelectorProps>(null);
const listRef = useRef<RefOptionListProps>(null);

const tokenWithEnter = useMemo(() => (tokenSeparators || []).includes('\n'), [tokenSeparators]);

/** Used for component focused management */
const [mockFocused, setMockFocused, cancelSetMockFocused] = useDelayReset();

Expand Down Expand Up @@ -1028,6 +1030,7 @@ export default function generateSelector<
onSearch={triggerSearch}
onSearchSubmit={onSearchSubmit}
onSelect={onInternalSelectionSelect}
tokenWithEnter={tokenWithEnter}
/>
</SelectTrigger>

Expand Down