Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(segmentedcontrol): keep tab-through a pure focus move (#3598)

Shahrier Islam Arhame9b31f8621da
diff --git a/.changeset/segmented-tab-onchange.md b/.changeset/segmented-tab-onchange.md
new file mode 100644
index 0000000000..226f489106
--- /dev/null
+++ b/.changeset/segmented-tab-onchange.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] SegmentedControl: tabbing through no longer rewrites the value (#3597)
+
+When value matched no enabled item (initial empty state, stale server value, or the selected item disabled), the roving tab stop fell back to the first enabled radio and selection-follows-focus fired onChange with it — a keyboard user mutated the form just by tabbing past the control. Selection now only follows focus moves within the group (arrow/Home/End); entering focus is a pure focus move, and click selection is unchanged.
+@arham766
diff --git a/packages/core/src/Avatar/Avatar.tsx b/packages/core/src/Avatar/Avatar.tsx
index 8be8b17e18..53546514ed 100644
--- a/packages/core/src/Avatar/Avatar.tsx
+++ b/packages/core/src/Avatar/Avatar.tsx
@@ -63,20 +63,7 @@ type AvatarNamedSize = 'tiny' | 'xsmall' | 'small' | 'medium' | 'large';
* Numeric size options (in pixels)
*/
type AvatarNumericSize =
- | 16
- | 20
- | 24
- | 32
- | 36
- | 40
- | 48
- | 60
- | 64
- | 72
- | 96
- | 128
- | 144
- | 180;
+ 16 | 20 | 24 | 32 | 36 | 40 | 48 | 60 | 64 | 72 | 96 | 128 | 144 | 180;
/**
* Avatar size - can be a named size or a specific pixel value
diff --git a/packages/core/src/SegmentedControl/SegmentedControl.test.tsx b/packages/core/src/SegmentedControl/SegmentedControl.test.tsx
index fd2fb52039..d29c2d27af 100644
--- a/packages/core/src/SegmentedControl/SegmentedControl.test.tsx
+++ b/packages/core/src/SegmentedControl/SegmentedControl.test.tsx
@@ -259,6 +259,56 @@ describe('SegmentedControl', () => {
});
describe('SegmentedControl keyboard navigation', () => {
+ describe('tab-through is a pure focus move (#3597)', () => {
+ it('does not fire onChange when tabbing in with an unmatched value', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <>
+ <button type="button">before</button>
+ <SegmentedControl label="View" value="archived" onChange={onChange}>
+ <SegmentedControlItem value="grid" label="Grid" />
+ <SegmentedControlItem value="list" label="List" />
+ </SegmentedControl>
+ </>,
+ );
+ screen.getByText('before').focus();
+ await user.tab();
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('does not fire onChange when tabbing in while the selected item is disabled', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <>
+ <button type="button">before</button>
+ <SegmentedControl label="View" value="list" onChange={onChange}>
+ <SegmentedControlItem value="grid" label="Grid" />
+ <SegmentedControlItem value="list" label="List" isDisabled />
+ </SegmentedControl>
+ </>,
+ );
+ screen.getByText('before').focus();
+ await user.tab();
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('still selects on arrow-key navigation within the group', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <SegmentedControl label="View" value="grid" onChange={onChange}>
+ <SegmentedControlItem value="grid" label="Grid" />
+ <SegmentedControlItem value="list" label="List" />
+ </SegmentedControl>,
+ );
+ screen.getByRole('radio', {name: 'Grid'}).focus();
+ await user.keyboard('{ArrowRight}');
+ expect(onChange).toHaveBeenCalledWith('list');
+ });
+ });
+
it('navigates with ArrowRight and selects', async () => {
const user = userEvent.setup();
const handleChange = vi.fn();
diff --git a/packages/core/src/SegmentedControl/SegmentedControl.tsx b/packages/core/src/SegmentedControl/SegmentedControl.tsx
index a90376c9b9..7d7408407f 100644
--- a/packages/core/src/SegmentedControl/SegmentedControl.tsx
+++ b/packages/core/src/SegmentedControl/SegmentedControl.tsx
@@ -209,6 +209,15 @@ export function SegmentedControl({
if (isDisabled) {
return;
}
+ // Only select when focus moved WITHIN the group (arrow/Home/End — APG
+ // selection-follows-focus). When focus is entering from outside, Tab
+ // must stay a pure focus move: with an unmatched or disabled-selected
+ // value the roving tab stop falls back to the first enabled radio, and
+ // selecting it here would rewrite the form value on mere traversal.
+ // Clicks are unaffected: the item's own handleClick selects.
+ if (!e.currentTarget.contains(e.relatedTarget)) {
+ return;
+ }
const focused = (e.target as HTMLElement | null)?.closest<HTMLElement>(
'[role="radio"][data-value]',
);