Skip to content

Commit 14c47d1

Browse files
committed
[joy-ui][Tooltip] Support event handlers with extra parameters
1 parent 3455c42 commit 14c47d1

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

packages/mui-joy/src/Tooltip/Tooltip.test.tsx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { expect } from 'chai';
3-
import { createRenderer } from '@mui-internal/test-utils';
3+
import { spy } from 'sinon';
4+
import { createRenderer, act } from '@mui-internal/test-utils';
45
import { unstable_capitalize as capitalize } from '@mui/utils';
56
import { PopperProps } from '@mui/base';
67
import { ThemeProvider } from '@mui/joy/styles';
@@ -130,4 +131,51 @@ describe('<Tooltip />', () => {
130131
});
131132
});
132133
});
134+
135+
describe('focus', () => {
136+
// https://github.com/mui/mui-x/issues/12248
137+
it('should support event handlers with extra parameters', () => {
138+
const handleFocus = spy((event, extra) => extra);
139+
const handleBlur = spy((event, ...params) => params);
140+
141+
const TextField = React.forwardRef<
142+
HTMLDivElement,
143+
{
144+
onFocus: (event: React.FocusEvent, ...params: any[]) => void;
145+
onBlur: (event: React.FocusEvent, ...params: any[]) => void;
146+
}
147+
>(function TextField(props, ref) {
148+
const { onFocus, onBlur, ...other } = props;
149+
return (
150+
<div ref={ref} {...other}>
151+
<input
152+
type="text"
153+
onFocus={(event) => onFocus(event, 'focus')}
154+
onBlur={(event) => onBlur(event, 'blur', 1)}
155+
/>
156+
</div>
157+
);
158+
});
159+
const { getByRole } = render(
160+
<Tooltip open title="test">
161+
<TextField onFocus={handleFocus} onBlur={handleBlur} />
162+
</Tooltip>,
163+
);
164+
const input = getByRole('textbox');
165+
166+
act(() => {
167+
input.focus();
168+
});
169+
170+
expect(handleFocus.callCount).to.equal(1);
171+
expect(handleFocus.returnValues[0]).to.equal('focus');
172+
173+
act(() => {
174+
input.blur();
175+
});
176+
177+
expect(handleBlur.callCount).to.equal(1);
178+
expect(handleBlur.returnValues[0]).to.deep.equal(['blur', 1]);
179+
});
180+
});
133181
});

packages/mui-joy/src/Tooltip/Tooltip.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ function composeMouseEventHandler(
179179
}
180180

181181
function composeFocusEventHandler(
182-
handler: (event: React.FocusEvent<HTMLElement>) => void,
183-
eventHandler: (event: React.FocusEvent<HTMLElement>) => void,
182+
handler: (event: React.FocusEvent<HTMLElement>, ...params: any[]) => void,
183+
eventHandler: (event: React.FocusEvent<HTMLElement>, ...params: any[]) => void,
184184
) {
185-
return (event: React.FocusEvent<HTMLElement>) => {
185+
return (event: React.FocusEvent<HTMLElement>, ...params: any[]) => {
186186
if (eventHandler) {
187-
eventHandler(event);
187+
eventHandler(event, ...params);
188188
}
189-
handler(event);
189+
handler(event, ...params);
190190
};
191191
}
192192
/**

0 commit comments

Comments
 (0)