Skip to content

Commit

Permalink
Merge pull request #1695 from dxc-technology/gomezivann/radio-group
Browse files Browse the repository at this point in the history
Rename `readonly` prop to `readOnly` in Radio Group
  • Loading branch information
raquelarrojo committed Oct 2, 2023
2 parents 752ca0a + b58f386 commit 5a1e592
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 41 deletions.
6 changes: 3 additions & 3 deletions lib/src/common/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ export const componentTokens = {
errorRadioInputColor: CORE_TOKENS.color_red_700,
hoverErrorRadioInputColor: CORE_TOKENS.color_red_800,
activeErrorRadioInputColor: CORE_TOKENS.color_red_900,
readonlyRadioInputColor: CORE_TOKENS.color_grey_500,
hoverReadonlyRadioInputColor: CORE_TOKENS.color_grey_600,
activeReadonlyRadioInputColor: CORE_TOKENS.color_grey_700,
readOnlyRadioInputColor: CORE_TOKENS.color_grey_500,
hoverReadOnlyRadioInputColor: CORE_TOKENS.color_grey_600,
activeReadOnlyRadioInputColor: CORE_TOKENS.color_grey_700,
disabledRadioInputColor: CORE_TOKENS.color_grey_500,
disabledLabelFontColor: CORE_TOKENS.color_grey_500,
disabledHelperTextFontColor: CORE_TOKENS.color_grey_500,
Expand Down
24 changes: 12 additions & 12 deletions lib/src/radio-group/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const DxcRadio = ({
error,
disabled,
focused,
readonly,
readOnly,
tabIndex,
}: RadioProps): JSX.Element => {
const [radioLabelId] = useState(`radio-${uuidv4()}`);
Expand Down Expand Up @@ -41,22 +41,22 @@ const DxcRadio = ({
<RadioContainer
error={error}
disabled={disabled}
readonly={readonly}
readOnly={readOnly}
onClick={disabled ? undefined : handleOnClick}
>
<RadioInputContainer>
<RadioInput
error={error}
disabled={disabled}
readonly={readonly}
readOnly={readOnly}
role="radio"
aria-checked={checked}
aria-disabled={disabled}
aria-labelledby={radioLabelId}
tabIndex={disabled ? -1 : focused ? tabIndex : -1}
ref={ref}
>
{checked && <Dot disabled={disabled} readonly={readonly} error={error} />}
{checked && <Dot disabled={disabled} readOnly={readOnly} error={error} />}
</RadioInput>
</RadioInputContainer>
<Label id={radioLabelId} disabled={disabled}>
Expand All @@ -71,7 +71,7 @@ const DxcRadio = ({
type CommonStylingProps = {
error: RadioProps["error"];
disabled: RadioProps["disabled"];
readonly: RadioProps["readonly"];
readOnly: RadioProps["readOnly"];
};
const getRadioInputStateColor = (
props: CommonStylingProps & { theme: AdvancedTheme["radioGroup"] },
Expand All @@ -83,20 +83,20 @@ const getRadioInputStateColor = (
? props.theme.disabledRadioInputColor
: props.error
? props.theme.errorRadioInputColor
: props.readonly
? props.theme.readonlyRadioInputColor
: props.readOnly
? props.theme.readOnlyRadioInputColor
: props.theme.radioInputColor;
case "hover":
return props.error
? props.theme.hoverErrorRadioInputColor
: props.readonly
? props.theme.hoverReadonlyRadioInputColor
: props.readOnly
? props.theme.hoverReadOnlyRadioInputColor
: props.theme.hoverRadioInputColor;
case "active":
return props.error
? props.theme.activeErrorRadioInputColor
: props.readonly
? props.theme.activeReadonlyRadioInputColor
: props.readOnly
? props.theme.activeReadOnlyRadioInputColor
: props.theme.activeRadioInputColor;
}
};
Expand Down Expand Up @@ -149,7 +149,7 @@ const Label = styled.span<{ disabled: RadioProps["disabled"] }>`
const RadioContainer = styled.span<CommonStylingProps>`
display: inline-flex;
align-items: center;
cursor: ${(props) => (props.disabled ? "not-allowed" : props.readonly ? "default" : "pointer")};
cursor: ${(props) => (props.disabled ? "not-allowed" : props.readOnly ? "default" : "pointer")};
&:hover {
${RadioInput} {
Expand Down
20 changes: 10 additions & 10 deletions lib/src/radio-group/RadioGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ export const Chromatic = () => (
<Title title="Readonly radio input sub-states" theme="light" level={3} />
<ExampleContainer>
<Title title="Enabled" theme="light" level={4} />
<DxcRadioGroup label="Label" helperText="Helper text" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" helperText="Helper text" options={single_option} defaultValue="A" readOnly />
</ExampleContainer>
<ExampleContainer pseudoState="pseudo-hover">
<Title title="Hovered" theme="light" level={4} />
<DxcRadioGroup label="Label" helperText="Helper text" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" helperText="Helper text" options={single_option} defaultValue="A" readOnly />
</ExampleContainer>
<ExampleContainer pseudoState="pseudo-active">
<Title title="Active" theme="light" level={4} />
<DxcRadioGroup label="Label" helperText="Helper text" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" helperText="Helper text" options={single_option} defaultValue="A" readOnly />
</ExampleContainer>
<Title title="Error radio input sub-states" theme="light" level={3} />
<ExampleContainer>
Expand All @@ -81,7 +81,7 @@ export const Chromatic = () => (
helperText="Helper text"
options={single_option}
defaultValue="A"
readonly
readOnly
error="Error message"
/>
</ExampleContainer>
Expand All @@ -92,7 +92,7 @@ export const Chromatic = () => (
helperText="Helper text"
options={single_option}
defaultValue="A"
readonly
readOnly
error="Error message"
/>
</ExampleContainer>
Expand All @@ -115,7 +115,7 @@ export const Chromatic = () => (
</ExampleContainer>
<ExampleContainer>
<Title title="Readonly" theme="light" level={4} />
<DxcRadioGroup label="Label" readonly helperText="Helper text" options={options} />
<DxcRadioGroup label="Label" readOnly helperText="Helper text" options={options} />
</ExampleContainer>
<ExampleContainer>
<Title title="Error space reserved" theme="light" level={4} />
Expand Down Expand Up @@ -159,25 +159,25 @@ export const Chromatic = () => (
<ExampleContainer>
<Title title="Readonly enabled" theme="light" level={4} />
<HalstackProvider theme={opinionatedTheme}>
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readOnly />
</HalstackProvider>
</ExampleContainer>
<ExampleContainer pseudoState="pseudo-hover">
<Title title="Readonly hovered" theme="light" level={4} />
<HalstackProvider theme={opinionatedTheme}>
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readOnly />
</HalstackProvider>
</ExampleContainer>
<ExampleContainer pseudoState="pseudo-active">
<Title title="Readonly active" theme="light" level={4} />
<HalstackProvider theme={opinionatedTheme}>
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readOnly />
</HalstackProvider>
</ExampleContainer>
<ExampleContainer pseudoState="pseudo-focus">
<Title title="Readonly focused" theme="light" level={4} />
<HalstackProvider theme={opinionatedTheme}>
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readonly />
<DxcRadioGroup label="Label" options={single_option} defaultValue="A" readOnly />
</HalstackProvider>
</ExampleContainer>
<ExampleContainer>
Expand Down
21 changes: 19 additions & 2 deletions lib/src/radio-group/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe("Radio Group component tests", () => {
expect(submitInput.value).toBe("8");
});

test("Readonly radio group lets the user move the focus, but neither click nor keyboard press changes the value", () => {
test("Read-only radio group lets the user move the focus, but neither click nor keyboard press changes the value", () => {
const onChange = jest.fn();
const { getByRole, getAllByRole, container } = render(
<DxcRadioGroup
Expand All @@ -460,7 +460,7 @@ describe("Radio Group component tests", () => {
helperText="test-radio-group-helper-text"
options={options}
onChange={onChange}
readonly
readOnly
/>
);
const radioGroup = getByRole("radiogroup");
Expand All @@ -479,4 +479,21 @@ describe("Radio Group component tests", () => {
expect(radios[4].tabIndex).toBe(0);
expect(submitInput.value).toBe("");
});

test("Read-only radio group sends its value on submit", () => {
const handlerOnSubmit = jest.fn((e) => {
e.preventDefault();
const formData = new FormData(e.target);
const formProps = Object.fromEntries(formData);
expect(formProps).toStrictEqual({ radiogroup: "data" });
});
const { getByText } = render(
<form onSubmit={handlerOnSubmit}>
<DxcRadioGroup name="radiogroup" label="test-radio-group-label" value="data" options={options} readOnly />
<button type="submit">Submit</button>
</form>
);
const submit = getByText("Submit");
userEvent.click(submit);
});
});
10 changes: 5 additions & 5 deletions lib/src/radio-group/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
disabled = false,
optional = false,
optionalItemLabel,
readonly = false,
readOnly = false,
stacking = "column",
defaultValue,
value,
Expand Down Expand Up @@ -62,7 +62,7 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
const handleOnChange = useCallback(
(newValue: string) => {
const currentValue = value ?? innerValue;
if (newValue !== currentValue && !readonly) {
if (newValue !== currentValue && !readOnly) {
value ?? setInnerValue(newValue);
onChange?.(newValue);
}
Expand Down Expand Up @@ -146,8 +146,8 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
aria-labelledby={radioGroupLabelId}
aria-invalid={error ? true : false}
aria-errormessage={error ? errorId : undefined}
aria-required={!disabled && !readonly && !optional}
aria-readonly={readonly}
aria-required={!disabled && !readOnly && !optional}
aria-readOnly={readOnly}
aria-orientation={stacking === "column" ? "vertical" : "horizontal"}
>
<ValueInput name={name} disabled={disabled} value={value ?? innerValue ?? ""} readOnly />
Expand All @@ -163,7 +163,7 @@ const DxcRadioGroup = React.forwardRef<RefType, RadioGroupPropsType>(
error={error}
disabled={option.disabled || disabled}
focused={currentFocusIndex === index}
readonly={readonly}
readOnly={readOnly}
tabIndex={tabIndex}
/>
))}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/radio-group/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ type RadioGroupProps = {
*/
optionalItemLabel?: string;
/**
* If true, the component will be marked as readonly.
* If true, the component will not be mutable, meaning the user can not edit the control.
*/
readonly?: boolean;
readOnly?: boolean;
/**
* Sets the orientation of the options within the radio group.
*/
Expand Down Expand Up @@ -108,7 +108,7 @@ export type RadioProps = {
error?: string;
disabled: boolean;
focused: boolean;
readonly: boolean;
readOnly: boolean;
tabIndex: number;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ const sections = [
<td>Label of the optional radio input.</td>
</tr>
<tr>
<td>readonly: boolean</td>
<td>readOnly: boolean</td>
<td>
<Code>false</Code>
</td>
<td>If true, the component will be marked as readonly.</td>
<td>
If true, the component will not be mutable, meaning the user can
not edit the control.
</td>
</tr>
<tr>
<td>stacking: "row" | "column"</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const sections = [
<DxcParagraph>
The following states are defined in the life cycle of the component:{" "}
<strong>enabled</strong>, <strong>error</strong>,{" "}
<strong>readonly</strong> and <strong>disabled</strong>.
<strong>readOnly</strong> and <strong>disabled</strong>.
</DxcParagraph>
<Figure caption="Radio group states">
<Image src={radioGroupStates} alt="Radio group states" />
Expand Down Expand Up @@ -168,7 +168,7 @@ const sections = [
</tr>
<tr>
<td>
<Code>readonlyRadioInputColor</Code>
<Code>readOnlyRadioInputColor</Code>
</td>
<td>Radio input:readonly</td>
<td>
Expand All @@ -178,7 +178,7 @@ const sections = [
</tr>
<tr>
<td>
<Code>hoverReadonlyRadioInputColor</Code>
<Code>hoverReadOnlyRadioInputColor</Code>
</td>
<td>Radio input:readonly:hover</td>
<td>
Expand All @@ -188,7 +188,7 @@ const sections = [
</tr>
<tr>
<td>
<Code>activeReadonlyRadioInputColor</Code>
<Code>activeReadOnlyRadioInputColor</Code>
</td>
<td>Radio input:readonly:active</td>
<td>
Expand Down

0 comments on commit 5a1e592

Please sign in to comment.