Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[material-ui][Tooltip] Support event handlers with extra parameters
  • Loading branch information
LukasTy committed Feb 29, 2024
commit 3455c42ba855ba803a42ae5f6b1a851a8f6d6386
6 changes: 3 additions & 3 deletions packages/mui-material/src/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ export function testReset() {
}

function composeEventHandler(handler, eventHandler) {
return (event) => {
return (event, ...params) => {
if (eventHandler) {
eventHandler(event);
eventHandler(event, ...params);
}
handler(event);
handler(event, ...params);
};
}

Expand Down
39 changes: 39 additions & 0 deletions packages/mui-material/src/Tooltip/Tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,45 @@ describe('<Tooltip />', () => {
expect(handleFocus.callCount).to.equal(1);
expect(handleFocus.returned(input)).to.equal(true);
});

// https://github.com/mui/mui-x/issues/12248
it('should support event handlers with extra parameters', () => {
const handleFocus = spy((event, extra) => extra);
const handleBlur = spy((event, ...params) => params);

const TextField = React.forwardRef(function TextField(props, ref) {
const { onFocus, onBlur, ...other } = props;
return (
<div ref={ref} {...other}>
<input
type="text"
onFocus={(event) => onFocus(event, 'focus')}
onBlur={(event) => onBlur(event, 'blur', 1)}
/>
</div>
);
});
render(
<Tooltip open title="test">
<TextField onFocus={handleFocus} onBlur={handleBlur} variant="standard" />
</Tooltip>,
);
const input = screen.getByRole('textbox');

act(() => {
input.focus();
});

expect(handleFocus.callCount).to.equal(1);
expect(handleFocus.returnValues[0]).to.equal('focus');

act(() => {
input.blur();
});

expect(handleBlur.callCount).to.equal(1);
expect(handleBlur.returnValues[0]).to.deep.equal(['blur', 1]);
});
});

describe('warnings', () => {
Expand Down