Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/react-aria-components/src/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {HoverEvents} from '@react-types/shared';
import {LabelContext} from './Label';
import {mergeProps} from 'react-aria/mergeProps';
import {mergeRefs} from 'react-aria/mergeRefs';
import React, {createContext, ForwardedRef, forwardRef, useContext, useMemo} from 'react';
import React, {createContext, ForwardedRef, forwardRef, Ref, useContext, useMemo} from 'react';
import {TextContext} from './Text';
import {useFocusRing} from 'react-aria/useFocusRing';
import {useHover} from 'react-aria/useHover';
Expand Down Expand Up @@ -89,7 +89,7 @@ export interface CheckboxProps
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>;
inputRef?: Ref<HTMLInputElement | null>;
}

export interface CheckboxFieldProps
Expand All @@ -109,7 +109,7 @@ export interface CheckboxFieldProps
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>;
inputRef?: Ref<HTMLInputElement | null>;
}

export interface CheckboxButtonProps
Expand Down Expand Up @@ -429,7 +429,7 @@ export const CheckboxField = /*#__PURE__*/ (forwardRef as forwardRefType)(functi

function useCheckboxAria(
props: CheckboxProps | CheckboxFieldProps,
userProvidedInputRef: RefObject<HTMLInputElement | null> | null
userProvidedInputRef: Ref<HTMLInputElement | null> | null
): [CheckboxAria, RefObject<HTMLInputElement | null>] {
let {validationBehavior: formValidationBehavior} = useSlottedContext(FormContext) || {};
let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
Expand Down
6 changes: 3 additions & 3 deletions packages/react-aria-components/src/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {LabelContext} from './Label';
import {mergeProps} from 'react-aria/mergeProps';
import {mergeRefs} from 'react-aria/mergeRefs';
import {RadioGroupState, useRadioGroupState} from 'react-stately/useRadioGroupState';
import React, {createContext, ForwardedRef, forwardRef, useContext, useMemo} from 'react';
import React, {createContext, ForwardedRef, forwardRef, Ref, useContext, useMemo} from 'react';
import {SelectionIndicatorContext} from './SelectionIndicator';
import {SharedElementTransition} from './SharedElementTransition';
import {TextContext} from './Text';
Expand Down Expand Up @@ -89,7 +89,7 @@ export interface RadioProps
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>;
inputRef?: Ref<HTMLInputElement | null>;
}

export interface RadioFieldProps
Expand All @@ -108,7 +108,7 @@ export interface RadioFieldProps
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>;
inputRef?: Ref<HTMLInputElement | null>;
}

export interface RadioButtonProps
Expand Down
16 changes: 11 additions & 5 deletions packages/react-aria-components/src/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {forwardRefType, GlobalDOMAttributes, RefObject} from '@react-types/share
import {HoverEvents} from '@react-types/shared';
import {mergeProps} from 'react-aria/mergeProps';
import {mergeRefs} from 'react-aria/mergeRefs';
import React, {createContext, ForwardedRef, forwardRef, useContext} from 'react';
import React, {createContext, ForwardedRef, forwardRef, Ref, useContext, useMemo} from 'react';
import {TextContext} from './Text';
import {ToggleState, useToggleState} from 'react-stately/useToggleState';
import {useFocusRing} from 'react-aria/useFocusRing';
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface SwitchProps
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>;
inputRef?: Ref<HTMLInputElement | null>;
}

export interface SwitchFieldProps
Expand All @@ -85,7 +85,7 @@ export interface SwitchFieldProps
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>;
inputRef?: Ref<HTMLInputElement | null>;
}

export interface SwitchButtonProps
Expand Down Expand Up @@ -225,7 +225,10 @@ export const Switch = /*#__PURE__*/ (forwardRef as forwardRefType)(function Swit
let {inputRef: userProvidedInputRef = null, ...otherProps} = props;
[props, ref] = useContextProps(otherProps, ref, SwitchContext);
let inputRef = useObjectRef(
mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null)
useMemo(
() => mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null),
[userProvidedInputRef, props.inputRef]
)
);
let state = useToggleState(props);
let aria = useSwitch(
Expand Down Expand Up @@ -276,7 +279,10 @@ export const SwitchField = /*#__PURE__*/ (forwardRef as forwardRefType)(function
let {validationBehavior: formValidationBehavior} = useSlottedContext(FormContext) || {};
let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
let inputRef = useObjectRef(
mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null)
useMemo(
() => mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null),
[userProvidedInputRef, props.inputRef]
)
);
let state = useToggleState(props);
let aria = useSwitch(
Expand Down
9 changes: 9 additions & 0 deletions packages/react-aria-components/test/Checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ describe.each(['Checkbox', 'CheckboxField'])('%s', comp => {
expect(inputRef.current).toBe(getByRole('checkbox'));
});

it('should support callback ref', () => {
let cleanup = jest.fn();
let onRef = jest.fn(() => cleanup);
let {getByRole, unmount} = render(<Checkbox inputRef={onRef}>Test</Checkbox>);
expect(onRef).toHaveBeenCalledWith(getByRole('checkbox'));
unmount();
expect(cleanup).toHaveBeenCalledTimes(1);
});

it('should support and merge input ref on context', () => {
let inputRef = React.createRef();
let contextInputRef = React.createRef();
Expand Down
17 changes: 17 additions & 0 deletions packages/react-aria-components/test/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,23 @@ describe.each(['RadioGroup', 'RadioField'])('%s', comp => {
expect(inputRef.current).toBe(radio);
});

it('should support callback ref', () => {
let cleanup = jest.fn();
let onRef = jest.fn(() => cleanup);
let {getByRole, unmount} = render(
<RadioGroup>
<Label>Test</Label>
<Radio inputRef={onRef} value="a">
A
</Radio>
</RadioGroup>
);
let radio = getByRole('radio');
expect(onRef).toHaveBeenCalledWith(radio);
unmount();
expect(cleanup).toHaveBeenCalledTimes(1);
});

it('should support and merge input ref on context', () => {
let inputRef = React.createRef();
let contextInputRef = React.createRef();
Expand Down
9 changes: 9 additions & 0 deletions packages/react-aria-components/test/Switch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ describe.each(['Switch', 'SwitchField'])('%s', comp => {
expect(inputRef.current).toBe(getByRole('switch'));
});

it('should support callback ref', () => {
let cleanup = jest.fn();
let onRef = jest.fn(() => cleanup);
let {getByRole, unmount} = render(<Switch inputRef={onRef}>Test</Switch>);
expect(onRef).toHaveBeenCalledWith(getByRole('switch'));
unmount();
expect(cleanup).toHaveBeenCalledTimes(1);
});

it('should support and merge input ref on context', () => {
let inputRef = React.createRef();
let contextInputRef = React.createRef();
Expand Down