Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextField] Fix running click event on disabled #36892

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions packages/mui-material/src/InputBase/InputBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ const InputBase = React.forwardRef(function InputBase(inProps, ref) {
if (inputRef.current && event.currentTarget === event.target) {
inputRef.current.focus();
}

if (onClick) {
if (onClick && !fcs.disabled) {
Comment on lines -443 to +442
Copy link
Member

@oliviertassinari oliviertassinari Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It's not clear if it's the desired behavior:

export default function App() {
  return (
    <div
      onClick={() => {
        console.log("root");
      }}
    >
      <input
        disabled
        onClick={() => {
          console.log("input");
        }}
      />
    </div>
  );
}

https://codesandbox.io/s/amazing-banzai-xc225m?file=/src/App.js

Copy link
Member

@mj12albert mj12albert Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 This looked ok to me as a plain <input> doesn't fire onClick when it's disabled: https://codesandbox.io/s/async-https-n9x4y3-onclick-disabled-n9x4y3 @oliviertassinari

Copy link
Member

@oliviertassinari oliviertassinari Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there can be startAdornment, endAdornment, and renderSuffix. IMHO, we shouldn't try to make InputBase behave like an input because it's not: it's a root > input like structure. Instead, if we were to stay close to the normal DOM behavior we would avoid surprises for people that are familiar with the platform behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't try to make InputBase behave like an input because it's not

OK this makes sense 👌 will try to find a fix within Autocomplete

onClick(event);
}
};
Expand Down
4 changes: 4 additions & 0 deletions packages/mui-material/src/TextField/TextField.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export interface BaseTextFieldProps
name?: string;
onBlur?: InputBaseProps['onBlur'];
onFocus?: StandardInputProps['onFocus'];
/**
* @ignore
*/
onClick?: InputBaseProps['onClick'];
/**
* The short hint displayed in the `input` before the user enters a value.
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-material/src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
name,
onBlur,
onChange,
onClick,
onFocus,
placeholder,
required = false,
Expand Down Expand Up @@ -168,6 +169,7 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
onClick={onClick}
placeholder={placeholder}
inputProps={inputProps}
{...InputMore}
Expand Down Expand Up @@ -345,6 +347,10 @@ TextField.propTypes /* remove-proptypes */ = {
* You can pull out the new value by accessing `event.target.value` (string).
*/
onChange: PropTypes.func,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* @ignore
*/
Expand Down
19 changes: 18 additions & 1 deletion packages/mui-material/src/TextField/TextField.test.js
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, describeConformance } from 'test/utils';
import { spy } from 'sinon';
import { createRenderer, describeConformance, fireEvent } from 'test/utils';
import FormControl from '@mui/material/FormControl';
import { inputBaseClasses } from '@mui/material/InputBase';
import MenuItem from '@mui/material/MenuItem';
Expand Down Expand Up @@ -154,6 +155,22 @@ describe('<TextField />', () => {
});
});

describe('prop: disabled', () => {
it('should not run click event when disabled', () => {
const handleClick = spy();
const { getByRole } = render(<TextField disabled onClick={handleClick} />);
fireEvent.click(getByRole('textbox'));
expect(handleClick.callCount).to.equal(0);
});

it('should not run click event when disabled and when onClick prop is set through InputProps', () => {
const handleClick = spy();
const { getByRole } = render(<TextField disabled InputProps={{ onClick: handleClick }} />);
fireEvent.click(getByRole('textbox'));
expect(handleClick.callCount).to.equal(0);
});
});

describe('prop: select', () => {
it('can render a <select /> when `native`', () => {
const currencies = [
Expand Down