An open source design system that's fully customizable and agent ready
TypeScript
fix(MultiSelector): block changes from disabled open panels (#9152)
zakelstorm5ef8ecf2cedb
diff --git a/.changeset/ast-085-multiselector.md b/.changeset/ast-085-multiselector.mdnew file mode 100644index 0000000000..c90d60d366--- /dev/null+++ b/.changeset/ast-085-multiselector.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] MultiSelector blocks selection and creation callbacks and Actions when it becomes disabled while its panel is open, without removing the focused control.+@korkt-kimdiff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex 9070e58ac2..4224570f50 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -592,6 +592,41 @@ describe('MultiSelector', () => {expect(onChange).not.toHaveBeenCalled();});+ it('does not toggle options or select all after becoming disabled', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ const changeAction = vi.fn();+ const selector = (isDisabled: boolean) => (+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={['Apple']}+ onChange={onChange}+ changeAction={changeAction}+ isDisabled={isDisabled}+ hasSelectAll+ />+ );+ const {rerender} = render(selector(false));+ const trigger = screen.getByRole('combobox', {name: 'Fruit'});+ await user.click(trigger);+ const banana = screen.getByRole('option', {name: 'Banana', ...h});++ rerender(selector(true));+ expect(trigger).toHaveFocus();+ expect(trigger).toHaveAttribute('aria-expanded', 'true');+ await user.click(banana);+ await user.click(screen.getByRole('option', {name: 'Apple', ...h}));+ await user.click(screen.getByRole('option', {name: /^Select all/, ...h}));+ expect(onChange).not.toHaveBeenCalled();+ expect(changeAction).not.toHaveBeenCalled();++ rerender(selector(false));+ await user.click(banana);+ expect(onChange).toHaveBeenCalledExactlyOnceWith(['Apple', 'Banana']);+ expect(changeAction).toHaveBeenCalledExactlyOnceWith(['Apple', 'Banana']);+ });+it('renders disabled state', () => {render(<MultiSelector@@ -3838,6 +3873,46 @@ describe('MultiSelector hasCreate', () => {expect(search).toHaveValue('');});+ it('does not create by Enter or click after becoming disabled', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ const changeAction = vi.fn();+ const selector = (isDisabled: boolean) => (+ <MultiSelector+ label="Labels"+ options={OPTIONS}+ value={['bug']}+ onChange={onChange}+ changeAction={changeAction}+ isDisabled={isDisabled}+ hasSearch+ hasCreate+ />+ );+ const {rerender} = render(selector(false));+ await user.click(screen.getByRole('button', {name: 'Labels'}));+ const search = screen.getByRole('combobox', h);+ await user.type(search, 'Urgent');+ const create = screen.getByRole('option', {name: 'Create "Urgent"', ...h});++ rerender(selector(true));+ expect(search).toHaveFocus();+ await user.keyboard('{Enter}');+ await user.click(create);+ expect(onChange).not.toHaveBeenCalled();+ expect(changeAction).not.toHaveBeenCalled();+ expect(search).toHaveValue('Urgent');++ rerender(selector(false));+ await user.click(create);+ expect(onChange).toHaveBeenCalledExactlyOnceWith(['bug', 'Urgent'], {+ type: 'create',+ query: 'Urgent',+ });+ expect(changeAction).toHaveBeenCalledExactlyOnceWith(['bug', 'Urgent']);+ expect(search).toHaveValue('');+ });+it('says only the latest status when several keystrokes land in one frame (spec:AST-086 FR9)', async () => {// A barcode scanner, dictation or automation types faster than a frame:// each keystroke states the panel's status through the search'sdiff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex f185a08203..aa23936e92 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -1215,11 +1215,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({// (spec:AST-086 FR8).useBusyAnnouncement(isBusy, t('@astryx.spinner.loading'), triggerRef);- // 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- // useMultiCombobox (onTriggerClick / onKeyDown).+ // Disabled-reason tooltip listeners attach to the existing trigger container.+ // The trigger stays focusable via aria-disabled. Trigger guards and the value+ // commit guards below block activation, including from an already-open panel.const showsDisabledMessage = isDisabled && !!disabledMessage;const disabledMessageTooltip = useTooltip({placement: 'above',@@ -1626,6 +1624,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({// Clear all selected values. Shared by the clear button and the keyboard// Delete/Backspace path so clearing is reachable without a mouse.const clearValues = useCallback(() => {+ if (isDisabled) {+ return;+ }onChange([]);announceSelection([]);if (changeAction) {@@ -1635,6 +1636,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({});}}, [+ isDisabled,onChange,changeAction,startTransition,@@ -1663,6 +1665,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({const handleToggle = useCallback((itemValue: string) => {+ if (isDisabled) {+ return;+ }const newValue = optimisticValue.includes(itemValue)? optimisticValue.filter(v => v !== itemValue): [...optimisticValue, itemValue];@@ -1677,6 +1682,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({}},[+ isDisabled,optimisticValue,onChange,changeAction,@@ -1711,6 +1717,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({: false;const handleSelectAll = useCallback(() => {+ if (isDisabled) {+ return;+ }let newValue: string[];if (allEnabledSelected) {// Deselect all enabled items, keep disabled items that are selected@@ -1736,6 +1745,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({});}}, [+ isDisabled,allEnabledSelected,enabledItems,optimisticValue,@@ -1752,6 +1762,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({// the option the caller adds is visible.const commitCreate = useCallback((query: string) => {+ if (isDisabled) {+ return;+ }const newValue = optimisticValue.includes(query)? optimisticValue: [...optimisticValue, query];@@ -1766,6 +1779,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({}},[+ isDisabled,optimisticValue,onChange,changeAction,