An open source design system that's fully customizable and agent ready
TypeScript
fix(Tokenizer): suppress Create when the typed label matches a selected token (#6367)
Manohar Paturib29bc2a27925
diff --git a/.changeset/tokenizer-create-dedupe-by-label.md b/.changeset/tokenizer-create-dedupe-by-label.mdnew file mode 100644index 0000000000..cf6ff663c0--- /dev/null+++ b/.changeset/tokenizer-create-dedupe-by-label.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Stop Tokenizer's `hasCreate` "Create X" entry from offering a label that is already tokenized under a different id. The duplicate check compared the typed text against selected ids, but real selections usually carry an opaque id (`u1` under the label "Alice"), and the search results could not catch the match because they are pre-filtered to remove already-selected items — so typing an existing token's label offered "Create", which committed a second, duplicate token with a new id. The check now also compares against selected labels (case-insensitive), in both the Create entry and the stale-entry commit guard.++@ManoharPaturidiff --git a/packages/core/src/Tokenizer/Tokenizer.test.tsx b/packages/core/src/Tokenizer/Tokenizer.test.tsxindex 00dc343c79..8ff89d7a35 100644--- a/packages/core/src/Tokenizer/Tokenizer.test.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.test.tsx@@ -920,6 +920,93 @@ describe('Tokenizer', () => {expect(screen.queryByText('Create "existing"')).not.toBeInTheDocument();});+ it('does not show Create option for an already-selected label whose id differs from it', async () => {+ // Real selections usually carry an opaque id (`u1` under the label+ // "Alice"). The duplicate check used to compare the typed text against+ // selected ids only, and the search results could not catch the match+ // because filteredSource had already removed the selected item — so+ // typing the label of an existing token offered "Create", which added a+ // duplicate token under a new id.+ const source: SearchSource = {+ search: (query: string) =>+ [{id: 'u1', label: 'Alice'}].filter(u =>+ u.label.toLowerCase().includes(query.toLowerCase()),+ ),+ bootstrap: () => [],+ };+ const onChange = vi.fn();+ render(+ <Tokenizer+ label="Tags"+ searchSource={source}+ value={[{id: 'u1', label: 'Alice'}]}+ onChange={onChange}+ hasCreate+ debounceMs={0}+ />,+ );++ const input = screen.getByRole('combobox');+ await act(async () => {+ fireEvent.change(input, {target: {value: 'Alice'}});+ });+ await act(async () => {+ await new Promise(r => setTimeout(r, 50));+ });++ expect(screen.queryByText('Create "Alice"')).not.toBeInTheDocument();+ });++ it('does not add a duplicate token when a stale Create entry is committed', async () => {+ // The guard in handleAdd must apply the same label check: a Create+ // entry rendered before the selection changed still commits through+ // here, and an opaque id must not let a same-label token through.+ const source: SearchSource = {+ search: (query: string) =>+ // A label that contains the query but is not exactly it, so the+ // Create entry for the query itself is legitimately offered.+ [{id: 'u2', label: 'Alice Smith'}].filter(u =>+ u.label.toLowerCase().includes(query.toLowerCase()),+ ),+ bootstrap: () => [],+ };+ const onChange = vi.fn();+ const {rerender} = render(+ <Tokenizer+ label="Tags"+ searchSource={source}+ value={[]}+ onChange={onChange}+ hasCreate+ debounceMs={0}+ />,+ );++ const input = screen.getByRole('combobox');+ await act(async () => {+ fireEvent.change(input, {target: {value: 'Alice'}});+ });+ await act(async () => {+ await new Promise(r => setTimeout(r, 50));+ });+ // The Create entry exists while the selection is empty...+ expect(screen.queryByText('Create "Alice"')).toBeInTheDocument();+ // ...then the selection gains Alice (under an opaque id) before the+ // entry is committed.+ rerender(+ <Tokenizer+ label="Tags"+ searchSource={source}+ value={[{id: 'u1', label: 'Alice'}]}+ onChange={onChange}+ hasCreate+ debounceMs={0}+ />,+ );+ fireEvent.click(screen.getByText('Create "Alice"'));+ expect(onChange).not.toHaveBeenCalled();+ });+it('does not show Create option when hasCreate is false', async () => {render(<Tokenizerdiff --git a/packages/core/src/Tokenizer/Tokenizer.tsx b/packages/core/src/Tokenizer/Tokenizer.tsxindex d0fd190345..c479229b8a 100644--- a/packages/core/src/Tokenizer/Tokenizer.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.tsx@@ -729,6 +729,18 @@ export function Tokenizer<T extends SearchableItem>({[value],);+ // Selected labels, lowercased, for the Create entry's duplicate check. The+ // typed text is a label, not an id: real selections usually carry an opaque+ // id (`usr_123` under the label "Alice"), so comparing the text against ids+ // alone misses an already-tokenized label — and the search results cannot+ // catch it either, because filteredSource has already removed the selected+ // item from them. The result was a "Create Alice" entry that, when picked,+ // added a second "Alice" token under a different id.+ const selectedLabels = useMemo(+ () => new Set(value.map(item => item.label.toLowerCase())),+ [value],+ );+const filteredSource: SearchSource<T> = useMemo(() => ({search: async (query: string) => {@@ -759,6 +771,7 @@ export function Tokenizer<T extends SearchableItem>({}const alreadyExists =selectedIds.has(trimmed) ||+ selectedLabels.has(trimmed.toLowerCase()) ||results.some(item => item.label.toLowerCase() === trimmed.toLowerCase(),);@@ -773,7 +786,7 @@ export function Tokenizer<T extends SearchableItem>({} as unknown as T,];},- [hasCreate, selectedIds],+ [hasCreate, selectedIds, selectedLabels],);const emptySource: SearchSource<T> = useMemo(@@ -807,7 +820,13 @@ export function Tokenizer<T extends SearchableItem>({item.id.startsWith(CREATABLE_ID_PREFIX)) {const createdValue = item.id.slice(CREATABLE_ID_PREFIX.length);- if (selectedIds.has(createdValue)) {+ // Same domain fix as createEntries: the created value names itself by+ // label, so a selection carrying an opaque id with that label is also a+ // duplicate, not just one whose id happens to equal the text.+ if (+ selectedIds.has(createdValue) ||+ selectedLabels.has(createdValue.toLowerCase())+ ) {return;}const base = {id: createdValue, label: createdValue};@@ -825,7 +844,16 @@ export function Tokenizer<T extends SearchableItem>({onChange(newItems, {item, type: 'add'});announce(t('@astryx.tokenizer.tokenAdded', {label: item.label}));},- [value, onChange, isAtMax, selectedIds, hasCreate, announce, t],+ [+ value,+ onChange,+ isAtMax,+ selectedIds,+ selectedLabels,+ hasCreate,+ announce,+ t,+ ],);// Handle removing an item. Single removal path: both Backspace on an empty