Skip to content

Commit 44f2953

Browse files
authored
fix: separate error when content contains (#522)
1 parent f1651a5 commit 44f2953

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

src/Selector/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface SelectorProps {
7373
maxTagPlaceholder?: React.ReactNode | ((omittedValues: LabelValueType[]) => React.ReactNode);
7474
tagRender?: (props: CustomTagProps) => React.ReactElement;
7575

76-
/** Check if `tokenSeparators` contains `\n` */
76+
/** Check if `tokenSeparators` contains `\n` or `\r\n` */
7777
tokenWithEnter?: boolean;
7878

7979
// Motion
@@ -180,7 +180,8 @@ const Selector: React.RefForwardingComponent<RefSelectorProps, SelectorProps> =
180180

181181
// Pasted text should replace back to origin content
182182
if (tokenWithEnter && pastedTextRef.current && /[\r\n]/.test(pastedTextRef.current)) {
183-
const replacedText = pastedTextRef.current.replace(/[\r\n]/g, ' ');
183+
// CRLF will be treated as a single space for input element
184+
const replacedText = pastedTextRef.current.replace(/\r\n/g, ' ').replace(/[\r\n]/g, ' ');
184185
value = value.replace(replacedText, pastedTextRef.current);
185186
}
186187

src/generate.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ 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]);
325+
const tokenWithEnter = useMemo(
326+
() => (tokenSeparators || []).some(tokenSeparator => ['\n', '\r\n'].includes(tokenSeparator)),
327+
[tokenSeparators],
328+
);
326329

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

tests/Tags.test.tsx

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,49 @@ describe('Select.Tags', () => {
140140
expectOpen(wrapper, false);
141141
});
142142

143-
it('paste content to split', () => {
144-
const onChange = jest.fn();
145-
const wrapper = mount(
146-
<Select mode="tags" tokenSeparators={[' ', '\n']} onChange={onChange}>
147-
<Option value="1">1</Option>
148-
</Select>,
149-
);
143+
// Paste tests
144+
[
145+
{
146+
tokenSeparators: [' ', '\n'],
147+
clipboardText: '\n light\n bamboo\n ',
148+
inputValue: ' light bamboo ',
149+
},
150+
{
151+
tokenSeparators: ['\r\n'],
152+
clipboardText: '\r\nlight\r\nbamboo\r\n',
153+
inputValue: ' light bamboo ',
154+
},
155+
{
156+
tokenSeparators: [' ', '\r\n'],
157+
clipboardText: '\r\n light\r\n bamboo\r\n ',
158+
inputValue: ' light bamboo ',
159+
},
160+
{
161+
tokenSeparators: ['\n'],
162+
clipboardText: '\nlight\nbamboo\n',
163+
inputValue: ' light bamboo ',
164+
},
165+
].forEach(({ tokenSeparators, clipboardText, inputValue }) => {
166+
it(`paste content to split (${JSON.stringify(tokenSeparators)})`, () => {
167+
const onChange = jest.fn();
168+
const wrapper = mount(
169+
<Select mode="tags" tokenSeparators={tokenSeparators} onChange={onChange}>
170+
<Option value="1">1</Option>
171+
</Select>,
172+
);
150173

151-
wrapper.find('input').simulate('paste', {
152-
clipboardData: {
153-
getData: () => `
154-
light
155-
bamboo
156-
`,
157-
},
174+
wrapper.find('input').simulate('paste', {
175+
clipboardData: {
176+
getData: () => clipboardText,
177+
},
178+
});
179+
wrapper.find('input').simulate('change', {
180+
target: { value: inputValue },
181+
});
182+
183+
expect(onChange).toHaveBeenCalledWith(['light', 'bamboo'], expect.anything());
184+
expect(onChange).toHaveBeenCalledTimes(1);
158185
});
159-
wrapper.find('input').simulate('change', {
160-
target: { value: ' light bamboo ' },
161-
});
162-
163-
expect(onChange).toHaveBeenCalledWith(['light', 'bamboo'], expect.anything());
164-
expect(onChange).toHaveBeenCalledTimes(1);
165186
});
166187

167188
it('renders unlisted item in value', () => {

0 commit comments

Comments
 (0)