Skip to content

Commit b2327a5

Browse files
authored
RAC Radio and Checkbox improvements (#4780)
1 parent 986d093 commit b2327a5

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function CheckboxGroup(props: CheckboxGroupProps, ref: ForwardedRef<HTMLDivEleme
137137
<Provider
138138
values={[
139139
[InternalCheckboxGroupContext, state],
140-
[LabelContext, {...labelProps, ref: labelRef}],
140+
[LabelContext, {...labelProps, ref: labelRef, elementType: 'span'}],
141141
[TextContext, {
142142
slots: {
143143
description: descriptionProps,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import React, {createContext, ForwardedRef, forwardRef, useState} from 'react';
1919
import {TextContext} from './Text';
2020

2121
export interface RadioGroupProps extends Omit<AriaRadioGroupProps, 'children' | 'label' | 'description' | 'errorMessage'>, RenderProps<RadioGroupRenderProps>, SlotProps {}
22-
export interface RadioProps extends Omit<AriaRadioProps, 'children'>, RenderProps<RadioRenderProps> {}
22+
export interface RadioProps extends Omit<AriaRadioProps, 'children'>, RenderProps<RadioRenderProps>, SlotProps {}
2323

2424
export interface RadioGroupRenderProps {
2525
/**
@@ -102,6 +102,7 @@ export interface RadioRenderProps {
102102
}
103103

104104
export const RadioGroupContext = createContext<ContextValue<RadioGroupProps, HTMLDivElement>>(null);
105+
export const RadioContext = createContext<ContextValue<RadioProps, HTMLInputElement>>(null);
105106
let InternalRadioContext = createContext<RadioGroupState | null>(null);
106107

107108
function RadioGroup(props: RadioGroupProps, ref: ForwardedRef<HTMLDivElement>) {
@@ -135,7 +136,7 @@ function RadioGroup(props: RadioGroupProps, ref: ForwardedRef<HTMLDivElement>) {
135136
<Provider
136137
values={[
137138
[InternalRadioContext, state],
138-
[LabelContext, {...labelProps, ref: labelRef}],
139+
[LabelContext, {...labelProps, ref: labelRef, elementType: 'span'}],
139140
[TextContext, {
140141
slots: {
141142
description: descriptionProps,
@@ -150,6 +151,7 @@ function RadioGroup(props: RadioGroupProps, ref: ForwardedRef<HTMLDivElement>) {
150151
}
151152

152153
function Radio(props: RadioProps, ref: ForwardedRef<HTMLInputElement>) {
154+
[props, ref] = useContextProps(props, ref, RadioContext);
153155
let state = React.useContext(InternalRadioContext)!;
154156
let domRef = useObjectRef(ref);
155157
let {inputProps, isSelected, isDisabled, isPressed: isPressedKeyboard} = useRadio({

packages/react-aria-components/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export {OverlayArrow} from './OverlayArrow';
3838
export {Popover, PopoverContext} from './Popover';
3939
export {ProgressBar, ProgressBarContext} from './ProgressBar';
4040
export {Provider, useContextProps} from './utils';
41-
export {RadioGroup, Radio, RadioGroupContext} from './RadioGroup';
41+
export {RadioGroup, Radio, RadioGroupContext, RadioContext} from './RadioGroup';
4242
export {SearchField, SearchFieldContext} from './SearchField';
4343
export {Select, SelectValue, SelectContext} from './Select';
4444
export {Separator, SeparatorContext} from './Separator';

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

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

13-
import {Button, Dialog, DialogTrigger, Label, Modal, Radio, RadioGroup, RadioGroupContext, Text} from '../';
13+
import {Button, Dialog, DialogTrigger, Label, Modal, Radio, RadioContext, RadioGroup, RadioGroupContext, Text} from '../';
1414
import {fireEvent, render, within} from '@react-spectrum/test-utils';
1515
import React from 'react';
1616
import userEvent from '@testing-library/user-event';
@@ -100,15 +100,22 @@ describe('RadioGroup', () => {
100100
});
101101

102102
it('should support slot', () => {
103-
let {getByRole} = render(
103+
let {getByRole, getAllByRole} = render(
104104
<RadioGroupContext.Provider value={{slots: {test: {'aria-label': 'test'}}}}>
105-
<TestRadioGroup groupProps={{slot: 'test'}} />
105+
<RadioContext.Provider value={{'data-test': 'test'}}>
106+
<TestRadioGroup groupProps={{slot: 'test'}} />
107+
</RadioContext.Provider>
106108
</RadioGroupContext.Provider>
107109
);
108110

109111
let group = getByRole('radiogroup');
110112
expect(group).toHaveAttribute('slot', 'test');
111113
expect(group).toHaveAttribute('aria-label', 'test');
114+
115+
let radios = getAllByRole('radio');
116+
for (let radio of radios) {
117+
expect(radio).toHaveAttribute('data-test', 'test');
118+
}
112119
});
113120

114121
it('should support hover', () => {

0 commit comments

Comments
 (0)