Skip to content

Commit 1b6a7a4

Browse files
committed
fix(select): merge raw trigger element event handlers
1 parent 700c9f8 commit 1b6a7a4

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/SelectInput/index.tsx

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

221221
if (RootComponent) {
222+
const originProps = (RootComponent as any).props || {};
223+
const mergedProps = { ...originProps, ...domProps };
224+
225+
Object.keys(originProps).forEach((key) => {
226+
const originVal = originProps[key];
227+
const domVal = domProps[key];
228+
229+
if (typeof originVal === 'function' && typeof domVal === 'function') {
230+
mergedProps[key] = (...args: any[]) => {
231+
domVal(...args);
232+
originVal(...args);
233+
};
234+
}
235+
});
236+
222237
if (React.isValidElement<any>(RootComponent)) {
223238
return React.cloneElement(RootComponent, {
224-
...domProps,
239+
...mergedProps,
225240
ref: composeRef((RootComponent as any).ref, rootRef),
226241
});
227242
}
228243

229-
return <RootComponent {...domProps} ref={rootRef} />;
244+
return <RootComponent {...mergedProps} ref={rootRef} />;
230245
}
231246

232247
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)