Skip to content

Commit abfbb8b

Browse files
authored
fix(ui-components): add Search storybook examples and support rich hit descriptions (#9163)
1 parent ffe2a7b commit abfbb8b

2 files changed

Lines changed: 106 additions & 3 deletions

File tree

‎packages/ui-components/src/Common/Search/Results/Hit/index.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { DocumentTextIcon } from '@heroicons/react/24/outline';
22
import { SearchResults } from '@orama/ui/components';
33

4-
import type { LinkLike } from '#ui/types';
4+
import type { LinkLike, FormattedMessage } from '#ui/types';
55
import type { FC } from 'react';
66

77
import styles from './index.module.css';
88

99
type HitProps = {
1010
document: {
1111
title?: string;
12-
description?: string;
12+
description?: FormattedMessage;
1313
href: string;
1414
};
1515
as?: LinkLike;
@@ -21,7 +21,7 @@ const Hit: FC<HitProps> = ({ document, as: Link = 'a' }) => (
2121
<DocumentTextIcon />
2222
<div>
2323
{typeof document?.title === 'string' && <h3>{document.title}</h3>}
24-
{typeof document?.description === 'string' && (
24+
{document?.description && (
2525
<p className={styles.hitDescription}>{document.description}</p>
2626
)}
2727
</div>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { create, insertMultiple } from '@orama/orama';
2+
3+
import Search from '#ui/Common/Search';
4+
import SearchHit from '#ui/Common/Search/Results/Hit';
5+
6+
import type { OramaCloud } from '@orama/core';
7+
import type { Meta as MetaObj, StoryObj } from '@storybook/react-webpack5';
8+
9+
type Story = StoryObj<typeof Search>;
10+
type Meta = MetaObj<typeof Search>;
11+
12+
const searchClient = (() => {
13+
const db = create({
14+
schema: {
15+
title: 'string',
16+
description: 'string',
17+
href: 'string',
18+
siteSection: 'string',
19+
pageTitle: 'string',
20+
pageSectionTitle: 'string',
21+
pageSectionContent: 'string',
22+
},
23+
});
24+
25+
void insertMultiple(db, [
26+
{
27+
title: 'Getting Started',
28+
description: 'Learn how to install Node.js and run your first script.',
29+
href: '/en/learn/getting-started/introduction-to-nodejs',
30+
siteSection: 'Learn',
31+
pageTitle: 'Getting Started',
32+
pageSectionTitle: 'Introduction to Node.js',
33+
pageSectionContent: 'A quick overview of Node.js fundamentals.',
34+
},
35+
{
36+
title: 'Node.js Download',
37+
description: 'Download binaries for Linux, macOS and Windows.',
38+
href: '/en/download',
39+
siteSection: 'Download',
40+
pageTitle: 'Download',
41+
pageSectionTitle: 'Choose your platform',
42+
pageSectionContent: 'Download options for supported platforms.',
43+
},
44+
{
45+
title: 'About Node.js',
46+
description: 'History, governance and project overview.',
47+
href: '/en/about',
48+
siteSection: 'About',
49+
pageTitle: 'About',
50+
pageSectionTitle: 'Project overview',
51+
pageSectionContent: 'Community-driven runtime for JavaScript.',
52+
},
53+
]);
54+
55+
return db as unknown as OramaCloud;
56+
})();
57+
58+
export const Default: Story = {
59+
args: {
60+
client: searchClient,
61+
placeholder: 'Search the docs...',
62+
},
63+
decorators: [
64+
Story => (
65+
<div className="mx-auto w-full max-w-[48rem] p-6">
66+
<Story />
67+
</div>
68+
),
69+
],
70+
};
71+
72+
export const CustomLabels: Story = {
73+
args: {
74+
client: searchClient,
75+
placeholder: 'Search Node.js pages...',
76+
noResultsTitle: 'Nothing found for',
77+
closeShortcutLabel: 'close search',
78+
navigateShortcutLabel: 'move between results',
79+
selectShortcutLabel: 'open selected page',
80+
},
81+
decorators: Default.decorators,
82+
};
83+
84+
export const DescriptionWithCode: Story = {
85+
render: () => (
86+
<div className="mx-auto w-full max-w-[48rem] p-6">
87+
<SearchHit
88+
document={{
89+
title: 'Run with watch mode',
90+
href: '/en/learn/command-line/run-nodejs-scripts-from-the-command-line',
91+
description: (
92+
<>
93+
Use <code>node --watch app.mjs</code> to restart your app
94+
automatically on file changes.
95+
</>
96+
),
97+
}}
98+
/>
99+
</div>
100+
),
101+
};
102+
103+
export default { component: Search } as Meta;

0 commit comments

Comments
 (0)