Skip to content

Commit

Permalink
[TextField] fix running click event on disabled (mui#36892)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 committed Apr 25, 2023
1 parent 5b7e9c5 commit 9118505
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
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) {
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

0 comments on commit 9118505

Please sign in to comment.