( props: AriaCheckboxProps, state: ToggleState, inputRef: RefObject<HTMLInputElement | null> )
| 60 | * @param inputRef - A ref for the HTML input element. |
| 61 | */ |
| 62 | export function useCheckbox( |
| 63 | props: AriaCheckboxProps, |
| 64 | state: ToggleState, |
| 65 | inputRef: RefObject<HTMLInputElement | null> |
| 66 | ): CheckboxAria { |
| 67 | let { |
| 68 | labelProps, |
| 69 | inputProps, |
| 70 | descriptionProps, |
| 71 | errorMessageProps, |
| 72 | isSelected, |
| 73 | isPressed, |
| 74 | isDisabled, |
| 75 | isReadOnly, |
| 76 | isInvalid, |
| 77 | validationErrors, |
| 78 | validationDetails |
| 79 | } = useToggle(props, state, inputRef); |
| 80 | |
| 81 | let {isIndeterminate} = props; |
| 82 | useEffect(() => { |
| 83 | // indeterminate is a property, but it can only be set via javascript |
| 84 | // https://css-tricks.com/indeterminate-checkboxes/ |
| 85 | if (inputRef.current) { |
| 86 | inputRef.current.indeterminate = !!isIndeterminate; |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | return { |
| 91 | labelProps: mergeProps( |
| 92 | labelProps, |
| 93 | useMemo( |
| 94 | () => ({ |
| 95 | // Prevent label from being focused when mouse down on it. |
| 96 | // Note, this does not prevent the input from being focused in the `click` event. |
| 97 | onMouseDown: e => e.preventDefault() |
| 98 | }), |
| 99 | [] |
| 100 | ) |
| 101 | ), |
| 102 | inputProps, |
| 103 | descriptionProps, |
| 104 | errorMessageProps, |
| 105 | isSelected, |
| 106 | isPressed, |
| 107 | isDisabled, |
| 108 | isReadOnly, |
| 109 | isInvalid, |
| 110 | validationErrors, |
| 111 | validationDetails |
| 112 | }; |
| 113 | } |
no test coverage detected