Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/examples/tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Test: React.FC = () => {
<Select
placeholder="placeholder"
mode="tags"
preventCommitOnBlur={false}
style={{ width: 400 }}
disabled={disabled}
maxTagCount={maxTagCount}
Expand Down Expand Up @@ -88,6 +89,7 @@ const Test: React.FC = () => {
<Select
placeholder="placeholder"
mode="tags"
preventCommitOnBlur={false}
style={{ width: 500 }}
disabled={disabled}
maxTagCount={maxTagCount}
Expand Down
13 changes: 12 additions & 1 deletion src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
value?: ValueType | null;
defaultValue?: ValueType | null;
onChange?: (value: ValueType, option: OptionType | OptionType[]) => void;

// >>> Blur
preventCommitOnBlur?: boolean
}

function isRawValue(value: DraftValueType): value is RawValueType {
Expand Down Expand Up @@ -198,6 +201,9 @@ const Select = React.forwardRef(
labelInValue,
onChange,

// Blur
preventCommitOnBlur = false,

...restProps
} = props;

Expand Down Expand Up @@ -536,6 +542,12 @@ const Select = React.forwardRef(
setSearchValue(searchText);
setActiveValue(null);

if(preventCommitOnBlur){
triggerChange('');
setSearchValue('');
return
}

// [Submit] Tag mode should flush input
if (info.source === 'submit') {
const formatted = (searchText || '').trim();
Expand All @@ -546,7 +558,6 @@ const Select = React.forwardRef(
triggerSelect(formatted, true);
setSearchValue('');
}

return;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Tags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ describe('Select.Tags', () => {
expect(onChange).toHaveBeenCalledWith(['foo'], [{}]);
});

it('should not call on blur when set attribute tagsModeCommitOnBlur equals false', () => {
const onChange = jest.fn();
const wrapper = mount(<Select mode="tags" onChange={onChange} preventCommitOnBlur={true} />);

wrapper
.find('input')
.simulate('change', { target: { value: 'foo' } })
.simulate('blur');

jest.runAllTimers();
expect(findSelection(wrapper).text()).toBe("");
expect(onChange).toHaveBeenCalledWith([''], [{}]);
});

it('tokenize input', () => {
const handleChange = jest.fn();
const handleSelect = jest.fn();
Expand Down