Skip to content

Commit

Permalink
[TextField] Fix to handle onClick on root element (mui#38072)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Aug 14, 2023
1 parent 2bfecd8 commit 113429c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/mui-material/src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
InputMore['aria-describedby'] = undefined;
}

const handleClick = (event) => {
if (!disabled && onClick) {
// The `onClick` is registered both on the root and the input elements.
// Without stopping the propagation, the event could be triggered twice.
event.stopPropagation();
onClick(event);
}
};

const id = useId(idOverride);
const helperTextId = helperText && id ? `${id}-helper-text` : undefined;
const inputLabelId = label && id ? `${id}-label` : undefined;
Expand All @@ -170,7 +179,7 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
onClick={onClick}
onClick={handleClick}
placeholder={placeholder}
inputProps={inputProps}
{...InputMore}
Expand All @@ -189,6 +198,7 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
color={color}
variant={variant}
ownerState={ownerState}
onClick={handleClick}
{...other}
>
{label != null && label !== '' && (
Expand Down
9 changes: 9 additions & 0 deletions packages/mui-material/src/TextField/TextField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,13 @@ describe('<TextField />', () => {
expect(getByRole('button')).toHaveAccessibleDescription('Foo bar');
});
});

it('should trigger `onClick` only once', () => {
const handleClick = spy();
const { getByRole } = render(
<TextField variant="outlined" label="Test" onClick={handleClick} />,
);
fireEvent.click(getByRole('textbox'));
expect(handleClick.callCount).to.equal(1);
});
});
17 changes: 17 additions & 0 deletions test/e2e/fixtures/TextField/TextFieldWithOnClick.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function TextFieldWithOnClick() {
const [isClicked, setIsClicked] = React.useState(false);
return (
<TextField
id="outlined-basic"
label="Outlined"
error={isClicked}
variant="outlined"
onClick={() => {
setIsClicked(true);
}}
/>
);
}
10 changes: 10 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,14 @@ describe('e2e', () => {
expect(pageErrors.length).to.equal(0);
});
});

describe('<TextField />', () => {
it('should handle `onClick` when clicking on the focused label position', async () => {
await renderFixture('TextField/TextFieldWithOnClick');

// execute the click on the focused label position
await page.getByRole('textbox').click({ position: { x: 10, y: 10 } });
await page.waitForSelector('.MuiInputBase-root.Mui-error');
});
});
});

0 comments on commit 113429c

Please sign in to comment.