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

[SwitchBase] Ignore focus and blur events #18303

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 0 additions & 32 deletions packages/material-ui/src/internal/SwitchBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {
inputProps,
inputRef,
name,
onBlur,
onChange,
onFocus,
readOnly,
required,
tabIndex,
Expand All @@ -58,26 +56,6 @@ const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {

const muiFormControl = useFormControl();

const handleFocus = event => {
if (onFocus) {
onFocus(event);
}

if (muiFormControl && muiFormControl.onFocus) {
muiFormControl.onFocus(event);
}
};

const handleBlur = event => {
if (onBlur) {
onBlur(event);
}

if (muiFormControl && muiFormControl.onBlur) {
muiFormControl.onBlur(event);
}
};

const handleInputChange = event => {
const checked = event.target.checked;

Expand Down Expand Up @@ -115,8 +93,6 @@ const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {
disabled={disabled}
tabIndex={null}
role={undefined}
onFocus={handleFocus}
onBlur={handleBlur}
ref={ref}
{...other}
>
Expand Down Expand Up @@ -194,21 +170,13 @@ SwitchBase.propTypes = {
* @ignore
*/
name: PropTypes.string,
/**
* @ignore
*/
onBlur: PropTypes.func,
/**
* Callback fired when the state is changed.
*
* @param {object} event The event source of the callback.
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
*/
onChange: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* It prevents the user from changing the value of the field
* (not from interacting with the field).
Expand Down
38 changes: 1 addition & 37 deletions packages/material-ui/src/internal/SwitchBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import describeConformance from '../test-utils/describeConformance';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import { createClientRender } from 'test/utils/createClientRender';
import SwitchBase from './SwitchBase';
import FormControl, { useFormControl } from '../FormControl';
import FormControl from '../FormControl';
import IconButton from '../IconButton';

const shouldSuccessOnce = name => func => () => {
Expand Down Expand Up @@ -314,42 +314,6 @@ describe('<SwitchBase />', () => {
});
});

describe('focus/blur', () => {
it('forwards focus/blur events and notifies the FormControl', () => {
function FocusMonitor(props) {
const { focused } = useFormControl();

return <span {...props}>focused: {String(focused)}</span>;
}
const handleBlur = spy();
const handleFocus = spy();
const { getByRole, getByTestId } = render(
<FormControl>
<FocusMonitor data-testid="focus-monitor" />
<SwitchBase
data-testid="root"
onBlur={handleBlur}
onFocus={handleFocus}
icon="unchecked"
checkedIcon="checked"
type="checkbox"
/>
</FormControl>,
);
const checkbox = getByRole('checkbox');

checkbox.focus();

expect(getByTestId('focus-monitor')).to.have.text('focused: true');
expect(handleFocus.callCount).to.equal(1);

checkbox.blur();

expect(getByTestId('focus-monitor')).to.have.text('focused: false');
expect(handleBlur.callCount).to.equal(1);
});
});

describe('check transitioning between controlled states throws errors', () => {
beforeEach(() => {
consoleErrorMock.spy();
Expand Down