Skip to content

Commit

Permalink
[joy-ui][Tooltip] Support event handlers with extra parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Feb 29, 2024
1 parent 3455c42 commit 14c47d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
50 changes: 49 additions & 1 deletion packages/mui-joy/src/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer } from '@mui-internal/test-utils';
import { spy } from 'sinon';
import { createRenderer, act } from '@mui-internal/test-utils';
import { unstable_capitalize as capitalize } from '@mui/utils';
import { PopperProps } from '@mui/base';
import { ThemeProvider } from '@mui/joy/styles';
Expand Down Expand Up @@ -130,4 +131,51 @@ describe('<Tooltip />', () => {
});
});
});

describe('focus', () => {
// 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<
HTMLDivElement,
{
onFocus: (event: React.FocusEvent, ...params: any[]) => void;
onBlur: (event: React.FocusEvent, ...params: any[]) => void;
}
>(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>
);
});
const { getByRole } = render(
<Tooltip open title="test">
<TextField onFocus={handleFocus} onBlur={handleBlur} />
</Tooltip>,
);
const input = 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]);
});
});
});
10 changes: 5 additions & 5 deletions packages/mui-joy/src/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ function composeMouseEventHandler(
}

function composeFocusEventHandler(
handler: (event: React.FocusEvent<HTMLElement>) => void,
eventHandler: (event: React.FocusEvent<HTMLElement>) => void,
handler: (event: React.FocusEvent<HTMLElement>, ...params: any[]) => void,
eventHandler: (event: React.FocusEvent<HTMLElement>, ...params: any[]) => void,
) {
return (event: React.FocusEvent<HTMLElement>) => {
return (event: React.FocusEvent<HTMLElement>, ...params: any[]) => {
if (eventHandler) {
eventHandler(event);
eventHandler(event, ...params);
}
handler(event);
handler(event, ...params);
};
}
/**
Expand Down

0 comments on commit 14c47d1

Please sign in to comment.