Skip to content

Commit 0c28454

Browse files
authored
fix: [RAC] Checkbox applies data attributes to both the input and the label (#7151)
* applies data attributes to label (not input) * not to propagate the data attributes to inputProps
1 parent e9aa00a commit 0c28454

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

packages/react-aria-components/src/Checkbox.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import {AriaCheckboxGroupProps, AriaCheckboxProps, HoverEvents, mergeProps, useCheckbox, useCheckboxGroup, useCheckboxGroupItem, useFocusRing, useHover, VisuallyHidden} from 'react-aria';
1313
import {CheckboxContext} from './RSPContexts';
1414
import {CheckboxGroupState, useCheckboxGroupState, useToggleState} from 'react-stately';
15-
import {ContextValue, Provider, RACValidation, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot, useSlottedContext} from './utils';
15+
import {ContextValue, Provider, RACValidation, removeDataAttributes, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot, useSlottedContext} from './utils';
1616
import {FieldErrorContext} from './FieldError';
1717
import {filterDOMProps, mergeRefs, useObjectRef} from '@react-aria/utils';
1818
import {FormContext} from './Form';
@@ -180,7 +180,7 @@ function Checkbox(props: CheckboxProps, ref: ForwardedRef<HTMLLabelElement>) {
180180
let {labelProps, inputProps, isSelected, isDisabled, isReadOnly, isPressed, isInvalid} = groupState
181181
// eslint-disable-next-line react-hooks/rules-of-hooks
182182
? useCheckboxGroupItem({
183-
...props,
183+
...removeDataAttributes(props),
184184
// Value is optional for standalone checkboxes, but required for CheckboxGroup items;
185185
// it's passed explicitly here to avoid typescript error (requires ignore).
186186
// @ts-ignore
@@ -190,7 +190,7 @@ function Checkbox(props: CheckboxProps, ref: ForwardedRef<HTMLLabelElement>) {
190190
}, groupState, inputRef)
191191
// eslint-disable-next-line react-hooks/rules-of-hooks
192192
: useCheckbox({
193-
...props,
193+
...removeDataAttributes(props),
194194
children: typeof props.children === 'function' ? true : props.children,
195195
validationBehavior
196196
// eslint-disable-next-line react-hooks/rules-of-hooks
@@ -204,7 +204,6 @@ function Checkbox(props: CheckboxProps, ref: ForwardedRef<HTMLLabelElement>) {
204204
});
205205

206206
let renderProps = useRenderProps({
207-
// TODO: should data attrs go on the label or on the <input>? useCheckbox passes them to the input...
208207
...props,
209208
defaultClassName: 'react-aria-Checkbox',
210209
values: {

packages/react-aria-components/test/Checkbox.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('Checkbox', () => {
3737
let {getByRole} = render(<Checkbox data-foo="bar">Test</Checkbox>);
3838
let checkbox = getByRole('checkbox').closest('label');
3939
expect(checkbox).toHaveAttribute('data-foo', 'bar');
40+
expect(getByRole('checkbox')).not.toHaveAttribute('data-foo', 'bar');
4041
});
4142

4243
it('should support slot', () => {

0 commit comments

Comments
 (0)