Skip to content

Commit

Permalink
Fix internal state consistency of control cards in controlled mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gluxon committed Apr 18, 2024
1 parent daebef2 commit ee03cdb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/core/src/components/control-card/useCheckedControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import type { CheckedControlProps } from "../forms/controlProps";
* Keep track of a control's checked state in both controlled and uncontrolled modes
*/
export function useCheckedControl(props: CheckedControlProps) {
const [checked, setChecked] = React.useState(() => props.defaultChecked ?? false);
React.useEffect(() => {
if (props.checked !== undefined) {
setChecked(props.checked);
}
}, [props.checked]);
const [checkedStateForUncontrolledMode, setChecked] = React.useState(() => props.defaultChecked ?? false);

// If the checked prop is passed, this input is in "controlled mode" and
// should always reflect the value of the controlled prop. Any internal
// state tracked for "uncontrolled mode" should be ignored.
const checked = props.checked ?? checkedStateForUncontrolledMode;

const onChange = React.useCallback<React.ChangeEventHandler<HTMLInputElement>>(
e => {
setChecked(c => !c);
Expand Down

1 comment on commit ee03cdb

@svc-palantir-github
Copy link

Choose a reason for hiding this comment

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

Fix internal state consistency of control cards in controlled mode

Build artifact links for this commit: documentation | landing | table | demo

This is an automated comment from the deploy-preview CircleCI job.

Please sign in to comment.