Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/proud-bears-study.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

fix: Checkbox indeterminate state now persists on click if the state didn't change
27 changes: 19 additions & 8 deletions packages/react/src/Checkbox/Checkbox.features.stories.tsx
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
import {useState} from 'react'
import Checkbox from '../Checkbox'
import FormControl from '../FormControl'
import {MarkGithubIcon} from '@primer/octicons-react'
Expand DownExpand Up@@ -43,11 +44,21 @@ export const WithCaption = () => {
)
}

export const Indeterminate = () => (
<form>
<FormControl>
<Checkbox value="default" indeterminate />
<FormControl.Label>Default label</FormControl.Label>
</FormControl>
</form>
)
export const Indeterminate = () => {
const [isIndeterminate, setIsIndeterminate] = useState(true)
return (
<form>
<FormControl>
<Checkbox
value="default"
indeterminate={isIndeterminate}
onChange={() => {
setIsIndeterminate(!isIndeterminate)
}}
checked={false}
/>
<FormControl.Label>Default label</FormControl.Label>
</FormControl>
</form>
)
}
5 changes: 5 additions & 0 deletions packages/react/src/Checkbox/Checkbox.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,11 @@ const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
const handleOnChange: ChangeEventHandler<HTMLInputElement> = e => {
checkboxGroupContext.onChange && checkboxGroupContext.onChange(e)
onChange && onChange(e)

if (indeterminate && checkboxRef.current) {
checkboxRef.current.indeterminate = true
checkboxRef.current.setAttribute('aria-checked', 'mixed')
}
}
const inputProps = {
type: 'checkbox',
Expand Down
Loading