-
Notifications
You must be signed in to change notification settings - Fork 662
Checkbox: replace useLayoutEffect with ref callback for indeterminate #7765
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@primer/react": patch | ||
| --- | ||
|
|
||
| Checkbox: replace useLayoutEffect with ref callback for setting indeterminate state |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import {clsx} from 'clsx' | ||
| import {useProvidedRefOrCreate} from '../hooks' | ||
| import React, {useContext, useEffect, type ChangeEventHandler, type InputHTMLAttributes, type ReactElement} from 'react' | ||
| import useLayoutEffect from '../utils/useIsomorphicLayoutEffect' | ||
| import {useMergedRefs} from '../hooks' | ||
| import React, {useContext, type ChangeEventHandler, type InputHTMLAttributes, type ReactElement} from 'react' | ||
| import type {FormValidationStatus} from '../utils/types/FormValidationStatus' | ||
| import {CheckboxGroupContext} from '../CheckboxGroup/CheckboxGroupContext' | ||
| import classes from './Checkbox.module.css' | ||
|
|
@@ -45,7 +44,19 @@ const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>( | |
| ref, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| ): ReactElement<any> => { | ||
| const checkboxRef = useProvidedRefOrCreate(ref as React.RefObject<HTMLInputElement>) | ||
| const setIndeterminate = React.useCallback( | ||
| (node: HTMLInputElement | null) => { | ||
| if (node) { | ||
| node.indeterminate = indeterminate || false | ||
| } | ||
| }, | ||
| // `checked` is intentionally included: browsers clear the indeterminate state | ||
| // when checked changes, so we need the callback to re-run to restore it. | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| [indeterminate, checked], | ||
| ) | ||
| const mergedRef = useMergedRefs(ref, setIndeterminate) | ||
|
|
||
| const checkboxGroupContext = useContext(CheckboxGroupContext) | ||
| const handleOnChange: ChangeEventHandler<HTMLInputElement> = e => { | ||
| checkboxGroupContext.onChange && checkboxGroupContext.onChange(e) | ||
|
|
@@ -54,37 +65,19 @@ const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>( | |
| const inputProps = { | ||
| type: 'checkbox', | ||
| disabled, | ||
| ref: checkboxRef, | ||
| ref: mergedRef, | ||
| checked: indeterminate ? false : checked, | ||
| defaultChecked, | ||
| required, | ||
| ['aria-required']: required ? ('true' as const) : ('false' as const), | ||
| ['aria-invalid']: validationStatus === 'error' ? ('true' as const) : ('false' as const), | ||
| ['aria-checked']: indeterminate ? ('mixed' as const) : undefined, | ||
| onChange: handleOnChange, | ||
| value, | ||
| name: value, | ||
| ...rest, | ||
|
Comment on lines
+74
to
78
|
||
| } | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (checkboxRef.current) { | ||
| checkboxRef.current.indeterminate = indeterminate || false | ||
| } | ||
| }, [indeterminate, checked, checkboxRef]) | ||
|
|
||
| useEffect(() => { | ||
| const {current: checkbox} = checkboxRef | ||
| if (!checkbox) { | ||
| return | ||
| } | ||
|
|
||
| if (indeterminate) { | ||
| checkbox.setAttribute('aria-checked', 'mixed') | ||
| } else { | ||
| checkbox.setAttribute('aria-checked', checkbox.checked ? 'true' : 'false') | ||
| } | ||
| }) | ||
| // @ts-expect-error inputProp needs a non nullable ref | ||
| return <input {...inputProps} className={clsx(className, sharedClasses.Input, classes.Checkbox)} /> | ||
| }, | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
eslint-disable-next-line react-hooks/exhaustive-depslooks unnecessary here because the dependency list is already explicit and complete. Keeping this suppression can hide real missing-dependency issues in future edits; consider removing the disable and leaving the explanatory comment aboutcheckedtriggering a rerun.