Skip to content

Commit 6f8ba67

Browse files
fix: RadioGroup should not validate on form reset (#8212)
Co-authored-by: Robert Snow <[email protected]>
1 parent 3285e6b commit 6f8ba67

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {FormContext} from './Form';
1818
import {forwardRefType, RefObject} from '@react-types/shared';
1919
import {LabelContext} from './Label';
2020
import {RadioGroupState, useRadioGroupState} from 'react-stately';
21-
import React, {createContext, ForwardedRef, forwardRef} from 'react';
21+
import React, {createContext, ForwardedRef, forwardRef, useMemo} from 'react';
2222
import {TextContext} from './Text';
2323

2424
export interface RadioGroupProps extends Omit<AriaRadioGroupProps, 'children' | 'label' | 'description' | 'errorMessage' | 'validationState' | 'validationBehavior'>, RACValidation, RenderProps<RadioGroupRenderProps>, SlotProps {}
@@ -186,7 +186,7 @@ export const Radio = /*#__PURE__*/ (forwardRef as forwardRefType)(function Radio
186186
} = props;
187187
[props, ref] = useContextProps(otherProps, ref, RadioContext);
188188
let state = React.useContext(RadioGroupStateContext)!;
189-
let inputRef = useObjectRef(mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null));
189+
let inputRef = useObjectRef(useMemo(() => mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null), [userProvidedInputRef, props.inputRef]));
190190
let {labelProps, inputProps, isSelected, isDisabled, isPressed} = useRadio({
191191
...removeDataAttributes<RadioProps>(props),
192192
// ReactNode type doesn't allow function children.

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

Lines changed: 20 additions & 1 deletion
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, ModalOverlay, Radio, RadioGroup} from 'react-aria-components';
13+
import {Button, Dialog, DialogTrigger, FieldError, Form, Label, Modal, ModalOverlay, Radio, RadioGroup} from 'react-aria-components';
1414
import React, {useState} from 'react';
1515
import styles from '../example/index.css';
1616

@@ -96,3 +96,22 @@ export const RadioGroupInDialogExample = () => {
9696
</DialogTrigger>
9797
);
9898
};
99+
100+
export const RadioGroupSubmitExample = () => {
101+
return (
102+
<Form>
103+
<RadioGroup
104+
className={styles.radiogroup}
105+
data-testid="radio-group-example"
106+
isRequired>
107+
<Label>Favorite pet</Label>
108+
<Radio className={styles.radio} value="dogs" data-testid="radio-dog">Dog</Radio>
109+
<Radio className={styles.radio} value="cats">Cat</Radio>
110+
<Radio className={styles.radio} value="dragon">Dragon</Radio>
111+
<FieldError className={styles.errorMessage} />
112+
</RadioGroup>
113+
<Button type="submit">Submit</Button>
114+
<Button type="reset">Reset</Button>
115+
</Form>
116+
);
117+
};

0 commit comments

Comments
 (0)