Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): the Tokenizer showcase block searches its tags (#8737)

Xinzi Zhoudbb2d527ea4a
diff --git a/.changeset/cli-tokenizer-showcase-search.md b/.changeset/cli-tokenizer-showcase-search.md
new file mode 100644
index 0000000000..23aacd0062
--- /dev/null
+++ b/.changeset/cli-tokenizer-showcase-search.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] The `Tokenizer` showcase block template searches its tags: typing lists the tags that match. Its search source returned nothing for every query, so the live example on the Tokenizer docs page answered all typing with "No results found".
+
+@imdreamrunner
diff --git a/packages/cli/api/template/tokenizer-showcase.test.mjs b/packages/cli/api/template/tokenizer-showcase.test.mjs
new file mode 100644
index 0000000000..3d46f630d3
--- /dev/null
+++ b/packages/cli/api/template/tokenizer-showcase.test.mjs
@@ -0,0 +1,47 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Contract test for the Tokenizer showcase block.
+ * @input The shipped TokenizerShowcase block source.
+ * @output Guards the block's search source: typing in the showcase must list
+ * matching tags rather than an empty menu.
+ * @position CLI template regression coverage; runs in the node test project.
+ * The block is the hero example of the Tokenizer docs page. Like the other
+ * template contract tests here it reads the block's source instead of
+ * importing it, so it needs no built `@astryxdesign/core`.
+ */
+
+import fs from 'node:fs';
+import path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {describe, expect, it} from 'vitest';
+
+const sourcePath = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ '../../assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx',
+);
+const source = fs.readFileSync(sourcePath, 'utf8');
+
+// Evaluate the block's tag list and search source on their own, with the
+// type annotations removed, so the test exercises the shipped logic.
+function loadSearchSource() {
+ const tags = source.match(/const tags[^=]*=\s*(\[[\s\S]*?\n\]);/)?.[1];
+ const searchSource = source.match(
+ /const source[^=]*=\s*(\{[\s\S]*?\n\});/,
+ )?.[1];
+ if (tags == null || searchSource == null) {
+ throw new Error('TokenizerShowcase no longer declares `tags` and `source`');
+ }
+ const untyped = searchSource.replace(/\((\w+):\s*string\)/g, '($1)');
+ return new Function(`const tags = ${tags}; return ${untyped};`)();
+}
+
+describe('Tokenizer showcase block', () => {
+ it('lists the tags that match a typed query, ignoring case', async () => {
+ const {search, bootstrap} = loadSearchSource();
+ const labels = async query => (await search(query)).map(tag => tag.label);
+ expect(await labels('mar')).toEqual(['Marketing']);
+ expect(await labels('A')).toEqual(['Marketing', 'Research', 'Sales']);
+ expect((await bootstrap()).length).toBeGreaterThan(0);
+ });
+});
diff --git a/packages/cli/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx b/packages/cli/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx
index 52d838e4a1..304a096679 100644
--- a/packages/cli/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx
+++ b/packages/cli/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx
@@ -6,16 +6,23 @@ import {useState} from 'react';
import {Tokenizer} from '@astryxdesign/core/Tokenizer';
import type {SearchableItem, SearchSource} from '@astryxdesign/core/Typeahead';
+const tags: SearchableItem[] = [
+ {id: '1', label: 'Design'},
+ {id: '2', label: 'Engineering'},
+ {id: '3', label: 'Marketing'},
+ {id: '4', label: 'Product'},
+ {id: '5', label: 'Research'},
+ {id: '6', label: 'Sales'},
+];
+
const source: SearchSource = {
- search: () => [],
- bootstrap: () => [],
+ search: (query: string) =>
+ tags.filter(tag => tag.label.toLowerCase().includes(query.toLowerCase())),
+ bootstrap: () => tags,
};
export default function TokenizerShowcase() {
- const [value, setValue] = useState<SearchableItem[]>([
- {id: '1', label: 'Design'},
- {id: '2', label: 'Engineering'},
- ]);
+ const [value, setValue] = useState<SearchableItem[]>([tags[0], tags[1]]);
return (
<div style={{display: 'grid', gridTemplateColumns: 'minmax(0, 400px)'}}>
<Tokenizer