Skip to content

Commit 0dd01c5

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

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

docs/examples/tags.tsx

+2
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+
preventCommitOnBlur={false}
4243
style={{ width: 400 }}
4344
disabled={disabled}
4445
maxTagCount={maxTagCount}
@@ -88,6 +89,7 @@ const Test: React.FC = () => {
8889
<Select
8990
placeholder="placeholder"
9091
mode="tags"
92+
preventCommitOnBlur={false}
9193
style={{ width: 500 }}
9294
disabled={disabled}
9395
maxTagCount={maxTagCount}

src/Select.tsx

+12-1
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+
preventCommitOnBlur?: 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+
preventCommitOnBlur = false,
206+
201207
...restProps
202208
} = props;
203209

@@ -536,6 +542,12 @@ const Select = React.forwardRef(
536542
setSearchValue(searchText);
537543
setActiveValue(null);
538544

545+
if(preventCommitOnBlur){
546+
triggerChange('');
547+
setSearchValue('');
548+
return
549+
}
550+
539551
// [Submit] Tag mode should flush input
540552
if (info.source === 'submit') {
541553
const formatted = (searchText || '').trim();
@@ -546,7 +558,6 @@ const Select = React.forwardRef(
546558
triggerSelect(formatted, true);
547559
setSearchValue('');
548560
}
549-
550561
return;
551562
}
552563

tests/Tags.test.tsx

+14
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} preventCommitOnBlur={true} />);
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)