Skip to content

Commit

Permalink
fix(ChoiceGroup): the label now renders as a div by default
Browse files Browse the repository at this point in the history
It was rendering as an h4, causing problems with heading hierarchies
  • Loading branch information
DSil committed Feb 27, 2025
1 parent 86a167f commit e7119b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
27 changes: 13 additions & 14 deletions packages/orbit-components/src/ChoiceGroup/ChoiceGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const meta: Meta<ChoiceGroupPropsAndCustomArgs> = {
label: "What was the reason for your cancellation?",
onlySelectionText: "Only",
labelSize: LABEL_SIZES.NORMAL,
labelAs: LABEL_ELEMENTS.H4,
error: "",
filter: false,
onChange: action("onChange"),
Expand Down Expand Up @@ -59,9 +58,9 @@ type Story = StoryObj<ChoiceGroupPropsAndCustomArgs>;
export const Default: Story = {
render: args => (
<ChoiceGroup {...args}>
<Radio label="Reason one" value="one" />
<Radio label="Reason two" value="two" />
<Radio label="Reason three" value="three" />
<Radio name="reason" label="Reason one" value="one" />
<Radio name="reason" label="Reason two" value="two" />
<Radio name="reason" label="Reason three" value="three" />
</ChoiceGroup>
),

Expand Down Expand Up @@ -104,9 +103,9 @@ export const Filter: Story = {
export const WithError: Story = {
render: args => (
<ChoiceGroup {...args}>
<Radio label="Reason one" value="one" />
<Radio label="Reason two" value="two" />
<Radio label="Reason three" value="three" />
<Radio name="reason" label="Reason one" value="one" />
<Radio name="reason" label="Reason two" value="two" />
<Radio name="reason" label="Reason three" value="three" />
</ChoiceGroup>
),

Expand Down Expand Up @@ -153,7 +152,7 @@ export const RenderProp: Story = {
}}
>
<Item>
<Radio label={`Option ${index}`} value={index} />
<Radio name="reason" label={`Option ${index}`} value={index} />
</Item>
</div>
)}
Expand All @@ -176,9 +175,9 @@ export const RenderProp: Story = {
export const Playground: Story = {
render: args => (
<ChoiceGroup {...args}>
<Radio label="Reason one" value="one" />
<Radio label="Reason two" value="two" />
<Radio label="Reason three" value="three" />
<Radio name="reason" label="Reason one" value="one" />
<Radio name="reason" label="Reason two" value="two" />
<Radio name="reason" label="Reason three" value="three" />
</ChoiceGroup>
),

Expand All @@ -191,9 +190,9 @@ export const Rtl: Story = {
render: args => (
<RenderInRtl>
<ChoiceGroup {...args}>
<Radio label="Reason one" value="one" />
<Radio label="Reason two" value="two" />
<Radio label="Reason three" value="three" />
<Radio name="reason" label="Reason one" value="one" />
<Radio name="reason" label="Reason two" value="two" />
<Radio name="reason" label="Reason three" value="three" />
</ChoiceGroup>
</RenderInRtl>
),
Expand Down
14 changes: 9 additions & 5 deletions packages/orbit-components/src/ChoiceGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cx from "clsx";
import Heading from "../Heading";
import type { Type } from "../Heading/types";
import Stack from "../Stack";
import { LABEL_SIZES, LABEL_ELEMENTS } from "./consts";
import { LABEL_SIZES } from "./consts";
import Feedback from "./components/Feedback";
import FilterWrapper from "./components/FilterWrapper";
import useRandomId from "../hooks/useRandomId";
Expand Down Expand Up @@ -45,7 +45,7 @@ const ChoiceGroup = React.forwardRef<HTMLDivElement, Props>(
id,
label,
labelSize = LABEL_SIZES.NORMAL,
labelAs = LABEL_ELEMENTS.H4,
labelAs = "div",
error,
children,
filter,
Expand Down Expand Up @@ -82,14 +82,18 @@ const ChoiceGroup = React.forwardRef<HTMLDivElement, Props>(
)}
>
{label && (
<Heading id={groupID} type={getHeadingSize(labelSize)} as={labelAs} spaceAfter="medium">
<Heading
id={groupID}
type={getHeadingSize(labelSize)}
as={labelAs}
role={undefined}
spaceAfter="medium"
>
{label}
</Heading>
)}
{typeof children === "function" ? (
children({
// for now a plain <div> is all we need, but we're reserving this space in the API
// in case we'll need something more in the future
Container: "div",
Item: ItemContainer({ filter, onOnlySelection, onlySelectionText, itemProps }),
spacing: filter ? "0px" : theme.orbit.space200,
Expand Down
2 changes: 1 addition & 1 deletion packages/orbit-components/src/ChoiceGroup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type * as React from "react";
import type * as Common from "../common/types";

export type Size = "normal" | "large";
type Element = "h2" | "h3" | "h4" | "h5" | "h6";
type Element = "h2" | "h3" | "h4" | "h5" | "h6" | "div";

export interface Props extends Common.Globals {
readonly children:
Expand Down

0 comments on commit e7119b9

Please sign in to comment.