An open source design system that's fully customizable and agent ready
TypeScript
fix(core): a disabled Switch no longer runs a caller's key handlers (#9122)
Parinitheecd9c1a5251
diff --git a/.changeset/switch-disabled-key-handlers.md b/.changeset/switch-disabled-key-handlers.mdnew file mode 100644index 0000000000..19b3e5454f--- /dev/null+++ b/.changeset/switch-disabled-key-handlers.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A disabled `Switch` no longer runs a caller's `onKeyDown` or `onKeyUp` (`spec:AST-085` FR2). Before, the switch stayed focusable through `aria-disabled` instead of native `disabled`, so a key pressed on it still reached those handlers, while its `onClick` was already withheld. It still refused to toggle. An enabled switch is unchanged: its key handlers still run.++@parinith-webdiff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsxindex 4e2c85a98f..fb95600716 100644--- a/packages/core/src/Switch/Switch.test.tsx+++ b/packages/core/src/Switch/Switch.test.tsx@@ -700,6 +700,51 @@ describe('Switch', () => {expect(onChange).not.toHaveBeenCalled();});+ it('does not run caller key handlers while disabled', async () => {+ const user = userEvent.setup();+ const onKeyDown = vi.fn();+ const onKeyUp = vi.fn();+ render(+ <Switch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ onKeyDown={onKeyDown}+ onKeyUp={onKeyUp}+ isDisabled+ />,+ );+ await user.tab();+ expect(screen.getByRole('switch')).toHaveFocus();+ await user.keyboard('{Enter}');+ await user.keyboard(' ');+ expect(onKeyDown).not.toHaveBeenCalled();+ expect(onKeyUp).not.toHaveBeenCalled();+ });++ it('still runs caller key handlers on an enabled switch', async () => {+ const user = userEvent.setup();+ const onKeyDown = vi.fn();+ const onKeyUp = vi.fn();+ render(+ <Switch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ onKeyDown={onKeyDown}+ onKeyUp={onKeyUp}+ />,+ );+ await user.tab();+ // Releasing Tab lands a keyup on the newly focused switch; count only+ // the key pressed after it.+ onKeyDown.mockClear();+ onKeyUp.mockClear();+ await user.keyboard('{Enter}');+ expect(onKeyDown).toHaveBeenCalledTimes(1);+ expect(onKeyUp).toHaveBeenCalledTimes(1);+ });+it('stays focusable via aria-disabled when disabled without a reason', async () => {const user = userEvent.setup();const onChange = vi.fn();diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsxindex 927a57dab3..bc1ff12b36 100644--- a/packages/core/src/Switch/Switch.tsx+++ b/packages/core/src/Switch/Switch.tsx@@ -762,8 +762,12 @@ export function Switch({style,)}{...rest}- // Native `disabled` suppressed clicks on the switch (spec:AST-085 FR2).- onClick={isDisabled ? undefined : rest.onClick}>+ // Native `disabled` suppressed clicks and key events on the switch; the+ // switch is no longer natively disabled, so withhold the caller's+ // handlers while disabled (spec:AST-085 FR2).+ onClick={isDisabled ? undefined : rest.onClick}+ onKeyDown={isDisabled ? undefined : rest.onKeyDown}+ onKeyUp={isDisabled ? undefined : rest.onKeyUp}><divref={el => {// Interaction (hover/focus) listeners for the disabled-message