-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat!: split apart checkbox checked and indeterminate props #1520
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 |
---|---|---|
|
@@ -137,8 +137,6 @@ exports[`<Checkbox /> Disabled story renders snapshot 1`] = ` | |
class="checkbox" | ||
> | ||
<input | ||
aria-checked="mixed" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need |
||
checked="" | ||
class="checkbox__input" | ||
disabled="" | ||
id=":rb:" | ||
|
@@ -157,8 +155,6 @@ exports[`<Checkbox /> Disabled story renders snapshot 1`] = ` | |
class="checkbox" | ||
> | ||
<input | ||
aria-checked="mixed" | ||
checked="" | ||
class="checkbox__input" | ||
id=":rc:" | ||
readonly="" | ||
|
@@ -205,11 +201,8 @@ exports[`<Checkbox /> Indeterminate story renders snapshot 1`] = ` | |
class="checkbox" | ||
> | ||
<input | ||
aria-checked="mixed" | ||
checked="" | ||
class="checkbox__input" | ||
id=":r6:" | ||
readonly="" | ||
type="checkbox" | ||
/> | ||
<label | ||
|
@@ -443,12 +436,9 @@ exports[`<Checkbox /> WithoutVisibleLabel story renders snapshot 1`] = ` | |
class="checkbox" | ||
> | ||
<input | ||
aria-checked="mixed" | ||
aria-label="a checkbox has no name" | ||
checked="" | ||
class="checkbox__input" | ||
id=":rf:" | ||
readonly="" | ||
type="checkbox" | ||
/> | ||
</div> | ||
|
@@ -479,9 +469,7 @@ exports[`<Checkbox /> WithoutVisibleLabel story renders snapshot 1`] = ` | |
class="checkbox" | ||
> | ||
<input | ||
aria-checked="mixed" | ||
aria-label="a checkbox has no name" | ||
checked="" | ||
class="checkbox__input" | ||
disabled="" | ||
id=":ri:" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { CheckboxInput } from './CheckboxInput'; | ||
|
||
it('forwards refs', () => { | ||
const someRef = React.createRef<HTMLInputElement>(); | ||
render(<CheckboxInput id="test" ref={someRef} />); | ||
expect(someRef.current).toBeInstanceOf(HTMLInputElement); | ||
}); | ||
|
||
describe('indeterminancy', () => { | ||
it('is indeterminate when "indeterminate" is true', () => { | ||
render(<CheckboxInput id="test" indeterminate />); | ||
const checkbox = screen.getByRole<HTMLInputElement>('checkbox'); | ||
expect(checkbox.indeterminate).toEqual(true); | ||
}); | ||
|
||
it('is not indeterminate when "indeterminate" is false', () => { | ||
render(<CheckboxInput id="test" indeterminate={false} />); | ||
const checkbox = screen.getByRole<HTMLInputElement>('checkbox'); | ||
expect(checkbox.indeterminate).toEqual(false); | ||
}); | ||
|
||
it('is not indeterminate when "indeterminate" is not present', () => { | ||
render(<CheckboxInput id="test" />); | ||
const checkbox = screen.getByRole<HTMLInputElement>('checkbox'); | ||
expect(checkbox.indeterminate).toEqual(false); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just also manually tested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sweet, thanks! |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import clsx from 'clsx'; | ||
import React from 'react'; | ||
import useForwardedRef from '../../util/useForwardedRef'; | ||
import styles from './CheckboxInput.module.css'; | ||
|
||
type CheckboxHTMLElementProps = Omit< | ||
|
@@ -9,11 +10,9 @@ type CheckboxHTMLElementProps = Omit< | |
|
||
export type CheckboxInputProps = CheckboxHTMLElementProps & { | ||
/** | ||
* Whether checkbox is checked. Defaults to false. | ||
* "indeterminate" can be used when a checkbox visually represents | ||
* a list of checkboxes that is "partially" checked. | ||
* Whether checkbox is checked. | ||
*/ | ||
checked?: boolean | 'indeterminate'; | ||
checked?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The breaking change => reducing the available options |
||
/** | ||
* Additional classnames passed in for styling. | ||
*/ | ||
|
@@ -22,6 +21,11 @@ export type CheckboxInputProps = CheckboxHTMLElementProps & { | |
* Checkbox ID. Used to connect the input with a label for accessibility purposes. | ||
*/ | ||
id: string; | ||
/** | ||
* Whether the checkbox is "indeterminate". Neither checked nor unchecked. The most common use | ||
* case for this is when a checkbox has sub-checkboxes, to represent a "partially checked" state. | ||
*/ | ||
indeterminate?: boolean; | ||
}; | ||
|
||
/** | ||
|
@@ -31,32 +35,24 @@ export type CheckboxInputProps = CheckboxHTMLElementProps & { | |
export const CheckboxInput = React.forwardRef< | ||
HTMLInputElement, | ||
CheckboxInputProps | ||
>(({ checked, className, disabled, ...other }, ref) => { | ||
// Make indeterminate checkbox visually match the colors of a checked state, but announce itself | ||
// as "mixed" to screen readers | ||
// | ||
// However, https://html.spec.whatwg.org/multipage/input.html#checkbox-state-(type=checkbox) | ||
// seems to consider `indeterminate` as a separate bit of information than `checked`. They can be | ||
// in all 4 combinations (only 3 visual states, though). | ||
// | ||
// Should we make them separate props? | ||
const checkedProps = | ||
checked === 'indeterminate' | ||
? { | ||
'aria-checked': 'mixed' as const, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we set Here we fix that (which unfortunately requires JS and jumping through a hoop). |
||
checked: true, | ||
jinlee93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
: { | ||
checked, | ||
}; | ||
>(({ checked, className, disabled, indeterminate, ...other }, ref) => { | ||
const forwardedRef = useForwardedRef(ref); | ||
|
||
// Make this checkbox indeterminate. Can only be done with JS for some reason. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See facebook/react#1798 for why React hasn't added an TL;DR: Browsers don't have an |
||
// See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes. | ||
React.useEffect(() => { | ||
if (forwardedRef.current) { | ||
forwardedRef.current.indeterminate = !!indeterminate; | ||
} | ||
}, [forwardedRef, indeterminate]); | ||
|
||
return ( | ||
<input | ||
checked={checked} | ||
className={clsx(className, styles['checkbox__input'])} | ||
disabled={disabled} | ||
ref={ref} | ||
ref={forwardedRef} | ||
type="checkbox" | ||
{...checkedProps} | ||
{...other} | ||
/> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import useForwardedRef from './useForwardedRef'; | ||
|
||
type Props = { | ||
callbackWithRef?: (ref: React.RefObject<HTMLElement>) => void; | ||
}; | ||
|
||
/** | ||
* Component for testing our forwarded ref. | ||
*/ | ||
const TestComponent = React.forwardRef<HTMLButtonElement, Props>( | ||
({ callbackWithRef }, ref) => { | ||
// Access the forwarded ref. | ||
const anotherRef = useForwardedRef(ref); | ||
|
||
// If present, pass the forwarded ref to a callback so we can do things with it in our tests. | ||
React.useEffect(() => { | ||
if (callbackWithRef) { | ||
callbackWithRef(anotherRef); | ||
} | ||
}, [anotherRef, callbackWithRef]); | ||
|
||
return <button ref={anotherRef}>☕</button>; | ||
}, | ||
); | ||
|
||
it('allows refs to be forwarded', () => { | ||
const someRef = React.createRef<HTMLButtonElement>(); | ||
render(<TestComponent ref={someRef} />); | ||
expect(screen.getByRole('button')).toBeInTheDocument(); | ||
expect(someRef.current).toBeInstanceOf(HTMLButtonElement); | ||
}); | ||
|
||
it('does not require a ref', () => { | ||
render(<TestComponent />); | ||
expect(screen.getByRole('button')).toBeInTheDocument(); | ||
}); | ||
|
||
it('allows forward refs to be intercepted and used by the component', () => { | ||
const someRef = React.createRef<HTMLButtonElement>(); | ||
|
||
render( | ||
<TestComponent | ||
// We've exposed the ref outside of the compnent via a callback for testing purposes. | ||
// Normally the component will do things with the ref internally. | ||
callbackWithRef={(ref) => { | ||
// Add a data attribute we can write assertions against. | ||
if (ref.current) { | ||
ref.current.dataset.coffee = '5'; | ||
} | ||
}} | ||
ref={someRef} | ||
/>, | ||
); | ||
|
||
expect(someRef.current?.dataset.coffee).toEqual('5'); | ||
expect(screen.getByRole('button').dataset.coffee).toEqual('5'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useRef, useEffect, type ForwardedRef, type RefObject } from 'react'; | ||
|
||
/** | ||
* Safely access a forwarded ref, which may or may not be present. | ||
*/ | ||
Comment on lines
+3
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
export default function useForwardedRef<T>(ref: ForwardedRef<T>): RefObject<T> { | ||
const innerRef = useRef<T>(null); | ||
|
||
// Keep the internal and forwarded refs in sync. | ||
useEffect(() => { | ||
if (!ref) { | ||
return; | ||
} else if (typeof ref === 'function') { | ||
ref(innerRef.current); | ||
} else { | ||
ref.current = innerRef.current; | ||
} | ||
}, [ref]); | ||
|
||
return innerRef; | ||
} |
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.
I like how simple this looks