From 10563629ef3a8b8f7a10f1a728a19940c4f611c6 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 1 Sep 2020 19:11:33 -0700 Subject: [PATCH] misc zone search fixes & improvements --- src/app/WardrobePage/ItemsAndSearchPanels.js | 12 +++++++-- src/app/WardrobePage/SearchToolbar.js | 28 +++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/app/WardrobePage/ItemsAndSearchPanels.js b/src/app/WardrobePage/ItemsAndSearchPanels.js index da90fa8..b6f119d 100644 --- a/src/app/WardrobePage/ItemsAndSearchPanels.js +++ b/src/app/WardrobePage/ItemsAndSearchPanels.js @@ -4,6 +4,9 @@ import { Box, Flex } from "@chakra-ui/core"; import ItemsPanel from "./ItemsPanel"; import SearchToolbar from "./SearchToolbar"; import SearchPanel from "./SearchPanel"; +import { isNullableType } from "graphql"; + +const emptyQuery = { value: "", filterToZoneLabel: null }; /** * ItemsAndSearchPanels manages the shared layout and state for: @@ -19,11 +22,16 @@ import SearchPanel from "./SearchPanel"; * state and refs. */ function ItemsAndSearchPanels({ loading, outfitState, dispatchToOutfit }) { - const [searchQuery, setSearchQuery] = React.useState(null); + const [searchQuery, setSearchQuery] = React.useState(emptyQuery); const scrollContainerRef = React.useRef(); const searchQueryRef = React.useRef(); const firstSearchResultRef = React.useRef(); + const onChange = React.useCallback( + (newQuery) => setSearchQuery(newQuery || emptyQuery), + [setSearchQuery] + ); + return ( @@ -34,7 +42,7 @@ function ItemsAndSearchPanels({ loading, outfitState, dispatchToOutfit }) { onChange={setSearchQuery} /> - {searchQuery ? ( + {searchQuery.value || searchQuery.filterToZoneLabel ? ( zl} - shouldRenderSuggestions={() => query?.filterToZoneLabel == null} + shouldRenderSuggestions={() => query.filterToZoneLabel == null} renderSuggestion={renderSuggestion} renderInputComponent={(props) => ( - {query?.filterToZoneLabel ? ( + {query.filterToZoneLabel ? ( {query.filterToZoneLabel} @@ -119,12 +120,23 @@ function SearchToolbar({ // placeholder: "Search for items to add…", "aria-label": "Search for items to add…", focusBorderColor: focusBorderColor, - value: query?.value || "", + value: query.value || "", ref: searchQueryRef, minWidth: 0, // HACK: Chakra isn't noticing the InputLeftElement swapping out - // for the InputLeftAddon, so the padding isn't updating. - paddingLeft: query?.filterToZoneLabel ? "1rem" : "2.5rem", + // for the InputLeftAddon, so the styles aren't updating... + // Hard override! + className: css` + padding-left: ${query.filterToZoneLabel + ? "1rem" + : "2.5rem"} !important; + border-bottom-left-radius: ${query.filterToZoneLabel + ? "0" + : "0.25rem"} !important; + border-top-left-radius: ${query.filterToZoneLabel + ? "0" + : "0.25rem"} !important; + `, onChange: (e, { newValue, method }) => { // The Autosuggest tries to change the _entire_ value of the element // when navigating suggestions, which isn't actually what we want. @@ -146,10 +158,8 @@ function SearchToolbar({ return; } onMoveFocusDownToResults(e); - } else if (e.key === "Backspace") { - if (query.value === "") { - onChange({ ...query, filterToZoneLabel: null }); - } + } else if (e.key === "Backspace" && e.target.selectionStart === 0) { + onChange({ ...query, filterToZoneLabel: null }); } }, }}