Skip to content

Commit

Permalink
feat(select): allows select the behavior of triggering blur event in …
Browse files Browse the repository at this point in the history
…tags mode.
  • Loading branch information
yuanxin518 committed Jul 20, 2023
1 parent 7d140b7 commit 0dd01c5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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

0 comments on commit 0dd01c5

Please sign in to comment.