Skip to content

Commit 344404d

Browse files
authored
fix(select): merge raw trigger element event handlers (#1215)
1 parent dc0ffc0 commit 344404d

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/SelectInput/index.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,29 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
227227
};
228228

229229
if (RootComponent) {
230+
const originProps = (RootComponent as any).props || {};
231+
const mergedProps = { ...originProps, ...domProps };
232+
233+
Object.keys(originProps).forEach((key) => {
234+
const originVal = originProps[key];
235+
const domVal = domProps[key];
236+
237+
if (typeof originVal === 'function' && typeof domVal === 'function') {
238+
mergedProps[key] = (...args: any[]) => {
239+
domVal(...args);
240+
originVal(...args);
241+
};
242+
}
243+
});
244+
230245
if (React.isValidElement<any>(RootComponent)) {
231246
return React.cloneElement(RootComponent, {
232-
...domProps,
247+
...mergedProps,
233248
ref: composeRef((RootComponent as any).ref, rootRef),
234249
});
235250
}
236251

237-
return <RootComponent {...domProps} ref={rootRef} />;
252+
return <RootComponent {...mergedProps} ref={rootRef} />;
238253
}
239254

240255
return (

tests/Custom.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,25 @@ describe('Select.Custom', () => {
2727

2828
expect(onPopupVisibleChange).toHaveBeenCalledWith(true);
2929
});
30+
31+
it('should not override raw input element event handlers', () => {
32+
const onFocus = jest.fn();
33+
const onBlur = jest.fn();
34+
35+
const { getByPlaceholderText } = render(
36+
<Select
37+
showSearch
38+
options={[{ value: 'a', label: 'A' }]}
39+
getRawInputElement={() => (
40+
<input placeholder="focus me" onFocus={onFocus} onBlur={onBlur} />
41+
)}
42+
/>,
43+
);
44+
45+
fireEvent.focus(getByPlaceholderText('focus me'));
46+
fireEvent.blur(getByPlaceholderText('focus me'));
47+
48+
expect(onFocus).toHaveBeenCalled();
49+
expect(onBlur).toHaveBeenCalled();
50+
});
3051
});

0 commit comments

Comments
 (0)