Skip to content

Commit 6c919ba

Browse files
authored
Feat/improve search example (#56)
* feat: include empty state & no results views in search example * fix(docs): update SDK Explorer url
1 parent 5a112f1 commit 6c919ba

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

apps/sdk-explorer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SDK Explorer
22

3-
👉🏻 **[https://sdk-examples.sanity.dev](https://sdk-examples.sanity.dev)**
3+
👉🏻 **[https://sdk-explorer.sanity.io](https://sdk-explorer.sanity.io)**
44

55
The Sanity App SDK Explorer contains an assortment of example interfaces built with our React SDK’s hooks. The purpose of the Explorer is to demonstrate how these hooks can be used to build out interfaces powered by Sanity, with a variety of approaches to styling.
66

apps/sdk-explorer/src/examples/document-collections/DocumentSearch/DocumentSearch.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,45 @@ export default function DocumentSearch(): JSX.Element {
4343
// Create a state variable for keeping track of the search input’s value
4444
const [search, setSearch] = useState('')
4545

46-
const {data: movies} = useDocuments({
46+
const {data: movies, count} = useDocuments({
4747
documentType: 'movie',
4848
// Pass the `search` state variable to the `useDocuments` hook’s `search` parameter
4949
search,
5050
})
5151

52+
function EmptyState() {
53+
return (
54+
<Card tone="primary" radius={2} shadow={1} padding={4}>
55+
Start typing in the input above to search for movies
56+
</Card>
57+
)
58+
}
59+
60+
function NoResults() {
61+
return (
62+
<Card tone="caution" radius={2} shadow={1} padding={4}>
63+
We couldn’t find any movies with content matching the search term ‘{search}’. Try something
64+
else?
65+
</Card>
66+
)
67+
}
68+
69+
function Results() {
70+
return movies.map((movie) => (
71+
<Suspense key={movie.documentId}>
72+
<DocumentResult documentHandle={movie} />
73+
</Suspense>
74+
))
75+
}
76+
77+
let Component
78+
79+
if (search === '') {
80+
Component = EmptyState
81+
} else if (count === 0) {
82+
Component = NoResults
83+
} else Component = Results
84+
5285
return (
5386
<ExampleLayout
5487
title="Document search"
@@ -57,7 +90,7 @@ export default function DocumentSearch(): JSX.Element {
5790
styling="Sanity UI"
5891
summary="This example passes a state variable to the useDocuments hook’s ‘search’ argument, enabling the creation of a dynamic search interface for documents in the targeted dataset(s). (Note: the ‘search’ parameter currently searches for matches across all of a document’s string fields.)"
5992
>
60-
<Stack space={5}>
93+
<Stack space={5} marginBottom={4}>
6194
<Stack space={3}>
6295
<Label htmlFor="movieSearch">Enter your search term:</Label>
6396
{/* Use a search input to set the value of the `search` state variable */}
@@ -71,13 +104,7 @@ export default function DocumentSearch(): JSX.Element {
71104
/>
72105
</Stack>
73106

74-
{search === ''
75-
? ''
76-
: movies.map((movie) => (
77-
<Suspense key={movie.documentId}>
78-
<DocumentResult documentHandle={movie} />
79-
</Suspense>
80-
))}
107+
<Component />
81108
</Stack>
82109
</ExampleLayout>
83110
)

0 commit comments

Comments
 (0)