-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add
indeterminate
prop to <Checkbox> that's separate from `c…
…hecked` https://html.spec.whatwg.org/multipage/input.html#checkbox-state-(type=checkbox) considers indeterminate to be a separate thing from a checkbox's checked state. In fact, it calls out that "The indeterminate IDL attribute only gives the appearance of a third state." Basically you can end up with all 4 combinatinos of checked and indeterminate, although visually there are only 3 states (both indeterminate states look the same). We can also see clearly at https://codepen.io/ahuth/pen/MWqmwyv that browsers behave this way.
- Loading branch information
Showing
7 changed files
with
160 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
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; | ||
} |