Skip to content

Commit 5afe84f

Browse files
committed
feat: add onBlurAddValue & onBlurRemoveSpaces APIs for Select tags mode
1 parent 8dee5af commit 5afe84f

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default () => (
133133
| optionRender | Custom rendering options | (oriOption: FlattenOptionData\<BaseOptionType\> , info: { index: number }) => React.ReactNode | - |
134134
| labelRender | Custom rendering label | (props: LabelInValueType) => React.ReactNode | - |
135135
| maxCount | The max number of items can be selected | number | - |
136-
| onBlurRemoveSpace | Whether to remove space when losing focus, only applies when `mode` is `tags` | boolean | true |
136+
| onBlurRemoveSpaces | Whether to remove spaces when losing focus, only applies when `mode` is `tags` | boolean | true |
137137
| onBlurAddValue | Whether to add the input value to the selected item when losing focus, only applies when `mode` is `tags` | boolean | true |
138138

139139
### Methods

docs/examples/tags.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ const Test: React.FC = () => {
111111
{children}
112112
</Select>
113113
</div>
114+
<h2>tags select with onBlurRemoveSpaces = false</h2>
115+
<div>
116+
<Select
117+
placeholder="placeholder"
118+
mode="tags"
119+
style={{ width: 500 }}
120+
disabled={disabled}
121+
maxTagCount={maxTagCount}
122+
maxTagTextLength={10}
123+
value={value}
124+
onBlurRemoveSpaces={false}
125+
onChange={(val) => {
126+
console.log('change:', val);
127+
setValue(val);
128+
}}
129+
/>
130+
</div>
131+
<h2>tags select with onBlurAddValue = false</h2>
132+
<div>
133+
<Select
134+
placeholder="placeholder"
135+
mode="tags"
136+
style={{ width: 500 }}
137+
disabled={disabled}
138+
maxTagCount={maxTagCount}
139+
maxTagTextLength={10}
140+
value={value}
141+
onBlurAddValue={false}
142+
onChange={(val) => {
143+
console.log('change:', val);
144+
setValue(val);
145+
}}
146+
/>
147+
</div>
114148
</div>
115149
);
116150
};

src/Select.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
164164
onChange?: (value: ValueType, option: OptionType | OptionType[]) => void;
165165

166166
// >>> tags mode only
167-
onBlurRemoveSpace?: boolean;
167+
onBlurRemoveSpaces?: boolean;
168168
onBlurAddValue?: boolean;
169169
}
170170

@@ -216,7 +216,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
216216
maxCount,
217217

218218
// tags mode only
219-
onBlurRemoveSpace = true,
219+
onBlurRemoveSpaces = true,
220220
onBlurAddValue = true,
221221

222222
...restProps
@@ -583,9 +583,9 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
583583

584584
// [Submit] Tag mode should flush input
585585
if (info.source === 'submit') {
586-
const formatted = onBlurRemoveSpace ? (searchText || '').trim() : searchText || '';
586+
const formatted = onBlurRemoveSpaces ? (searchText || '').trim() : searchText || '';
587587
// prevent empty tags from appearing when you click the Enter button
588-
if (formatted && onBlurAddValue) {
588+
if (formatted.trim() && onBlurAddValue) {
589589
const newRawValues = Array.from(new Set<RawValueType>([...rawValues, formatted]));
590590
triggerChange(newRawValues);
591591
triggerSelect(formatted, true);

tests/Tags.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,22 @@ describe('Select.Tags', () => {
540540
});
541541
expect(onChange).not.toBeCalled();
542542
});
543+
544+
it('should not add value when onBlurAddValue is false', () => {
545+
const { container } = render(<Select mode="tags" onBlurAddValue={false} />);
546+
const input = container.querySelector<HTMLInputElement>('input');
547+
toggleOpen(container);
548+
fireEvent.change(input, { target: { value: 'test' } });
549+
keyDown(input, KeyCode.TAB);
550+
// no selection item
551+
expect(container.querySelectorAll('.rc-select-selection-item')).toHaveLength(0);
552+
});
553+
554+
it('should not add value when onBlurRemoveSpaces is false', () => {
555+
const { container } = render(<Select mode="tags" onBlurRemoveSpaces={false} />);
556+
toggleOpen(container);
557+
fireEvent.change(container.querySelector('input'), { target: { value: ' test ' } });
558+
keyDown(container.querySelector('input'), KeyCode.ENTER);
559+
expect(findSelection(container).textContent).toEqual(' test ');
560+
});
543561
});

0 commit comments

Comments
 (0)