Skip to content

Commit 68a909f

Browse files
li-jia-nanMadCcc
andauthored
fix: Select support maxCount (#1015)
* fix: Select support maxCount * fix: fix error * test: add test case * test: add test case * Update src/BaseSelect.tsx Co-authored-by: MadCcc <[email protected]> * fix: fix * fix: fix * fix: fix * fix: fix * Update src/hooks/useOverMaxCount.ts Co-authored-by: MadCcc <[email protected]> * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix * Update src/BaseSelect.tsx Co-authored-by: MadCcc <[email protected]> * fix: fix --------- Co-authored-by: MadCcc <[email protected]>
1 parent 312c791 commit 68a909f

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

docs/examples/auto-tokenization.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Demo: React.FC = () => (
77
<h2>自动分词</h2>
88
<Select
99
mode="tags"
10+
maxCount={3}
1011
style={{ width: '100%' }}
1112
tokenSeparators={[',']}
1213
options={Array.from({ length: 20 }, (_, i) => ({ label: i.toString(), value: i.toString() }))}

src/BaseSelect.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import type { RefTriggerProps } from './SelectTrigger';
2828
import SelectTrigger from './SelectTrigger';
2929
import TransBtn from './TransBtn';
3030
import { getSeparatedContent } from './utils/valueUtil';
31+
import SelectContext from './SelectContext';
32+
import type { SelectContextProps } from './SelectContext';
3133

3234
export type {
3335
DisplayInfoType,
@@ -394,12 +396,21 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
394396
[tokenSeparators],
395397
);
396398

399+
const { maxCount, rawValues } = React.useContext<SelectContextProps>(SelectContext) || {};
400+
397401
const onInternalSearch = (searchText: string, fromTyping: boolean, isCompositing: boolean) => {
402+
if (rawValues?.size >= maxCount) {
403+
return;
404+
}
398405
let ret = true;
399406
let newSearchText = searchText;
400407
onActiveValueChange?.(null);
401408

402-
const separatedList = getSeparatedContent(searchText, tokenSeparators);
409+
const separatedList = getSeparatedContent(
410+
searchText,
411+
tokenSeparators,
412+
maxCount && maxCount - rawValues.size,
413+
);
403414

404415
// Check if match the `tokenSeparators`
405416
const patchLabels: string[] = isCompositing ? null : separatedList;

src/utils/valueUtil.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function injectPropsWithOption<T extends object>(option: T): T {
111111
return newOption;
112112
}
113113

114-
export const getSeparatedContent = (text: string, tokens: string[]): string[] => {
114+
export const getSeparatedContent = (text: string, tokens: string[], end?: number): string[] => {
115115
if (!tokens || !tokens.length) {
116116
return null;
117117
}
@@ -127,5 +127,9 @@ export const getSeparatedContent = (text: string, tokens: string[]): string[] =>
127127
.filter(Boolean);
128128
};
129129
const list = separate(text, tokens);
130-
return match ? list : null;
130+
if (match) {
131+
return typeof end !== 'undefined' ? list.slice(0, end) : list;
132+
} else {
133+
return null;
134+
}
131135
};

tests/Tags.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mount } from 'enzyme';
2-
import { render } from '@testing-library/react';
2+
import { fireEvent, render } from '@testing-library/react';
33
import KeyCode from 'rc-util/lib/KeyCode';
44
import classNames from 'classnames';
55
import * as React from 'react';
@@ -505,4 +505,25 @@ describe('Select.Tags', () => {
505505
expect(wrapper.find('.rc-select-item-option').length).toBe(1);
506506
expect(errSpy).not.toHaveBeenCalled();
507507
});
508+
509+
it(`paste content to split when count >= maxCount`, () => {
510+
const onChange = jest.fn();
511+
const { container } = render(
512+
<Select
513+
mode="tags"
514+
maxCount={3}
515+
onChange={onChange}
516+
tokenSeparators={[',']}
517+
value={['1', '2', '3']}
518+
/>,
519+
);
520+
const input = container.querySelector<HTMLInputElement>('input');
521+
fireEvent.paste(input, {
522+
clipboardData: { getData: () => 'test' },
523+
});
524+
fireEvent.change(input, {
525+
target: { value: 'test' },
526+
});
527+
expect(onChange).not.toBeCalled();
528+
});
508529
});

0 commit comments

Comments
 (0)