Skip to content

Commit

Permalink
feat: add onBlurAddValue & onBlurRemoveSpaces APIs for Select tags mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aojunhao123 committed Nov 2, 2024
1 parent 8dee5af commit 5afe84f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default () => (
| optionRender | Custom rendering options | (oriOption: FlattenOptionData\<BaseOptionType\> , info: { index: number }) => React.ReactNode | - |
| labelRender | Custom rendering label | (props: LabelInValueType) => React.ReactNode | - |
| maxCount | The max number of items can be selected | number | - |
| onBlurRemoveSpace | Whether to remove space when losing focus, only applies when `mode` is `tags` | boolean | true |
| onBlurRemoveSpaces | Whether to remove spaces when losing focus, only applies when `mode` is `tags` | boolean | true |
| onBlurAddValue | Whether to add the input value to the selected item when losing focus, only applies when `mode` is `tags` | boolean | true |

### Methods
Expand Down
34 changes: 34 additions & 0 deletions docs/examples/tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ const Test: React.FC = () => {
{children}
</Select>
</div>
<h2>tags select with onBlurRemoveSpaces = false</h2>
<div>
<Select
placeholder="placeholder"
mode="tags"
style={{ width: 500 }}
disabled={disabled}
maxTagCount={maxTagCount}
maxTagTextLength={10}
value={value}
onBlurRemoveSpaces={false}
onChange={(val) => {
console.log('change:', val);
setValue(val);
}}
/>
</div>
<h2>tags select with onBlurAddValue = false</h2>
<div>
<Select
placeholder="placeholder"
mode="tags"
style={{ width: 500 }}
disabled={disabled}
maxTagCount={maxTagCount}
maxTagTextLength={10}
value={value}
onBlurAddValue={false}
onChange={(val) => {
console.log('change:', val);
setValue(val);
}}
/>
</div>
</div>
);
};
Expand Down
8 changes: 4 additions & 4 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
onChange?: (value: ValueType, option: OptionType | OptionType[]) => void;

// >>> tags mode only
onBlurRemoveSpace?: boolean;
onBlurRemoveSpaces?: boolean;
onBlurAddValue?: boolean;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
maxCount,

// tags mode only
onBlurRemoveSpace = true,
onBlurRemoveSpaces = true,
onBlurAddValue = true,

...restProps
Expand Down Expand Up @@ -583,9 +583,9 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp

// [Submit] Tag mode should flush input
if (info.source === 'submit') {
const formatted = onBlurRemoveSpace ? (searchText || '').trim() : searchText || '';
const formatted = onBlurRemoveSpaces ? (searchText || '').trim() : searchText || '';
// prevent empty tags from appearing when you click the Enter button
if (formatted && onBlurAddValue) {
if (formatted.trim() && onBlurAddValue) {
const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
triggerChange(newRawValues);
triggerSelect(formatted, true);
Expand Down
18 changes: 18 additions & 0 deletions tests/Tags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,22 @@ describe('Select.Tags', () => {
});
expect(onChange).not.toBeCalled();
});

it('should not add value when onBlurAddValue is false', () => {
const { container } = render(<Select mode="tags" onBlurAddValue={false} />);
const input = container.querySelector<HTMLInputElement>('input');
toggleOpen(container);
fireEvent.change(input, { target: { value: 'test' } });
keyDown(input, KeyCode.TAB);
// no selection item
expect(container.querySelectorAll('.rc-select-selection-item')).toHaveLength(0);
});

it('should not add value when onBlurRemoveSpaces is false', () => {
const { container } = render(<Select mode="tags" onBlurRemoveSpaces={false} />);
toggleOpen(container);
fireEvent.change(container.querySelector('input'), { target: { value: ' test ' } });
keyDown(container.querySelector('input'), KeyCode.ENTER);
expect(findSelection(container).textContent).toEqual(' test ');
});
});

0 comments on commit 5afe84f

Please sign in to comment.