An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): block selection from disabled open popups (#9137)
zakelstormb13f483f05b6
diff --git a/.changeset/ast-085-selector.md b/.changeset/ast-085-selector.mdnew file mode 100644index 0000000000..82bff288b5--- /dev/null+++ b/.changeset/ast-085-selector.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Selector ignores option selections after it becomes disabled, including selections from an already-open popup and pointer gestures started while enabled. Its trigger remains focusable and disabled values remain excluded from forms.+@korkt-kimdiff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex f1f1d99448..de765b39e2 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -4885,6 +4885,145 @@ describe('Selector option descriptions and trigger value', () => {});});+describe('Selector disabled selection transitions', () => {+ it.each([+ {presentation: 'popover', hasSearch: false, withReason: false},+ {presentation: 'popover', hasSearch: true, withReason: true},+ {presentation: 'bottom-sheet', hasSearch: false, withReason: true},+ {presentation: 'bottom-sheet', hasSearch: true, withReason: false},+ ] as const)(+ 'rejects a pointer selection from a popup opened before disablement ($presentation, search=$hasSearch, reason=$withReason)',+ async ({presentation, hasSearch, withReason}) => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ const changeAction = vi.fn();+ function ControlledSelector({isDisabled}: {isDisabled: boolean}) {+ const [value, setValue] = useState('Apple');+ return (+ <form aria-label="Fruit form">+ <Selector+ label="Fruit"+ htmlName="fruit"+ options={OPTIONS}+ value={value}+ isDisabled={isDisabled}+ presentation={presentation}+ hasSearch={hasSearch}+ disabledMessage={withReason ? 'Unavailable reason' : undefined}+ onChange={next => {+ onChange(next);+ setValue(next);+ }}+ changeAction={changeAction}+ />+ </form>+ );+ }+ const {rerender} = render(<ControlledSelector isDisabled={false} />);+ const trigger = screen.getByRole(hasSearch ? 'button' : 'combobox', {+ name: 'Fruit',+ });+ const form = screen.getByRole<HTMLFormElement>('form', {+ name: 'Fruit form',+ });+ await user.click(trigger);+ // The bottom-sheet presentation loads lazily on its first opening.+ const banana = await screen.findByRole('option', {name: 'Banana', ...h});+ expect(trigger).toHaveAttribute('aria-expanded', 'true');+ if (hasSearch) {+ await waitFor(() =>+ expect(screen.getByRole('combobox', h)).toHaveFocus(),+ );+ }+ const focused = document.activeElement;++ rerender(<ControlledSelector isDisabled />);+ expect(document.activeElement).toBe(focused);+ expect(trigger).toHaveAttribute('aria-disabled', 'true');+ expect(new FormData(form).getAll('fruit')).toEqual([]);+ await user.click(banana);++ expect(onChange).not.toHaveBeenCalled();+ expect(changeAction).not.toHaveBeenCalled();+ expect(trigger).toHaveTextContent('Apple');+ rerender(<ControlledSelector isDisabled={false} />);+ expect(new FormData(form).getAll('fruit')).toEqual(['Apple']);+ await user.click(banana);+ expect(onChange).toHaveBeenCalledExactlyOnceWith('Banana');+ expect(changeAction).toHaveBeenCalledExactlyOnceWith('Banana');+ expect(new FormData(form).getAll('fruit')).toEqual(['Banana']);+ },+ );++ it('rejects activation of a retained option after closing and disabling', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ const changeAction = vi.fn();+ const selector = (isDisabled: boolean) => (+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Apple"+ isDisabled={isDisabled}+ onChange={onChange}+ changeAction={changeAction}+ />+ );+ const {rerender} = render(selector(false));+ const trigger = screen.getByRole('combobox', {name: 'Fruit'});+ await user.click(trigger);+ const banana = screen.getByRole('option', {name: 'Banana', ...h});+ await user.keyboard('{Escape}');+ expect(trigger).toHaveAttribute('aria-expanded', 'false');+ expect(banana).toBeInTheDocument();++ rerender(selector(true));+ fireEvent.click(banana, {detail: 0});++ expect(onChange).not.toHaveBeenCalled();+ expect(changeAction).not.toHaveBeenCalled();+ expect(trigger).toHaveTextContent('Apple');+ expect(trigger).toHaveFocus();+ });++ it.each(['mouse', 'touch', 'pen'])(+ 'rejects a %s gesture released after the selector becomes disabled',+ async pointerType => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ const changeAction = vi.fn();+ const selector = (isDisabled: boolean) => (+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Apple"+ isDisabled={isDisabled}+ onChange={onChange}+ changeAction={changeAction}+ />+ );+ const {rerender} = render(selector(false));+ const trigger = screen.getByRole('combobox', {name: 'Fruit'});+ await user.click(trigger);+ const apple = screen.getByRole('option', {name: 'Apple', ...h});+ const banana = screen.getByRole('option', {name: 'Banana', ...h});+ const pointer = {pointerType, pointerId: 1, button: 0};+ fireEvent.pointerDown(apple, pointer);+ fireEvent.pointerMove(banana, pointer);+ rerender(selector(true));+ fireEvent.pointerUp(banana, pointer);+ // Include the delayed click that a browser can send after the gesture.+ fireEvent.click(apple, {detail: 1});+ // Assistive activation reaches the same component-owned selection path.+ fireEvent.click(banana, {detail: 0});+ expect(onChange).not.toHaveBeenCalled();+ expect(changeAction).not.toHaveBeenCalled();+ expect(trigger).toHaveTextContent('Apple');+ expect(trigger).toHaveFocus();+ },+ );+});+describe('Selector press model', () => {const touch = {pointerType: 'touch', pointerId: 1};const h = {hidden: true};diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex ef9521e905..5e4528be5c 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -1060,11 +1060,9 @@ export function Selector<T extends SelectorOptionType>(useBusyAnnouncement(isBusy, t('@astryx.spinner.loading'), triggerRef);const announce = useAnnounce();- // Disabled-reason tooltip. Disabled controls swallow pointer events, so the- // tooltip listeners attach to the trigger container (which already exists)- // and the trigger button stays perceivable via aria-disabled instead of the- // disabled attribute. Activation is blocked by the isDisabled guards in- // useCombobox (onTriggerClick / onKeyDown).+ // Disabled-reason tooltip. The container receives hover and bubbled focus+ // while the trigger stays focusable via aria-disabled. Opening and selection+ // are guarded independently, including options in an already-open surface.const showsDisabledMessage = isDisabled && !!disabledMessage;const disabledMessageTooltip = useTooltip({placement: 'above',@@ -1327,6 +1325,11 @@ export function Selector<T extends SelectorOptionType>(const commitValue = useCallback((newValue: string) => {+ // A surface or pointer gesture can outlive the enabled state that opened+ // it. Enforce current availability before callbacks or optimistic state.+ if (isDisabled || isEffectivelyReadOnly) {+ return;+ }onChange?.(newValue);if (changeAction) {startTransition(async () => {@@ -1335,7 +1338,14 @@ export function Selector<T extends SelectorOptionType>(});}},- [onChange, changeAction, startTransition, setOptimisticValue],+ [+ isDisabled,+ isEffectivelyReadOnly,+ onChange,+ changeAction,+ startTransition,+ setOptimisticValue,+ ],);// Selector behavior (keyboard nav, selection)diff --git a/packages/core/src/Selector/hooks.ts b/packages/core/src/Selector/hooks.tsindex 5f19b8e412..6f7ed9733e 100644--- a/packages/core/src/Selector/hooks.ts+++ b/packages/core/src/Selector/hooks.ts@@ -301,13 +301,13 @@ export function useCombobox<T extends ComboboxOption = SelectorOptionData>({const selectItem = useCallback((item: T) => {- if (item.disabled) {+ if (isDisabled || item.disabled) {return;}onSelect?.(item.value);closeAndReset();},- [onSelect, closeAndReset],+ [isDisabled, onSelect, closeAndReset],);const onTriggerClick = useCallback(() => {