Skip to content

Commit d90df1b

Browse files
authored
fix: Make value required on AriaCheckboxGroupItemProps interface (#1337)
1 parent 7af788d commit d90df1b

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

packages/@react-aria/checkbox/test/useCheckboxGroup.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {AriaCheckboxGroupProps, AriaCheckboxProps} from '@react-types/checkbox';
13+
import {AriaCheckboxGroupItemProps, AriaCheckboxGroupProps} from '@react-types/checkbox';
1414
import {CheckboxGroupState, useCheckboxGroupState} from '@react-stately/checkbox';
1515
import React, {useRef} from 'react';
1616
import {render} from '@testing-library/react';
1717
import {useCheckboxGroup, useCheckboxGroupItem} from '../';
1818
import userEvent from '@testing-library/user-event';
1919

20-
function Checkbox({checkboxGroupState, ...props}: AriaCheckboxProps & { checkboxGroupState: CheckboxGroupState }) {
20+
function Checkbox({checkboxGroupState, ...props}: AriaCheckboxGroupItemProps & { checkboxGroupState: CheckboxGroupState }) {
2121
const ref = useRef<HTMLInputElement>();
2222
const {children} = props;
2323
const {inputProps} = useCheckboxGroupItem(props, checkboxGroupState, ref);
2424
return <label>{children}<input ref={ref} {...inputProps} /></label>;
2525
}
2626

27-
function CheckboxGroup({groupProps, checkboxProps}: {groupProps: AriaCheckboxGroupProps, checkboxProps: AriaCheckboxProps[]}) {
27+
function CheckboxGroup({groupProps, checkboxProps}: {groupProps: AriaCheckboxGroupProps, checkboxProps: AriaCheckboxGroupItemProps[]}) {
2828
const state = useCheckboxGroupState(groupProps);
2929
const {groupProps: checkboxGroupProps, labelProps} = useCheckboxGroup(groupProps, state);
3030
return (

packages/@react-spectrum/checkbox/src/Checkbox.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ function Checkbox(props: SpectrumCheckboxProps, ref: FocusableRef<HTMLLabelEleme
5050
// eslint-disable-next-line react-hooks/rules-of-hooks
5151
? useCheckboxGroupItem({
5252
...props,
53+
// Value is optional for standalone checkboxes, but required for CheckboxGroup items;
54+
// it's passed explicitly here to avoid typescript error (requires strictNullChecks disabled).
55+
value: props.value,
5356
// Only pass isRequired and validationState to react-aria if they came from
5457
// the props for this individual checkbox, and not from the group via context.
5558
isRequired: originalProps.isRequired,
@@ -68,6 +71,9 @@ function Checkbox(props: SpectrumCheckboxProps, ref: FocusableRef<HTMLLabelEleme
6871
console.warn(`${key} is unsupported on individual <Checkbox> elements within a <CheckboxGroup>. Please apply these props to the group instead.`);
6972
}
7073
}
74+
if (props.value == null) {
75+
console.warn('A <Checkbox> element within a <CheckboxGroup> requires a `value` property.');
76+
}
7177
}
7278

7379
return (

packages/@react-types/checkbox/src/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export interface AriaCheckboxGroupProps extends CheckboxGroupProps, DOMProps, Ar
7979
name?: string
8080
}
8181

82-
export interface AriaCheckboxGroupItemProps extends Omit<AriaCheckboxProps, 'isSelected' | 'defaultSelected'> {}
82+
export interface AriaCheckboxGroupItemProps extends Omit<AriaCheckboxProps, 'isSelected' | 'defaultSelected'> {
83+
value: string
84+
}
8385

8486
export interface SpectrumCheckboxProps extends AriaCheckboxProps, StyleProps {
8587
/**

0 commit comments

Comments
 (0)