Skip to content

Commit

Permalink
fix(RadioTile): forward ref to input node (#13130)
Browse files Browse the repository at this point in the history
Forward ref to input node of RadioTile,
such that it behaves the same as other form inputs.

Co-authored-by: Francine Lucca <[email protected]>
  • Loading branch information
remolueoend and francinelucca authored Mar 7, 2023
1 parent 36f1bbb commit 21536a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 2 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5967,6 +5967,7 @@ Map {
},
},
"RadioTile" => Object {
"$$typeof": Symbol(react.forward_ref),
"defaultProps": Object {
"onChange": [Function],
"tabIndex": 0,
Expand Down Expand Up @@ -6012,6 +6013,7 @@ Map {
"type": "oneOfType",
},
},
"render": [Function],
},
"Row" => Object {
"propTypes": Object {
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/components/RadioTile/RadioTile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,13 @@ describe('RadioTile', () => {

expect(screen.getByRole('radio')).toHaveAttribute('value', 'standard');
});

it('should pass a given ref to the input element', () => {
const ref = React.createRef();
render(<RadioTile ref={ref} value="some test value" />);

expect(ref.current.type).toEqual('radio');
expect(ref.current.value).toEqual('some test value');
});
});
});
34 changes: 19 additions & 15 deletions packages/react/src/components/RadioTile/RadioTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ import { useFallbackId } from '../../internal/useId';
import { usePrefix } from '../../internal/usePrefix';
import deprecate from '../../prop-types/deprecate';

function RadioTile({
children,
className: customClassName,
disabled,
// eslint-disable-next-line no-unused-vars
light,
checked,
name,
value,
id,
onChange,
tabIndex,
...rest
}) {
const RadioTile = React.forwardRef(function RadioTile(
{
children,
className: customClassName,
disabled,
// eslint-disable-next-line no-unused-vars
light,
checked,
name,
value,
id,
onChange,
tabIndex,
...rest
},
ref
) {
const prefix = usePrefix();
const inputId = useFallbackId(id);
const className = cx(
Expand Down Expand Up @@ -65,6 +68,7 @@ function RadioTile({
tabIndex={!disabled ? tabIndex : null}
type="radio"
value={value}
ref={ref}
/>
<label {...rest} htmlFor={inputId} className={className}>
<span className={`${prefix}--tile__checkmark`}>
Expand All @@ -74,7 +78,7 @@ function RadioTile({
</label>
</>
);
}
});

RadioTile.propTypes = {
/**
Expand Down

0 comments on commit 21536a5

Please sign in to comment.