diff --git a/build.washingtonpost.com/docs/components/input-search.mdx b/build.washingtonpost.com/docs/components/input-search.mdx
index 6aa037e19..a443e95dd 100644
--- a/build.washingtonpost.com/docs/components/input-search.mdx
+++ b/build.washingtonpost.com/docs/components/input-search.mdx
@@ -1169,8 +1169,8 @@ export default function Example() {
{results.length > 0 ? (
<>
-
- {results.slice(0, 5).map((result) => (
+
+ {results.map((result) => (
))}
@@ -1185,7 +1185,11 @@ export default function Example() {
}}
>
-
+
- Recommended
+ Recommended?
@@ -1247,7 +1250,6 @@ export default function Example() {
css={{
display: "block",
marginBlockEnd: "$075",
- marginInline: "$050",
}}
/>
+ let results1 = searchBooks(term1);
+ if (results1 && ebooksChecked) {
+ results1 = results1.filter((book) =>
book.format === "ebook" ? true : false
);
}
+ const [term2, setTerm2] = useState("");
+ const results2 = searchBooks(term2);
+
return (
-
-
- {
- setTerm(event.target.value);
- }}
- label="Search Library"
- />
- {results && (
-
- {results.length > 0 ? (
- <>
-
- setEbooksChecked(!ebooksChecked)}
+
+
+
+ {
+ setTerm1(event.target.value);
+ }}
+ label="Search Library"
+ />
+ {results1 && (
+
+ {results1.length > 0 ? (
+ <>
+
-
-
-
-
- Show only ebooks
-
-
-
-
- {results.map((book) =>
- book.format === "paper" ? (
-
- ) : (
-
- setEbooksChecked(!ebooksChecked)}
+ >
+
+
+
+
+ Show only ebooks
+
+
+
+
+ {results1.map((book) =>
+ book.format === "paper" ? (
+
+ ) : (
+
-
- {" "}
- {`${book.title} by ${book.author}`}
-
- )
- )}
-
- >
- ) : (
-
- )}
-
- )}
-
+
+
+ {" "}
+ {`${book.title} by ${book.author}`}
+
+ )
+ )}
+
+ >
+ ) : (
+
+ )}
+
+ )}
+
+
+
+
+ {
+ setTerm2(event.target.value);
+ }}
+ label="Search Library"
+ />
+ {results2 && (
+
+
+
+ All Results
+ Ebooks
+ Paperback
+
+
+
+ {results2.map((book) =>
+ book.format === "paper" ? (
+
+ ) : (
+
+
+
+ {" "}
+ {`${book.title} by ${book.author}`}
+
+ )
+ )}
+
+
+
+
+ {results2.map((book) =>
+ book.format === "ebook" ? (
+
+
+
+ {" "}
+ {`${book.title} by ${book.author}`}
+
+ ) : null
+ )}
+
+
+
+
+ {results2.map((book) =>
+ book.format === "paper" ? (
+
+ ) : null
+ )}
+
+
+
+
+ )}
+
+
);
}
@@ -1721,8 +1819,111 @@ export default function Example() {
There might be instances where a dedicated search view might be more appropriate. Utilizing the pattern of our drawer can be an effective way to do this.
-```jsx withPreview isGuide="error"
+```jsx withPreview demoHeight="430" isGuide="information"
+export default function Example() {
+ const cities = [
+ { name: "Atlanta", state: "Georgia" },
+ { name: "Austin", state: "Texas" },
+ { name: "Boston", state: "Massachusetts" },
+ { name: "Charlotte", state: "North Carolina" },
+ { name: "Chicago", state: "Illinois" },
+ { name: "Dallas", state: "Texas" },
+ { name: "Denver", state: "Colorado" },
+ { name: "Detroit", state: "Michigan" },
+ { name: "Houston", state: "Texas" },
+ { name: "Las Vegas", state: "Nevada" },
+ { name: "Miami", state: "Florida" },
+ { name: "Nashville", state: "Tennessee" },
+ { name: "New Orleans", state: "Louisiana" },
+ { name: "New York", state: "New York" },
+ { name: "Philadelphia", state: "Pennsylvania" },
+ { name: "Phoenix", state: "Arizona" },
+ { name: "Portland", state: "Oregon" },
+ { name: "San Diego", state: "California" },
+ { name: "San Francisco", state: "California" },
+ { name: "Seattle", state: "Washington" },
+ { name: "Washington", state: "D.C." },
+ ];
+ function searchCities(term) {
+ if (term === "") {
+ return;
+ }
+ return cities.filter(
+ (city) =>
+ city.name.toLowerCase().includes(term.toLowerCase()) ||
+ city.state.toLowerCase().includes(term.toLowerCase())
+ );
+ }
+
+ const [term, setTerm] = useState("");
+ const results = searchCities(term);
+
+ const [open, setOpen] = useState(false);
+
+ return (
+ setOpen(open)}
+ >
+
+
+
+
+ {`Find your city`}
+
+
+
+
+
+ {
+ setTerm(event.target.value);
+ }}
+ autocomplete={false}
+ />
+ {results && (
+
+ {results.length > 0 ? (
+
+ {results.map((city) => (
+
+ ))}
+
+ ) : (
+
+ )}
+
+ )}
+
+
+
+ );
+}
```
---
diff --git a/build.washingtonpost.com/pages/resources/working-examples.tsx b/build.washingtonpost.com/pages/resources/working-examples.tsx
index 8a829b9c5..8ffca953b 100644
--- a/build.washingtonpost.com/pages/resources/working-examples.tsx
+++ b/build.washingtonpost.com/pages/resources/working-examples.tsx
@@ -2,6 +2,7 @@ import * as React from "react";
import { useState } from "react";
import { useForm, Controller } from "react-hook-form";
import { isPossiblePhoneNumber } from "react-phone-number-input";
+import { matchSorter } from "match-sorter";
import {
Box,
Button,
@@ -17,6 +18,8 @@ import {
styled,
} from "@washingtonpost/wpds-ui-kit";
+import { cities } from "@washingtonpost/wpds-input-search/src/cities";
+
const STATES = [
"Alabama",
"Alaska",
@@ -128,6 +131,7 @@ const Form = () => {
handleSubmit,
register,
reset,
+ clearErrors,
watch,
} = useForm({});
@@ -137,6 +141,22 @@ const Form = () => {
const [checked, setChecked] = useState(false);
+ const useCityMatch = (term: string) => {
+ return React.useMemo(
+ () =>
+ term.trim() === ""
+ ? null
+ : matchSorter(cities, term, {
+ keys: [(item) => `${item.city}, ${item.state}`],
+ }),
+ [term]
+ );
+ };
+
+ const [term, setTerm] = React.useState("");
+
+ const results: { city: string; state: string }[] | null = useCityMatch(term);
+
return (
<>
Form Example
@@ -147,21 +167,6 @@ const Form = () => {