Skip to content

Commit de31413

Browse files
committed
chore: update
1 parent 04db275 commit de31413

File tree

6 files changed

+434
-526
lines changed

6 files changed

+434
-526
lines changed

src/BaseSelect/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
462462
onSearchSplit?.(patchLabels);
463463

464464
// Should close when paste finish
465-
// onToggleOpen(false);
466465
triggerOpen(false);
467466

468467
// Tell Selector that break next actions
@@ -476,7 +475,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
476475
}
477476

478477
// Open if from typing
479-
if (searchText && fromTyping) {
478+
if (searchText && fromTyping && ret) {
480479
triggerOpen(true);
481480
}
482481

@@ -941,6 +940,8 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
941940
onKeyDown={onInternalKeyDown}
942941
onKeyUp={onInternalKeyUp}
943942
onSelectorRemove={onSelectorRemove}
943+
// Token handling
944+
tokenWithEnter={tokenWithEnter}
944945
// Open
945946
onMouseDown={onInternalMouseDown}
946947
/>

src/SelectInput/Input.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
3434
autoComplete,
3535
...restProps
3636
} = props;
37-
const { prefixCls, mode, onSearch, onSearchSubmit, onInputBlur, autoFocus } =
37+
const { prefixCls, mode, onSearch, onSearchSubmit, onInputBlur, autoFocus, tokenWithEnter } =
3838
useSelectInputContext();
3939
const { id, classNames, styles, open, activeDescendantId, role, disabled } = useBaseProps() || {};
4040

@@ -43,6 +43,9 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
4343
// Used to handle input method composition status
4444
const compositionStatusRef = React.useRef<boolean>(false);
4545

46+
// Used to handle paste content, similar to original Selector implementation
47+
const pastedTextRef = React.useRef<string | null>(null);
48+
4649
// ============================== Refs ==============================
4750
const inputRef = React.useRef<HTMLInputElement>(null);
4851

@@ -51,7 +54,20 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
5154
// ============================== Data ==============================
5255
// Handle input changes
5356
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (event) => {
54-
const { value: nextVal } = event.target;
57+
let { value: nextVal } = event.target;
58+
59+
// Handle pasted text with tokenWithEnter, similar to original Selector implementation
60+
if (tokenWithEnter && pastedTextRef.current && /[\r\n]/.test(pastedTextRef.current)) {
61+
// CRLF will be treated as a single space for input element
62+
const replacedText = pastedTextRef.current
63+
.replace(/[\r\n]+$/, '')
64+
.replace(/\r\n/g, ' ')
65+
.replace(/[\r\n]/g, ' ');
66+
nextVal = nextVal.replace(replacedText, pastedTextRef.current);
67+
}
68+
69+
// Reset pasted text reference
70+
pastedTextRef.current = null;
5571

5672
// Call onSearch callback
5773
if (onSearch) {
@@ -95,9 +111,18 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
95111
const handleCompositionEnd: React.CompositionEventHandler<HTMLInputElement> = (event) => {
96112
compositionStatusRef.current = false;
97113

98-
// Trigger search when input method composition ends
99-
const { value: nextVal } = event.currentTarget;
100-
onSearch?.(nextVal, true, false);
114+
// Trigger search when input method composition ends, similar to original Selector
115+
if (mode !== 'combobox') {
116+
const { value: nextVal } = event.currentTarget;
117+
onSearch?.(nextVal, true, false);
118+
}
119+
};
120+
121+
// Handle paste events to track pasted content
122+
const handlePaste: React.ClipboardEventHandler<HTMLInputElement> = (event) => {
123+
const { clipboardData } = event;
124+
const pastedValue = clipboardData?.getData('text');
125+
pastedTextRef.current = pastedValue || '';
101126
};
102127

103128
// ============================= Width ==============================
@@ -139,6 +164,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
139164
onChange={handleChange}
140165
onKeyDown={handleKeyDown}
141166
onBlur={handleBlur}
167+
onPaste={handlePaste}
142168
onCompositionStart={handleCompositionStart}
143169
onCompositionEnd={handleCompositionEnd}
144170
// Accessibility attributes

src/SelectInput/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface SelectInputProps extends Omit<React.HTMLAttributes<HTMLDivEleme
2929
onSelectorRemove?: (value: DisplayValueType) => void;
3030
maxLength?: number;
3131
autoFocus?: boolean;
32+
/** Check if `tokenSeparators` contains `\n` or `\r\n` */
33+
tokenWithEnter?: boolean;
3234
// Add other props that need to be passed through
3335
className?: string;
3436
style?: React.CSSProperties;
@@ -96,6 +98,9 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
9698
onInputKeyDown,
9799
onSelectorRemove,
98100

101+
// Token handling
102+
tokenWithEnter,
103+
99104
...restProps
100105
} = props;
101106

tests/Tags.test.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createEvent, fireEvent, render } from '@testing-library/react';
1+
import { act, createEvent, fireEvent, render } from '@testing-library/react';
22
import KeyCode from '@rc-component/util/lib/KeyCode';
33
import { clsx } from 'clsx';
44
import * as React from 'react';
@@ -62,11 +62,10 @@ describe('Select.Tags', () => {
6262
it('tokenize input', () => {
6363
const handleChange = jest.fn();
6464
const handleSelect = jest.fn();
65-
const option2 = <Option value="2">2</Option>;
6665
const { container } = render(
6766
<Select mode="tags" tokenSeparators={[',']} onChange={handleChange} onSelect={handleSelect}>
6867
<Option value="1">1</Option>
69-
{option2}
68+
<Option value="2">2</Option>
7069
</Select>,
7170
);
7271

@@ -79,6 +78,7 @@ describe('Select.Tags', () => {
7978
expect(findSelection(container, 1).textContent).toEqual('3');
8079
expect(findSelection(container, 2).textContent).toEqual('4');
8180
expect(container.querySelector('input').value).toBe('');
81+
8282
expectOpen(container, false);
8383
});
8484

@@ -513,9 +513,7 @@ describe('Select.Tags', () => {
513513
const { container } = render(<Select mode="tags" tabIndex={0} />);
514514
expect(container.querySelector('.rc-select').getAttribute('tabindex')).toBeFalsy();
515515

516-
expect(
517-
container.querySelector('input.rc-select-selection-search-input').getAttribute('tabindex'),
518-
).toBe('0');
516+
expect(container.querySelector('input').getAttribute('tabindex')).toBe('0');
519517
});
520518

521519
it('press enter should not submit form', () => {

0 commit comments

Comments
 (0)