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

[Checkbox][Switch] Remove checked argument from onChange #25871

Merged
merged 5 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions docs/src/pages/guides/migration-v4/migration-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,21 @@ As the core components use emotion as a styled engine, the props used by emotion
</SvgIcon>
```

### Switch

- Remove the second argument from `onChange`. You can pull out the checked state by accessing `event.target.checked`.

```diff
function MySwitch() {
- const handleChange = (event: any, checked: boolean) => {
+ const handleChange = (event: any) => {
m4theushw marked this conversation as resolved.
Show resolved Hide resolved
+ const checked = event.target.checked;
};

return <Switch onChange={handleChange} />;
}
```

### Table

- The customization of the table pagination's actions labels must be done with the `getItemAriaLabel` prop. This increases consistency with the `Pagination` component.
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/internal/SwitchBase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface SwitchBaseProps
* You can pull out the new value by accessing `event.target.value` (string).
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
*/
onChange?: (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void;
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
readOnly?: boolean;
/**
* If `true`, the `input` element is required.
Expand Down
7 changes: 2 additions & 5 deletions packages/material-ui/src/internal/SwitchBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,10 @@ const SwitchBase = React.forwardRef(function SwitchBase(props, ref) {
return;
}

const newChecked = event.target.checked;

setCheckedState(newChecked);
setCheckedState(event.target.checked);

if (onChange) {
// TODO v5: remove the second argument.
onChange(event, newChecked);
onChange(event);
}
};

Expand Down
5 changes: 3 additions & 2 deletions packages/material-ui/src/internal/SwitchBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('<SwitchBase />', () => {
it('should call onChange when controlled', () => {
const checked = true;
const handleChange = spy();
const { getByRole } = render(
const { getByRole, setProps } = render(
<SwitchBase
icon="unchecked"
checkedIcon="checked"
Expand All @@ -207,9 +207,10 @@ describe('<SwitchBase />', () => {
);

getByRole('checkbox').click();
setProps({ checked: false });
Copy link
Member Author

Choose a reason for hiding this comment

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

We're relaying on a reference to the target now. If the checked prop is not updated, event.target.checked will be true again in next tick.

Copy link
Member

@eps1lon eps1lon Apr 23, 2021

Choose a reason for hiding this comment

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

Good call pointing this out. The click() was missing an act() i.e. flushing of effects. The setProps was implicitly doing that.

The other problem is that the test was no longer testing the click() but rather setProps({ checked: false }). I changed the test so that we actually test `onChange={(event) => setChecked(event.target.checked)}: bacce0c


expect(handleChange.callCount).to.equal(1);
expect(handleChange.firstCall.args[1]).to.equal(!checked);
expect(handleChange.firstCall.args[0].target.checked).to.equal(!checked);
});

it('should not change checkbox state when event is default prevented', () => {
Expand Down