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

feat: readonly radio button group #12412

Merged
merged 7 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -5864,6 +5864,9 @@ Map {
],
"type": "oneOf",
},
"readOnly": Object {
"type": "bool",
},
"valueSelected": Object {
"args": Array [
Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,10 @@ exports[`DataTable selection -- radio buttons should render 1`] = `
className="cds--radio-button__appearance"
/>
<Text
className="cds--visually-hidden"
className="cds--radio-button__label-text cds--visually-hidden"
>
<span
className="cds--visually-hidden"
className="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
Expand Down Expand Up @@ -664,10 +664,10 @@ exports[`DataTable selection -- radio buttons should render 1`] = `
className="cds--radio-button__appearance"
/>
<Text
className="cds--visually-hidden"
className="cds--radio-button__label-text cds--visually-hidden"
>
<span
className="cds--visually-hidden"
className="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
Expand Down Expand Up @@ -741,10 +741,10 @@ exports[`DataTable selection -- radio buttons should render 1`] = `
className="cds--radio-button__appearance"
/>
<Text
className="cds--visually-hidden"
className="cds--radio-button__label-text cds--visually-hidden"
>
<span
className="cds--visually-hidden"
className="cds--radio-button__label-text cds--visually-hidden"
dir="auto"
>
Select row
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/RadioButton/RadioButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const RadioButton = React.forwardRef(function RadioButton(
onChange(value, name, event);
}

const innerLabelClasses = classNames({
const innerLabelClasses = classNames(`${prefix}--radio-button__label-text`, {
[`${prefix}--visually-hidden`]: hideLabel,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export default {
page: mdx,
},
},
argTypes: {
readOnly: {
description: 'Specify whether the RadioButtonGroup is read-only',
control: {
type: 'boolean',
},
},
},
};

export const Default = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ describe('RadioButtonGroup', () => {
expect(fieldset).toBeDisabled();
});

it('should support readonly to prevent changes', () => {
render(
<RadioButtonGroup
defaultSelected="test-1"
readOnly={true}
name="test"
legendText="test">
<RadioButton labelText="test-1" value="test-1" />
<RadioButton labelText="test-2" value="test-2" />
</RadioButtonGroup>
);

const radio1 = screen.getByLabelText('test-1');
const radio2 = screen.getByLabelText('test-2');

expect(radio1).toBeChecked();
expect(radio2).not.toBeChecked();

userEvent.click(radio2);

// no change
expect(radio1).toBeChecked();
expect(radio2).not.toBeChecked();
});

it('should support `defaultSelected` as a way to select a radio button', () => {
render(
<RadioButtonGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/

import PropTypes from 'prop-types';
import React, { useState } from 'react';
import React, { createContext, useState } from 'react';
import classNames from 'classnames';
import { Legend } from '../Text';
import { usePrefix } from '../../internal/usePrefix';

export const RadioButtonGroupContext = createContext();

const RadioButtonGroup = React.forwardRef(function RadioButtonGroup(
{
children,
Expand All @@ -22,6 +24,7 @@ const RadioButtonGroup = React.forwardRef(function RadioButtonGroup(
name,
onChange = () => {},
orientation = 'horizontal',
readOnly,
valueSelected,
},
ref
Expand Down Expand Up @@ -63,23 +66,29 @@ const RadioButtonGroup = React.forwardRef(function RadioButtonGroup(
}

function handleOnChange(newSelection, value, evt) {
if (newSelection !== selected) {
setSelected(newSelection);
onChange(newSelection, name, evt);
if (!readOnly) {
if (newSelection !== selected) {
setSelected(newSelection);
onChange(newSelection, name, evt);
}
}
}

const fieldsetClasses = classNames(`${prefix}--radio-button-group`, {
[`${prefix}--radio-button-group--${orientation}`]:
orientation === 'vertical',
[`${prefix}--radio-button-group--label-${labelPosition}`]: labelPosition,
[`${prefix}--radio-button-group--readonly`]: readOnly,
});

const wrapperClasses = classNames(`${prefix}--form-item`, className);

return (
<div className={wrapperClasses} ref={ref}>
<fieldset className={fieldsetClasses} disabled={disabled}>
<fieldset
className={fieldsetClasses}
disabled={disabled}
aria-readonly={readOnly}>
{legendText && (
<Legend className={`${prefix}--label`}>{legendText}</Legend>
)}
Expand Down Expand Up @@ -137,6 +146,11 @@ RadioButtonGroup.propTypes = {
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),

/**
* Whether the RadioButtonGroup should be read-only
*/
readOnly: PropTypes.bool,

/**
* Specify the value that is currently selected in the group
*/
Expand Down
18 changes: 18 additions & 0 deletions packages/styles/scss/components/radio-button/_radio-button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ $radio-border-width: 1px !default;
}
}

// readonly
.#{$prefix}--radio-button-group--readonly
.#{$prefix}--radio-button
+ .#{$prefix}--radio-button__label
.#{$prefix}--radio-button__appearance {
border-color: $icon-disabled;
}

.#{$prefix}--radio-button-group--readonly .#{$prefix}--radio-button__label {
cursor: default;
}

.#{$prefix}--radio-button-group--readonly
.#{$prefix}--radio-button__label-text {
cursor: text;
user-select: text;
}

// Focus

.#{$prefix}--radio-button:focus
Expand Down