Skip to content

Commit

Permalink
[material-ui][Tooltip] Support event handlers with extra parameters (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Mar 13, 2024
1 parent 7ade0bd commit 9e97e73
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 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
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 @@ -224,11 +224,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 @@ -963,6 +963,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

0 comments on commit 9e97e73

Please sign in to comment.