From def195af9318f04c2f8aba04e8173983b351b1db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 19 Jul 2024 16:51:12 +0100 Subject: [PATCH] feat: Setup formik --- .../FlowEditor/components/Sidebar/Search.tsx | 134 ++++++++++-------- 1 file changed, 73 insertions(+), 61 deletions(-) diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search.tsx index 4c7dedcf26..516121f920 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search.tsx @@ -7,12 +7,13 @@ import { styled } from "@mui/material/styles"; import Typography from "@mui/material/Typography"; import { ComponentType, IndexedNode } from "@opensystemslab/planx-core/types"; import { ICONS } from "@planx/components/ui"; +import { useFormik } from "formik"; import type { SearchResult, SearchResults } from "hooks/useSearch"; import { useSearch } from "hooks/useSearch"; import { capitalize, get } from "lodash"; import { SLUGS } from "pages/FlowEditor/data/types"; import { useStore } from "pages/FlowEditor/lib/store"; -import React, { ChangeEvent, useEffect } from "react"; +import React, { useEffect } from "react"; import { useNavigation } from "react-navi"; import { FONT_WEIGHT_BOLD, FONT_WEIGHT_SEMI_BOLD } from "theme"; import ChecklistItem from "ui/shared/ChecklistItem"; @@ -48,8 +49,11 @@ const SearchResults: React.FC<{ results: SearchResults }> = ({ return ( <> - {results.length} {results.length > 1 ? "results" : "result"}: + {!results.length && "No matches found"} + {results.length === 1 && "1 result:"} + {results.length > 1 && `${results.length} results:`} + {results.map((result) => ( @@ -83,6 +87,7 @@ const Headline: React.FC = ({ text, matchIndices, variant }) => { variant === "data" ? '"Source Code Pro", monospace' : "inherit" } variant="body2" + display={"inline-block"} > {char} @@ -131,6 +136,7 @@ const SearchResultCard: React.FC<{ result: SearchResult }> = ({ const handleClick = () => { console.log("todo!"); + console.log({ nodeId: result.item.id }) // get path for node // generate url from path // navigate to url @@ -180,36 +186,41 @@ const ExternalPortalList: React.FC = () => { if (!hasExternalPortals) return null; return ( - + Your service also contains the following external portals, which have not been searched: - {Object.values(externalPortals).map(({ name, href }) => ( - - navigate("../" + href)}> - - External portal • - - - {href.replaceAll("/", " / ")} - - - - ))} + {Object.values(externalPortals).map(({ name, href }) => ( + + navigate("../" + href)}> + + External portal • + + + {href.replaceAll("/", " / ")} + + + + ))} ); }; +interface SearchNodes { + input: string; + facets: ["data.fn", "data.val"], +} + const Search: React.FC = () => { const [orderedFlow, setOrderedFlow] = useStore((state) => [ state.orderedFlow, @@ -220,52 +231,53 @@ const Search: React.FC = () => { if (!orderedFlow) setOrderedFlow(); }, [setOrderedFlow]); - /** Map of search facets to associated node keys */ - const facets = { - data: ["data.fn", "data.val"], - }; + const formik = useFormik({ + initialValues: { input: "", facets: ["data.fn", "data.val"] }, + onSubmit: ({ input }) => { search(input) }, + }); const { results, search } = useSearch({ list: orderedFlow || [], - keys: facets.data, + keys: formik.values.facets }); - const handleChange = (e: ChangeEvent) => { - const input = e.target.value; - search(input); - }; - return ( - - Search this flow and internal portals - - - {}} - /> - - {results.length > 0 && ( - <> - - - - )} - +
+ + Search this flow and internal portals + + { + formik.setFieldValue("input", e.target.value) + formik.handleSubmit(); + }} + inputProps={{ spellCheck: false }} + /> + { }} + /> + + {formik.values.input && ( + <> + + + + )} + +
); };