Skip to content

Commit 278bcea

Browse files
committed
feat(select): allows select the behavior of triggering blur event in tags mode.
1 parent 7d140b7 commit 278bcea

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

docs/examples/tags.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const Test: React.FC = () => {
3939
<Select
4040
placeholder="placeholder"
4141
mode="tags"
42+
tagsModeCommitOnBlur={false}
4243
style={{ width: 400 }}
4344
disabled={disabled}
4445
maxTagCount={maxTagCount}

src/Select.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
152152
value?: ValueType | null;
153153
defaultValue?: ValueType | null;
154154
onChange?: (value: ValueType, option: OptionType | OptionType[]) => void;
155+
156+
// >>> Blur
157+
tagsModeCommitOnBlur?: boolean
155158
}
156159

157160
function isRawValue(value: DraftValueType): value is RawValueType {
@@ -198,6 +201,9 @@ const Select = React.forwardRef(
198201
labelInValue,
199202
onChange,
200203

204+
// Blur
205+
tagsModeCommitOnBlur = true,
206+
201207
...restProps
202208
} = props;
203209

@@ -538,15 +544,20 @@ const Select = React.forwardRef(
538544

539545
// [Submit] Tag mode should flush input
540546
if (info.source === 'submit') {
541-
const formatted = (searchText || '').trim();
542-
// prevent empty tags from appearing when you click the Enter button
543-
if (formatted) {
544-
const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
545-
triggerChange(newRawValues);
546-
triggerSelect(formatted, true);
547-
setSearchValue('');
547+
if(!tagsModeCommitOnBlur){
548+
// prevent empty tags from appearing when you click the Enter button
549+
triggerChange('');
550+
setSearchValue('');
551+
}else{
552+
const formatted = (searchText || '').trim();
553+
// prevent empty tags from appearing when you click the Enter button
554+
if (formatted) {
555+
const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
556+
triggerChange(newRawValues);
557+
triggerSelect(formatted, true);
558+
setSearchValue('');
559+
}
548560
}
549-
550561
return;
551562
}
552563

tests/Tags.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ describe('Select.Tags', () => {
6363
expect(onChange).toHaveBeenCalledWith(['foo'], [{}]);
6464
});
6565

66+
it('should not call on blur when set attribute tagsModeCommitOnBlur equals false', () => {
67+
const onChange = jest.fn();
68+
const wrapper = mount(<Select mode="tags" onChange={onChange} tagsModeCommitOnBlur={false} />);
69+
70+
wrapper
71+
.find('input')
72+
.simulate('change', { target: { value: 'foo' } })
73+
.simulate('blur');
74+
75+
jest.runAllTimers();
76+
expect(findSelection(wrapper).text()).toBe("");
77+
expect(onChange).toHaveBeenCalledWith([''], [{}]);
78+
});
79+
6680
it('tokenize input', () => {
6781
const handleChange = jest.fn();
6882
const handleSelect = jest.fn();

0 commit comments

Comments
 (0)