Skip to content

Commit 2cb2682

Browse files
authored
demo: update demo (#1014)
* demo: update demo * update demo
1 parent 54a2063 commit 2cb2682

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

docs/demo/auto-tokenization.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: auto-tokenization
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/auto-tokenization.tsx"></code>

docs/examples/auto-tokenization.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import Select from 'rc-select';
3+
import '../../assets/index.less';
4+
5+
const Demo: React.FC = () => (
6+
<>
7+
<h2>自动分词</h2>
8+
<Select
9+
mode="tags"
10+
style={{ width: '100%' }}
11+
tokenSeparators={[',']}
12+
options={Array.from({ length: 20 }, (_, i) => ({ label: i.toString(), value: i.toString() }))}
13+
/>
14+
</>
15+
);
16+
17+
export default Demo;

src/BaseSelect.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ export interface BaseSelectProps extends BaseSelectPrivateProps, React.AriaAttri
205205
onClick?: React.MouseEventHandler<HTMLDivElement>;
206206
}
207207

208-
export function isMultiple(mode: Mode) {
209-
return mode === 'tags' || mode === 'multiple';
210-
}
208+
export const isMultiple = (mode: Mode) => mode === 'tags' || mode === 'multiple';
211209

212210
const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref) => {
213211
const {
@@ -293,7 +291,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
293291

294292
const domProps = {
295293
...restProps,
296-
} as Omit<keyof typeof restProps, (typeof DEFAULT_OMIT_PROPS)[number]>;
294+
};
297295

298296
DEFAULT_OMIT_PROPS.forEach((propName) => {
299297
delete domProps[propName];
@@ -400,10 +398,10 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
400398
let newSearchText = searchText;
401399
onActiveValueChange?.(null);
402400

401+
const separatedList = getSeparatedContent(searchText, tokenSeparators);
402+
403403
// Check if match the `tokenSeparators`
404-
const patchLabels: string[] = isCompositing
405-
? null
406-
: getSeparatedContent(searchText, tokenSeparators);
404+
const patchLabels: string[] = isCompositing ? null : separatedList;
407405

408406
// Ignore combobox since it's not split-able
409407
if (mode !== 'combobox' && patchLabels) {

src/utils/valueUtil.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function flattenOptions<OptionType extends BaseOptionType = DefaultOption
4747
label: fieldLabel,
4848
value: fieldValue,
4949
options: fieldOptions,
50-
groupLabel
50+
groupLabel,
5151
} = fillFieldNames(fieldNames, false);
5252

5353
function dig(list: OptionType[], isGroupOption: boolean) {
@@ -111,26 +111,21 @@ export function injectPropsWithOption<T extends object>(option: T): T {
111111
return newOption;
112112
}
113113

114-
export function getSeparatedContent(text: string, tokens: string[]): string[] {
114+
export const getSeparatedContent = (text: string, tokens: string[]): string[] | null => {
115115
if (!tokens || !tokens.length) {
116116
return null;
117117
}
118-
119118
let match = false;
120-
121-
function separate(str: string, [token, ...restTokens]: string[]) {
119+
const separate = (str: string, [token, ...restTokens]: string[]): string[] => {
122120
if (!token) {
123121
return [str];
124122
}
125-
126123
const list = str.split(token);
127124
match = match || list.length > 1;
128-
129125
return list
130126
.reduce((prevList, unitStr) => [...prevList, ...separate(unitStr, restTokens)], [])
131-
.filter((unit) => unit);
132-
}
133-
127+
.filter(Boolean);
128+
};
134129
const list = separate(text, tokens);
135130
return match ? list : null;
136-
}
131+
};

0 commit comments

Comments
 (0)