Skip to content

Commit dc0ffc0

Browse files
authored
fix: prevent dropdown from closing when mousedown on input in multiple mode (#1214)
* fix: avoid closing dropdown on input mousedown in multiple mode * chore: simplify input click condition * refactor: simplify isClickOnInput condition
1 parent 700c9f8 commit dc0ffc0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/SelectInput/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,21 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
180180
// so we need to mark the event directly
181181
(event.nativeEvent as any)._ori_target = inputDOM;
182182

183-
if (inputDOM && event.target !== inputDOM && !inputDOM.contains(event.target as Node)) {
183+
const isClickOnInput = inputDOM === event.target || inputDOM?.contains(event.target as Node);
184+
185+
if (inputDOM && !isClickOnInput) {
184186
event.preventDefault();
185187
}
186188

187189
// Check if we should prevent closing when clicking on selector
188190
// Don't close if: open && not multiple && (combobox mode || showSearch)
189-
const shouldPreventClose = triggerOpen && !multiple && (mode === 'combobox' || showSearch);
191+
const shouldPreventCloseOnSingle =
192+
triggerOpen && !multiple && (mode === 'combobox' || showSearch);
193+
194+
// Don't close if: open && multiple && click on input
195+
const shouldPreventCloseOnMultipleInput = triggerOpen && multiple && isClickOnInput;
196+
197+
const shouldPreventClose = shouldPreventCloseOnSingle || shouldPreventCloseOnMultipleInput;
190198

191199
if (!(event.nativeEvent as any)._select_lazy) {
192200
inputRef.current?.focus();

tests/Multiple.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,5 +712,22 @@ describe('Select.Multiple', () => {
712712
toggleOpen(container);
713713
expect(container.querySelector('input')).toHaveValue('');
714714
});
715+
716+
it('should not close dropdown when mousedown on input in multiple mode (text selection)', () => {
717+
const { container } = render(
718+
<Select mode="multiple" showSearch options={[{ value: 'light' }]} />,
719+
);
720+
721+
const input = container.querySelector('input') as HTMLInputElement;
722+
723+
// Open dropdown first
724+
toggleOpen(container);
725+
expectOpen(container, true);
726+
727+
// Start interaction from input (simulate starting text selection / cursor move)
728+
fireEvent.mouseDown(input);
729+
730+
expectOpen(container, true);
731+
});
715732
});
716733
});

0 commit comments

Comments
 (0)