diff --git a/site/docs/components/combo-box/examples.mdx b/site/docs/components/combo-box/examples.mdx index e12201ea6f1..a5585e4af94 100644 --- a/site/docs/components/combo-box/examples.mdx +++ b/site/docs/components/combo-box/examples.mdx @@ -155,7 +155,7 @@ Due to its flexible API, you can customize the filtering logic `ComboBox` uses t ## Server side data -You can use `ComboBox` to display data from a server. In this example, the data is fetched and filtered on the server side. +You can use `ComboBox` to display data from a server. In this example, a list of states is fetched from a JSON file and filtered on the client side. Note that the data fetching in this example has been made intentionally slow to highlight the loading states. diff --git a/site/src/examples/combo-box/ServerSideData.tsx b/site/src/examples/combo-box/ServerSideData.tsx index 45814b3b70a..3e9e1186862 100644 --- a/site/src/examples/combo-box/ServerSideData.tsx +++ b/site/src/examples/combo-box/ServerSideData.tsx @@ -1,5 +1,12 @@ import { ChangeEvent, ReactElement, SyntheticEvent, useState } from "react"; -import { ComboBox, Option, Spinner } from "@salt-ds/core"; +import { + ComboBox, + FormField, + FormFieldLabel, + FormFieldHelperText, + Option, + Spinner, +} from "@salt-ds/core"; import useSWR from "swr"; import styles from "./index.module.css"; @@ -17,7 +24,7 @@ const fetcher = async (url: string, filter: string) => { export const ServerSideData = (): ReactElement => { const [value, setValue] = useState(""); const { data, isLoading } = useSWR( - `/example-data/states.json?s=${value}`, + value.length > 2 ? `/example-data/states.json?s=${value}` : null, (url: string) => fetcher(url, value), { fallbackData: [], @@ -42,25 +49,42 @@ export const ServerSideData = (): ReactElement => { } }; + const hasResults = Array.isArray(data) && data.length > 0; + return ( - } - > - {!loading ? ( - data?.map((color) => + + State + } + > + {!loading && hasResults + ? data?.map((state) => + + Please enter more than 2 characters to search. + + ); };