An open source design system that's fully customizable and agent ready
TypeScript
fix(Tokenizer): block token changes while disabled (#8408)
zakelstormcc95dd0b8f08
diff --git a/.changeset/ast-085-tokenizer.md b/.changeset/ast-085-tokenizer.mdnew file mode 100644index 0000000000..554fe9eb38--- /dev/null+++ b/.changeset/ast-085-tokenizer.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Tokenizer blocks token additions, creation, removal, and supplied removal callbacks while disabled, preserving enabled removal and focus behavior. Disabled form values are omitted instead of rendering native-disabled hidden inputs.+@korkt-kimdiff --git a/packages/core/src/Tokenizer/Tokenizer.test.tsx b/packages/core/src/Tokenizer/Tokenizer.test.tsxindex 38d975ad89..4bd14e3e54 100644--- a/packages/core/src/Tokenizer/Tokenizer.test.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.test.tsx@@ -1296,14 +1296,48 @@ describe('Tokenizer', () => {expect(input.getAttribute('aria-describedby')).toContain(tooltip.id);});- it('blocks input while focusable-disabled', async () => {+ it('blocks typing, Backspace, and supplied token removal while disabled', async () => {const user = userEvent.setup();const onChange = vi.fn();- renderTokenizer({onChange});- const input = screen.getByRole('combobox');- input.focus();+ render(+ <Tokenizer+ label="Members"+ searchSource={userSource}+ value={[users[0]]}+ onChange={onChange}+ isDisabled+ disabledMessage="Editing is unavailable"+ renderToken={(item, onRemove) => (+ <button type="button" onClick={onRemove}>+ Remove {item.label}+ </button>+ )}+ />,+ );+ await user.tab();await user.keyboard('Ali');- expect((input as HTMLInputElement).value).toBe('');+ expect(screen.getByRole('combobox')).toHaveValue('');+ await user.keyboard('{Backspace}');+ await user.click(screen.getByRole('button', {name: 'Remove Alice'}));+ expect(onChange).not.toHaveBeenCalled();+ expect(politeRegion()).toBeNull();+ });++ it('does not commit an open result after becoming disabled', async () => {+ const onChange = vi.fn();+ const props = {+ label: 'Members',+ searchSource: userSource,+ value: [],+ onChange,+ hasEntriesOnFocus: true,+ disabledMessage: 'Editing is unavailable',+ };+ const {rerender} = render(<Tokenizer {...props} />);+ fireEvent.focus(screen.getByRole('combobox'));+ const result = await screen.findByText('Alice');+ rerender(<Tokenizer {...props} isDisabled />);+ fireEvent.click(result);expect(onChange).not.toHaveBeenCalled();});@@ -1478,13 +1512,15 @@ describe('Tokenizer', () => {searchSource={userSource}value={[users[0]]}onChange={() => {}}+ isRequiredisDisabled/></form>,);- expect([- ...new FormData(container.querySelector('form')!).keys(),- ]).toEqual([]);+ const form = container.querySelector('form')!;+ expect([...new FormData(form).keys()]).toEqual([]);+ expect(form.querySelector('input[type="hidden"]')).toBeNull();+ expect(form.checkValidity()).toBe(true);});});});diff --git a/packages/core/src/Tokenizer/Tokenizer.tsx b/packages/core/src/Tokenizer/Tokenizer.tsxindex 10988427ce..22465af668 100644--- a/packages/core/src/Tokenizer/Tokenizer.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.tsx@@ -603,10 +603,10 @@ export function Tokenizer<T extends SearchableItem>({const inputRef = useRef<HTMLInputElement>(null);const wrapperRef = useRef<HTMLDivElement>(null);- // Disabled-reason tooltip. Disabled controls swallow pointer events, so the- // tooltip listeners attach to the input wrapper and the typeahead input stays- // perceivable via aria-disabled instead of the disabled attribute. Input is- // blocked by the isDisabled guards in BaseTypeahead and handleWrapperClick.+ // Disabled-reason tooltip. The tooltip listeners attach to the input wrapper+ // and the typeahead input stays perceivable via aria-disabled instead of the+ // disabled attribute. Input is blocked by the isDisabled guards in+ // BaseTypeahead and handleWrapperClick.const showsDisabledMessage = isDisabled && !!disabledMessage;const disabledMessageTooltip = useTooltip({placement: 'above',@@ -803,7 +803,7 @@ export function Tokenizer<T extends SearchableItem>({// Handle adding an item — detect creatable synthetic itemsconst handleAdd = useCallback((item: T | null) => {- if (!item) {+ if (isDisabled || !item) {return;}if (isAtMax) {@@ -844,6 +844,7 @@ export function Tokenizer<T extends SearchableItem>({[value,onChange,+ isDisabled,isAtMax,selectedIds,selectedLabels,@@ -858,24 +859,27 @@ export function Tokenizer<T extends SearchableItem>({// announcement covers both.const handleRemove = useCallback((item: T) => {+ if (isDisabled) {+ return;+ }const newItems = value.filter(v => v.id !== item.id);onChange(newItems, {item, type: 'remove'});announce(t('@astryx.tokenizer.tokenRemoved', {label: item.label}));inputRef.current?.focus();},- [value, onChange, announce, t],+ [value, onChange, isDisabled, announce, t],);// Handle clearing all itemsconst handleClearAll = useCallback(() => {- if (value.length === 0) {+ if (isDisabled || value.length === 0) {return;}// Report the last item as removed (convention)const lastItem = value[value.length - 1];onChange([], {item: lastItem, type: 'remove'});inputRef.current?.focus();- }, [value, onChange]);+ }, [value, onChange, isDisabled]);// Handle backspace on empty input — remove last tokenconst handleKeyDown = useCallback(@@ -1065,16 +1069,9 @@ export function Tokenizer<T extends SearchableItem>({</TrailingKeyDownContext></BusyIndicatorLaneProvider>{htmlName != null &&+ !isDisabled &&value.map(item => (- <input- key={item.id}- type="hidden"- name={htmlName}- value={item.id}- // Disabled native controls are excluded from form submission;- // mirror that for the hidden carriers.- disabled={isDisabled}- />+ <input key={item.id} type="hidden" name={htmlName} value={item.id} />))}<EndLanelane={busyLane}