Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ const sections = [
<td>Reference to the component.</td>
<td>-</td>
</tr>
<tr>
<td>ariaLabel</td>
<td>
<TableCode>string</TableCode>
</td>
<td>Specifies a string to be used as the name for the radio group when no `label` is provided.</td>
<td>'Radio group'</td>
</tr>
</tbody>
</DxcTable>
),
Expand Down
8 changes: 7 additions & 1 deletion packages/lib/src/radio-group/RadioGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ describe("Radio Group component tests", () => {
expect(radioGroup.getAttribute("aria-invalid")).toBe("false");
expect(radioGroup.getAttribute("aria-required")).toBe("true");
expect(radioGroup.getAttribute("aria-orientation")).toBe("vertical");
expect(radioGroup.getAttribute("aria-label")).toBeNull();
expect(error.getAttribute("aria-live")).toBe("off");
radios.forEach((radio, index) => {
// if no option was previously selected, first option is the focusable one
if (index === 0) expect(radio.tabIndex).toBe(0);
else expect(radio.tabIndex).toBe(-1);
expect(radio.getAttribute("aria-checked")).toBe("false");
expect(radio.getAttribute("aria-disabled")).toBe("false");
expect(radio.getAttribute("aria-labelledby")).toBe(getByText(`Option 0${index + 1}`).id);
});
});

test("Initial render has correct aria-label", () => {
const { getByRole } = render(<DxcRadioGroup ariaLabel="Example aria label" options={options} error="" />);
const radioGroup = getByRole("radiogroup");
expect(radioGroup.getAttribute("aria-label")).toBe("Example aria label");
});

test("aria-orientation attribute changes depending on stacking prop value", () => {
const { getByRole } = render(<DxcRadioGroup label="test-radioGroup-label" options={options} stacking="row" />);
const radioGroup = getByRole("radiogroup");
Expand Down
4 changes: 3 additions & 1 deletion packages/lib/src/radio-group/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DxcRadioGroup = forwardRef<RefType, RadioGroupPropsType>(
onBlur,
error,
tabIndex = 0,
ariaLabel = "Radio group",
},
ref
): JSX.Element => {
Expand Down Expand Up @@ -160,12 +161,13 @@ const DxcRadioGroup = forwardRef<RefType, RadioGroupPropsType>(
stacking={stacking}
role="radiogroup"
aria-disabled={disabled}
aria-labelledby={radioGroupLabelId}
aria-labelledby={label ? radioGroupLabelId : undefined}
aria-invalid={!!error}
aria-errormessage={error ? errorId : undefined}
aria-required={!disabled && !readOnly && !optional}
aria-readonly={readOnly}
aria-orientation={stacking === "column" ? "vertical" : "horizontal"}
aria-label={label ? undefined : ariaLabel}
>
<ValueInput name={name} disabled={disabled} value={value ?? innerValue ?? ""} readOnly />
{innerOptions.map((option, index) => (
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/radio-group/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ type RadioGroupProps = {
* Value of the tabindex attribute.
*/
tabIndex?: number;
/**
* Specifies a string to be used as the name for the radio group when no `label` is provided.
*/
ariaLabel?: string;
};

/**
Expand Down
Loading