An open source design system that's fully customizable and agent ready
TypeScript
fix(core): a disabled SegmentedControlItem no longer runs a caller's onClick (#8989)
Parinith070d574dbab6
diff --git a/.changeset/segmented-control-item-disabled-onclick.md b/.changeset/segmented-control-item-disabled-onclick.mdnew file mode 100644index 0000000000..a70f07cb3d--- /dev/null+++ b/.changeset/segmented-control-item-disabled-onclick.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A disabled `SegmentedControlItem` no longer runs a caller's `onClick` (`spec:AST-085` FR2). Before, the segment refused to select but still called the handler on a click, and on Enter or Space for the selected segment of a group disabled with a reason, which stays focusable. This covers a segment disabled by its own `isDisabled` and one disabled by its group. An enabled segment is unchanged: its `onClick` still runs first and can still cancel selection with `preventDefault()`.++@parinith-webdiff --git a/packages/core/src/SegmentedControl/SegmentedControl.test.tsx b/packages/core/src/SegmentedControl/SegmentedControl.test.tsxindex 4b74bdf1d2..631651fd5c 100644--- a/packages/core/src/SegmentedControl/SegmentedControl.test.tsx+++ b/packages/core/src/SegmentedControl/SegmentedControl.test.tsx@@ -562,6 +562,90 @@ describe('SegmentedControl disabled state', () => {expect(handleChange).not.toHaveBeenCalled();});+ it("does not run a caller's onClick on a disabled segment", async () => {+ const user = userEvent.setup({pointerEventsCheck: 0});+ const handleClick = vi.fn();+ const handleChange = vi.fn();++ render(+ <SegmentedControl value="grid" onChange={handleChange} label="View mode">+ <SegmentedControlItem value="grid" label="Grid" />+ <SegmentedControlItem+ value="list"+ label="List"+ isDisabled+ onClick={handleClick}+ />+ </SegmentedControl>,+ );++ await user.click(screen.getByRole('radio', {name: 'List'}));+ expect(handleClick).not.toHaveBeenCalled();+ expect(handleChange).not.toHaveBeenCalled();+ });++ it("does not run a caller's onClick when the whole group is disabled", async () => {+ const user = userEvent.setup({pointerEventsCheck: 0});+ const handleClick = vi.fn();++ render(+ <SegmentedControl+ value="grid"+ onChange={() => {}}+ label="View mode"+ isDisabled>+ <SegmentedControlItem value="grid" label="Grid" />+ <SegmentedControlItem value="list" label="List" onClick={handleClick} />+ </SegmentedControl>,+ );++ await user.click(screen.getByRole('radio', {name: 'List'}));+ expect(handleClick).not.toHaveBeenCalled();+ });++ it("does not run a caller's onClick on a focusable disabled segment", async () => {+ // A group disabled with a reason keeps its selected segment in the tab+ // order, so Enter and Space can reach it and synthesize a click.+ const user = userEvent.setup({pointerEventsCheck: 0});+ const handleClick = vi.fn();++ render(+ <SegmentedControl+ value="grid"+ onChange={() => {}}+ label="View mode"+ isDisabled+ disabledMessage="Choose a project to switch views">+ <SegmentedControlItem value="grid" label="Grid" onClick={handleClick} />+ <SegmentedControlItem value="list" label="List" />+ </SegmentedControl>,+ );++ const selected = screen.getByRole('radio', {name: 'Grid'});+ selected.focus();+ expect(selected).toHaveFocus();+ await user.keyboard('{Enter}');+ await user.keyboard(' ');+ expect(handleClick).not.toHaveBeenCalled();+ });++ it("still runs a caller's onClick on an enabled segment", async () => {+ const user = userEvent.setup({pointerEventsCheck: 0});+ const handleClick = vi.fn();+ const handleChange = vi.fn();++ render(+ <SegmentedControl value="grid" onChange={handleChange} label="View mode">+ <SegmentedControlItem value="grid" label="Grid" />+ <SegmentedControlItem value="list" label="List" onClick={handleClick} />+ </SegmentedControl>,+ );++ await user.click(screen.getByRole('radio', {name: 'List'}));+ expect(handleClick).toHaveBeenCalledTimes(1);+ expect(handleChange).toHaveBeenCalledWith('list');+ });+it('skips disabled items during keyboard navigation', async () => {const user = userEvent.setup();const handleChange = vi.fn();diff --git a/packages/core/src/SegmentedControl/SegmentedControlItem.tsx b/packages/core/src/SegmentedControl/SegmentedControlItem.tsxindex c2382f99cd..836188771e 100644--- a/packages/core/src/SegmentedControl/SegmentedControlItem.tsx+++ b/packages/core/src/SegmentedControl/SegmentedControlItem.tsx@@ -210,11 +210,17 @@ export function SegmentedControlItem({// Consumer-first: a consumer onClick can call preventDefault() to opt out of// selection; otherwise selection proceeds when enabled and not selected.- const handleClick = composeEventHandlers(onClickProp, () => {- if (!isItemDisabled && !isSelected) {- ctx.onChange(value);- }- });+ // A disabled segment invokes no caller callback (spec:AST-085 FR2), so the+ // consumer onClick is left out while it is disabled, including the selected+ // segment of a group disabled with a reason, which stays focusable.+ const handleClick = composeEventHandlers(+ isItemDisabled ? undefined : onClickProp,+ () => {+ if (!isItemDisabled && !isSelected) {+ ctx.onChange(value);+ }+ },+ );const iconElement = icon ? (<span {...stylex.props(styles.icon, iconSizeStyles[size])}>{icon}</span>