From 612e549ddc090dae308a648fbd76e1aa4a6728f4 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 1 Sep 2020 17:49:12 -0700 Subject: [PATCH] extract SearchToolbar into a separate file This is in preparation for extending it with more UI stuff to auto-suggest zones! --- src/app/WardrobePage/ItemsAndSearchPanels.js | 77 +------------------- src/app/WardrobePage/SearchToolbar.js | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 75 deletions(-) create mode 100644 src/app/WardrobePage/SearchToolbar.js diff --git a/src/app/WardrobePage/ItemsAndSearchPanels.js b/src/app/WardrobePage/ItemsAndSearchPanels.js index b636a08..469780c 100644 --- a/src/app/WardrobePage/ItemsAndSearchPanels.js +++ b/src/app/WardrobePage/ItemsAndSearchPanels.js @@ -1,17 +1,8 @@ import React from "react"; -import { - Box, - Flex, - IconButton, - Input, - InputGroup, - InputLeftElement, - InputRightElement, - useColorModeValue, -} from "@chakra-ui/core"; -import { CloseIcon, SearchIcon } from "@chakra-ui/icons"; +import { Box, Flex } from "@chakra-ui/core"; import ItemsPanel from "./ItemsPanel"; +import SearchToolbar from "./SearchToolbar"; import SearchPanel from "./SearchPanel"; /** @@ -82,68 +73,4 @@ function ItemsAndSearchPanels({ loading, outfitState, dispatchToOutfit }) { ); } -/** - * SearchToolbar is rendered above both the ItemsPanel and the SearchPanel, - * and contains the search field where the user types their query. - * - * It has some subtle keyboard interaction support, like DownArrow to go to the - * first search result, and Escape to clear the search and go back to the - * ItemsPanel. (The SearchPanel can also send focus back to here, with Escape - * from anywhere, or UpArrow from the first result!) - */ -function SearchToolbar({ - query, - searchQueryRef, - firstSearchResultRef, - onChange, -}) { - const onMoveFocusDownToResults = (e) => { - if (firstSearchResultRef.current) { - firstSearchResultRef.current.focus(); - e.preventDefault(); - } - }; - - const focusBorderColor = useColorModeValue("green.600", "green.400"); - - return ( - - - - - onChange(e.target.value)} - onKeyDown={(e) => { - if (e.key === "Escape") { - onChange(""); - e.target.blur(); - } else if (e.key === "ArrowDown") { - onMoveFocusDownToResults(e); - } - }} - /> - {query && ( - - } - color="gray.400" - variant="ghost" - colorScheme="green" - aria-label="Clear search" - onClick={() => onChange("")} - // Big style hacks here! - height="calc(100% - 2px)" - marginRight="2px" - /> - - )} - - ); -} - export default ItemsAndSearchPanels; diff --git a/src/app/WardrobePage/SearchToolbar.js b/src/app/WardrobePage/SearchToolbar.js new file mode 100644 index 0000000..27b6448 --- /dev/null +++ b/src/app/WardrobePage/SearchToolbar.js @@ -0,0 +1,76 @@ +import React from "react"; +import { + IconButton, + Input, + InputGroup, + InputLeftElement, + InputRightElement, + useColorModeValue, +} from "@chakra-ui/core"; +import { CloseIcon, SearchIcon } from "@chakra-ui/icons"; + +/** + * SearchToolbar is rendered above both the ItemsPanel and the SearchPanel, + * and contains the search field where the user types their query. + * + * It has some subtle keyboard interaction support, like DownArrow to go to the + * first search result, and Escape to clear the search and go back to the + * ItemsPanel. (The SearchPanel can also send focus back to here, with Escape + * from anywhere, or UpArrow from the first result!) + */ +function SearchToolbar({ + query, + searchQueryRef, + firstSearchResultRef, + onChange, +}) { + const onMoveFocusDownToResults = (e) => { + if (firstSearchResultRef.current) { + firstSearchResultRef.current.focus(); + e.preventDefault(); + } + }; + + const focusBorderColor = useColorModeValue("green.600", "green.400"); + + return ( + + + + + onChange(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Escape") { + onChange(""); + e.target.blur(); + } else if (e.key === "ArrowDown") { + onMoveFocusDownToResults(e); + } + }} + /> + {query && ( + + } + color="gray.400" + variant="ghost" + colorScheme="green" + aria-label="Clear search" + onClick={() => onChange("")} + // Big style hacks here! + height="calc(100% - 2px)" + marginRight="2px" + /> + + )} + + ); +} + +export default SearchToolbar;