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

add a utility to convert tab names with spaces to dashes, fix a11y check for aria-controls #7766

Merged
Merged
Changes from 3 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
24 changes: 20 additions & 4 deletions src/components/BlockSwitcher/BlockSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Children } from 'react';
import { Children, useId } from 'react';
import { View, Tabs } from '@aws-amplify/ui-react';
import { BlockProps } from './Block';

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

export const BlockSwitcher = ({ children }: BlockSwitcherProps) => {
const uniqueId = useId();

if (!children.length || children.length <= 1) {
throw new Error(BlockSwitcherErrorMessage);
}

/**
* convert names with spaces to valid aria-controls and ID attribute values
josefaidt marked this conversation as resolved.
Show resolved Hide resolved
*/
const convertNameToValue = (name: string) => {
josefaidt marked this conversation as resolved.
Show resolved Hide resolved
return `${name.split(' ').join('-').toLowerCase()}-${uniqueId}`;
};

return (
<View className="block-switcher">
<Tabs.Container defaultValue={children[0].props.name}>
<Tabs.Container defaultValue={convertNameToValue(children[0].props.name)}>
<Tabs.List>
{Children.map(children, (child, index) => {
return (
child.props.name && (
<Tabs.Item value={child.props.name} key={index}>
<Tabs.Item
value={convertNameToValue(child.props.name)}
key={index}
>
{child.props.name}
</Tabs.Item>
)
Expand All @@ -30,7 +43,10 @@ export const BlockSwitcher = ({ children }: BlockSwitcherProps) => {
{Children.map(children, (child, index) => {
return (
child.props.name && (
<Tabs.Panel value={child.props.name} key={index}>
<Tabs.Panel
value={convertNameToValue(child.props.name)}
key={index}
>
{child}
</Tabs.Panel>
)
Expand Down