Skip to content

Commit b41fc36

Browse files
authored
feat: [TagInput] add split API (#2984)
* feat: [TagInput] add split API * docs: change API version * chore: Modify the API version number in the doc
1 parent 74d729f commit b41fc36

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

content/input/taginput/index-en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ import { TagInput } from '@douyinfe/semi-ui';
454454
|separator |Customize the separator |string\|string[] |, |1.19.0,string[] is supported since 1.29.0|
455455
|showClear |Whether to show the clear button |boolean |false |1.19.0|
456456
|size |Size, one of `small``large``default` |string |`default` |1.19.0|
457+
|split |Customize the separator processing function |(value: string, separator: string) => string[] | - |2.90.0|
457458
|style |Inline style |React.CSSProperties | - |1.19.0|
458459
|suffix |Suffix |ReactNode |- |1.19.0|
459460
|validateStatus|Validate status for styling only, one of `default``warning``error`|string |`default` |1.19.0|

content/input/taginput/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ import { TagInput } from '@douyinfe/semi-ui';
455455
|separator |设置批量输入时的分隔符 |string\|string[] |, |1.19.0, string[]是从1.29.0开始支持|
456456
|showClear |是否支持一键删除所有标签和输入内容 |boolean |false |1.19.0|
457457
|size |设置输入框尺寸,可选: `small``large``default` |string |`default` |1.19.0|
458+
|split |自定义分隔符处理函数 |(value: string, separator: string) => string[] | - |2.90.0|
458459
|style |内联样式 |React.CSSProperties | - |1.19.0|
459460
|suffix |后缀标签 |ReactNode |- |1.19.0|
460461
|validateStatus|设置校验状态样式,可选: `default``warning``error` |string |`default` |1.19.0|

packages/semi-foundation/tagInput/foundation.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class TagInputFoundation extends BaseFoundation<TagInputAdapter> {
7373
this._adapter.setEntering(true);
7474
}
7575

76+
_splitArray = (originString: string, separators: string | string[] | null) => {
77+
const { split } = this.getProps();
78+
if (isFunction(split)) {
79+
return split(originString, separators);
80+
}
81+
return getSplitedArray(originString, separators);
82+
}
83+
7684
handleInputCompositionEnd = (e: any) => {
7785
const { value } = e.target;
7886
const {
@@ -85,7 +93,7 @@ class TagInputFoundation extends BaseFoundation<TagInputAdapter> {
8593
}
8694
this._adapter.setEntering(false);
8795
let allowChange = true;
88-
const inputArr = getSplitedArray(value, separator);
96+
const inputArr = this._splitArray(value, separator);
8997
let index = 0;
9098
for (; index < inputArr.length; index++) {
9199
if (inputArr[index].length > maxLength) {
@@ -121,8 +129,8 @@ class TagInputFoundation extends BaseFoundation<TagInputAdapter> {
121129
const { inputValue } = this._adapter.getStates();
122130
let allowChange = true;
123131
if (isNumber(maxLength)) {
124-
const valueArr = getSplitedArray(value, separator);
125-
const inputArr = getSplitedArray(inputValue, separator);
132+
const valueArr = this._splitArray(value, separator);
133+
const inputArr = this._splitArray(inputValue, separator);
126134
const maxLen = Math.max(valueArr.length, inputArr.length);
127135
for (let i = 0; i < maxLen; i++) {
128136
// When the input length is increasing
@@ -174,7 +182,7 @@ class TagInputFoundation extends BaseFoundation<TagInputAdapter> {
174182
inputValue,
175183
tagsArray
176184
} = this._adapter.getStates();
177-
let addTags = getSplitedArray(inputValue, separator);
185+
let addTags = this._splitArray(inputValue, separator);
178186

179187
addTags = addTags.filter((item, idx) => {
180188
// If allowDuplicates is false, then filter duplicates

packages/semi-ui/tagInput/_story/tagInput.stories.jsx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React, { useState, useCallback } from 'react';
22
import { Toast, Icon, Button, Avatar, Form, Popover, SideSheet, Modal, TagInput, Switch } from '../../index';
3-
import { IconGift, IconVigoLogo, IconClose } from '@douyinfe/semi-icons';
3+
import { IconGift, IconVigoLogo, IconClose, IconCopy } from '@douyinfe/semi-icons';
4+
import copy from 'copy-text-to-clipboard';
5+
46
const style = {
57
width: 400,
68
marginTop: 10,
@@ -591,4 +593,39 @@ export const longTextItemDraggable = () => {
591593
style={{ width: 300 }}
592594
/>
593595
)
594-
}
596+
}
597+
598+
export const customSplit = () => {
599+
600+
const split = (value, separator) => {
601+
// custom split logic
602+
// 这是一个简单的测试用例,假设 separator 为 ',',不想被拆分的,前面用了 '/' 反义字符做标识
603+
const uniqueSeparator = 'uniq_semi_tag_input_separator';
604+
const uniqueSeparator2 = 'uniq2_semi_tag_input_separator';
605+
let temp = value.replace(new RegExp('/,', 'g'), uniqueSeparator);
606+
temp = temp.replace(new RegExp(',', 'g'), uniqueSeparator2);
607+
temp = temp.replace(new RegExp(uniqueSeparator, 'g'), ',');
608+
return temp.split(uniqueSeparator2);
609+
}
610+
611+
const clickToCopy = useCallback(() => {
612+
copy('test1,test2,test3/,3, test4');
613+
}, []);
614+
615+
return (
616+
<>
617+
<div>点击获取测试数据<Button theme='borderless' type='primary' onClick={clickToCopy} icon={<IconCopy />} /></div>
618+
<TagInput
619+
separator={','}
620+
placeholder='单个标签长度不超过5...'
621+
style={{ marginTop: 12, width: 400 }}
622+
onChange={v => console.log(v)}
623+
split={split}
624+
onInputExceed={v => {
625+
Toast.warning('超过 maxLength');
626+
console.log(v);
627+
}}
628+
/>
629+
</>
630+
);
631+
}

packages/semi-ui/tagInput/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface TagInputProps {
6666
separator?: string | string[] | null;
6767
showClear?: boolean;
6868
size?: Size;
69+
split?: (originString: string, separators: string | string[] | null) => string[];
6970
style?: React.CSSProperties;
7071
suffix?: React.ReactNode;
7172
validateStatus?: ValidateStatus;

0 commit comments

Comments
 (0)