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
57 changes: 57 additions & 0 deletions pages/radio-group/progressive-disclosure.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useState } from 'react';
import Box from '~components/box';
import FormField from '~components/form-field';
import RadioGroup from '~components/radio-group';
import Select, { SelectProps } from '~components/select';

const langOptions: Array<SelectProps.Option> = [
{ value: 'en-US', label: 'English (United States)' },
{ value: 'en-GB', label: 'English (United Kingdom)' },
{ value: 'de-DE', label: 'Deutsch (Deutschland)' },
{ value: 'de-CH', label: 'Deutsch (Schweiz)' },
];

export default function RadiosPage() {
const [radioSelection, setRadioSelection] = useState<string>('');
const [lang, setLang] = useState<SelectProps.Option>(langOptions[0]);

return (
<Box padding="l">
<h1>Radio group progressive disclosure demo</h1>
<FormField
label="Language settings"
description="You can select a specific language or have this service automatically identify the language in your media."
>
<RadioGroup
value={radioSelection}
onChange={event => setRadioSelection(event.detail.value)}
ariaControls="language-settings"
items={[
{
label: 'Specific language',
value: 'specific',
description:
'If you know the language spoken in the source media, choose this option for optimal results.',
},
{
label: 'Automatic language detection',
value: 'automatic',
description: 'If you do not know the language spoken in the source media, choose this option.',
},
]}
/>
</FormField>
<Box id="language-settings" display={radioSelection !== 'specific' ? 'none' : 'block'}>
<FormField label="Language" description="Select the language you want to use.">
<Select
selectedOption={lang}
onChange={event => setLang(event.detail.selectedOption)}
options={langOptions}
/>
</FormField>
</Box>
</Box>
);
}
7 changes: 7 additions & 0 deletions src/__tests__/__snapshots__/documenter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9391,6 +9391,13 @@ Object {
],
"name": "RadioGroup",
"properties": Array [
Object {
"description": "Adds \`aria-controls\` attribute to the radio group.
If the radio group controls any secondary content (for example, another form field), use this to provide an ID referring to the secondary content.",
"name": "ariaControls",
"optional": true,
"type": "string",
},
Object {
"description": "Adds \`aria-describedby\` to the component. If you're using this component within a form field,
don't set this property because the form field component automatically sets it.
Expand Down
4 changes: 4 additions & 0 deletions src/radio-group/__tests__/radio-group.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ test('renders attributes for assistive technology when set', function () {
const ariaLabelledby = 'something';
const ariaDescribedby = 'something else';
const ariaLabel = 'the last one';
const ariaControls = 'some-id-being-controlled';

const { wrapper } = renderRadioGroup(
<RadioGroup
ariaLabelledby={ariaLabelledby}
ariaDescribedby={ariaDescribedby}
ariaLabel={ariaLabel}
ariaControls={ariaControls}
value={null}
items={defaultItems}
/>
Expand All @@ -45,6 +47,7 @@ test('renders attributes for assistive technology when set', function () {
expect(rootElement).toHaveAttribute('aria-labelledby', ariaLabelledby);
expect(rootElement).toHaveAttribute('aria-describedby', ariaDescribedby);
expect(rootElement).toHaveAttribute('aria-label', ariaLabel);
expect(rootElement).toHaveAttribute('aria-controls', ariaControls);
});

test('does not render attributes for assistive technology when not set', function () {
Expand All @@ -53,6 +56,7 @@ test('does not render attributes for assistive technology when not set', functio
expect(rootElement).not.toHaveAttribute('aria-labelledby');
expect(rootElement).not.toHaveAttribute('aria-describedby');
expect(rootElement).not.toHaveAttribute('aria-label');
expect(rootElement).not.toHaveAttribute('aria-controls');
});

describe('name', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/radio-group/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface RadioGroupProps extends BaseComponentProps, FormFieldControlPro
*/
ariaRequired?: boolean;

/**
* Adds `aria-controls` attribute to the radio group.
* If the radio group controls any secondary content (for example, another form field), use this to provide an ID referring to the secondary content.
*/
ariaControls?: string;

/**
* Called when the user selects a different radio button. The event `detail` contains the current `value`.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/radio-group/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const InternalRadioGroup = React.forwardRef(
items,
ariaLabel,
ariaRequired,
ariaControls,
onChange,
__internalRootRef = null,
...props
Expand All @@ -40,6 +41,7 @@ const InternalRadioGroup = React.forwardRef(
aria-label={ariaLabel}
aria-describedby={ariaDescribedby}
aria-required={ariaRequired}
aria-controls={ariaControls}
{...baseProps}
className={clsx(baseProps.className, styles.root)}
ref={__internalRootRef}
Expand Down