Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(10343): Adds support for 'required' prop on RadioTile & TileGroup #16237

9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,15 @@
"contributions": [
"code"
]
},
{
"login": "2nikhiltom",
"name": "Nikhil Tomar",
"avatar_url": "https://avatars.githubusercontent.com/2nikhiltom?v=4",
"profile": "https://github.com/2nikhiltom",
"contributions": [
"code"
]
}
],
"commitConvention": "none"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,12 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
<td align="center"><a href="https://cuppajoey.com/"><img src="https://avatars.githubusercontent.com/u/14837881?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Joseph Schultz</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=cuppajoey" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/anjaly0606"><img src="https://avatars.githubusercontent.com/u/99959496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>anjaly0606</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=anjaly0606" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/jesnajoseijk"><img src="https://avatars.githubusercontent.com/u/38346258?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jesnajoseijk</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=jesnajoseijk" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/Jawahars"><img src="https://avatars.githubusercontent.com/u/4353146?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jawahar S</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=Jawahars" title="Code">💻</a></td>

</tr>
<tr>
<td align="center"><a href="https://github.com/Jawahars"><img src="https://avatars.githubusercontent.com/u/4353146?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jawahar S</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=Jawahars" title="Code">💻</a></td>
<td align="center"><a href="https://hollyos.com/"><img src="https://avatars.githubusercontent.com/u/4097509?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Holly Springsteen</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=hollyos" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/2nikhiltom"><img src="https://avatars.githubusercontent.com/2nikhiltom?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nikhil Tomar</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=2nikhiltom" title="Code">💻</a></td>
</tr>
</table>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6591,6 +6591,9 @@ Map {
"onChange": Object {
"type": "func",
},
"required": Object {
"type": "bool",
},
"tabIndex": Object {
"type": "number",
},
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 @@ -123,5 +123,13 @@ describe('RadioTile', () => {
expect(ref.current.type).toEqual('radio');
expect(ref.current.value).toEqual('some test value');
});
it('should pass "required" prop to the input element', () => {
render(
<RadioTile required value="some test value">
Option 1
</RadioTile>
);
expect(screen.getByRole('radio')).toHaveAttribute('required');
});
});
});
12 changes: 12 additions & 0 deletions packages/react/src/components/RadioTile/RadioTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export interface RadioTileProps {
* Specify the value of the underlying `<input>`.
*/
value: string | number;

/**
* `true` to specify if the input is required.
*/
required?: boolean;
}

const RadioTile = React.forwardRef(function RadioTile(
Expand All @@ -95,6 +100,7 @@ const RadioTile = React.forwardRef(function RadioTile(
id,
onChange = noopFn,
tabIndex = 0,
required,
...rest
}: RadioTileProps,
ref: React.Ref<HTMLInputElement>
Expand Down Expand Up @@ -153,6 +159,7 @@ const RadioTile = React.forwardRef(function RadioTile(
type="radio"
value={value}
ref={ref}
required={required}
/>
<label {...rest} htmlFor={inputId} className={className}>
<span className={`${prefix}--tile__checkmark`}>{icon()}</span>
Expand Down Expand Up @@ -211,6 +218,11 @@ RadioTile.propTypes = {
*/
onChange: PropTypes.func,

/**
* `true` to specify if the control is required.
*/
required: PropTypes.bool,

/**
* Specify the tab index of the underlying `<input>`.
*/
Expand Down
13 changes: 12 additions & 1 deletion packages/react/src/components/TileGroup/TileGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export interface TileGroupProps
* Specify the value that is currently selected in the group
*/
valueSelected?: string | number;
/**
* `true` to specify if input selection in group is required.
*/
required?: boolean;
}

const TileGroup = (props) => {
Expand All @@ -67,9 +71,10 @@ const TileGroup = (props) => {
name,
onChange = noopFn,
valueSelected,
required,
} = props;
const prefix = usePrefix();

const prefix = usePrefix();
const [selected, setSelected] = useState(valueSelected ?? defaultSelected);
const [prevValueSelected, setPrevValueSelected] = useState(valueSelected);

Expand All @@ -91,6 +96,7 @@ const TileGroup = (props) => {
return (
<RadioTile
{...otherProps}
required={required}
name={name}
key={value}
value={value}
Expand Down Expand Up @@ -174,6 +180,11 @@ TileGroup.propTypes = {
*/
onChange: PropTypes.func,

/**
* `true` to specify if input selection in group is required.
*/
required: PropTypes.bool,

/**
* Specify the value that is currently selected in the group
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { FeatureFlags } from '../../FeatureFlags';

describe('PasswordInput', () => {
describe('TileGroup', () => {
describe('renders as expected - Component API', () => {
it('should render `legend` in a <legend>', () => {
render(
Expand Down Expand Up @@ -54,6 +54,46 @@ describe('PasswordInput', () => {
expect(fieldset).toContainElement(screen.getByDisplayValue('test-2'));
});

it('should place required on every child <RadioTile>', () => {
render(
<TileGroup
defaultSelected="test-1"
legend="TestGroup"
name="test"
required>
<RadioTile id="test-1" value="test-1">
Option 1
</RadioTile>
<RadioTile id="test-2" value="test-2">
Option 2
</RadioTile>
</TileGroup>
);

expect(screen.getByDisplayValue('test-1')).toBeRequired();
expect(screen.getByDisplayValue('test-2')).toBeRequired();
});

it('should override required on every child <RadioTile>', () => {
render(
<TileGroup
defaultSelected="test-1"
legend="TestGroup"
name="test"
required>
<RadioTile id="test-1" value="test-1" required={false}>
Option 1
</RadioTile>
<RadioTile id="test-2" value="test-2">
Option 2
</RadioTile>
</TileGroup>
);

expect(screen.getByDisplayValue('test-1')).toBeRequired();
expect(screen.getByDisplayValue('test-2')).toBeRequired();
});

it('should support a custom `className` on the outermost element', () => {
const { container } = render(
<TileGroup
Expand Down
Loading