Skip to content

Commit d5403a7

Browse files
authored
add a utility to convert tab names with spaces to dashes, fix a11y check for aria-controls (#7766)
* add a utility to convert tab names with spaces to dashes, fix a11y check for aria-controls * add unique ID to name conversion * Update src/components/BlockSwitcher/BlockSwitcher.tsx * Update src/components/BlockSwitcher/BlockSwitcher.tsx
1 parent cb300ef commit d5403a7

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/components/BlockSwitcher/BlockSwitcher.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Children } from 'react';
1+
import { Children, useId } from 'react';
22
import { View, Tabs } from '@aws-amplify/ui-react';
33
import { BlockProps } from './Block';
44

@@ -10,17 +10,30 @@ export const BlockSwitcherErrorMessage =
1010
'BlockSwitcher requires more than one block element';
1111

1212
export const BlockSwitcher = ({ children }: BlockSwitcherProps) => {
13+
const uniqueId = useId();
14+
1315
if (!children.length || children.length <= 1) {
1416
throw new Error(BlockSwitcherErrorMessage);
1517
}
18+
19+
/**
20+
* convert names with spaces to valid ID attribute values
21+
*/
22+
const convertNameToValue = (name: string) => {
23+
return `${name.split(' ').join('-').toLowerCase()}-${uniqueId}`;
24+
};
25+
1626
return (
1727
<View className="block-switcher">
18-
<Tabs.Container defaultValue={children[0].props.name}>
28+
<Tabs.Container defaultValue={convertNameToValue(children[0].props.name)}>
1929
<Tabs.List>
2030
{Children.map(children, (child, index) => {
2131
return (
2232
child.props.name && (
23-
<Tabs.Item value={child.props.name} key={index}>
33+
<Tabs.Item
34+
value={convertNameToValue(child.props.name)}
35+
key={index}
36+
>
2437
{child.props.name}
2538
</Tabs.Item>
2639
)
@@ -30,7 +43,10 @@ export const BlockSwitcher = ({ children }: BlockSwitcherProps) => {
3043
{Children.map(children, (child, index) => {
3144
return (
3245
child.props.name && (
33-
<Tabs.Panel value={child.props.name} key={index}>
46+
<Tabs.Panel
47+
value={convertNameToValue(child.props.name)}
48+
key={index}
49+
>
3450
{child}
3551
</Tabs.Panel>
3652
)

0 commit comments

Comments
 (0)