diff --git a/src/app/WardrobePage/SearchPanel.js b/src/app/WardrobePage/SearchPanel.js index 9806a94..b733d79 100644 --- a/src/app/WardrobePage/SearchPanel.js +++ b/src/app/WardrobePage/SearchPanel.js @@ -98,38 +98,30 @@ function SearchResults({ // Background we want to restore the previous one! const [itemIdsToReconsider] = React.useState(outfitState.wornItemIds); - // When the checkbox changes, we should wear/unwear the item! - const onChange = (e) => { - const itemId = e.target.value; - const willBeWorn = e.target.checked; - if (willBeWorn) { - dispatchToOutfit({ type: "wearItem", itemId, itemIdsToReconsider }); - } else { - dispatchToOutfit({ type: "unwearItem", itemId, itemIdsToReconsider }); - } - }; - // You can use UpArrow/DownArrow to navigate between items, and even back up // to the search field! - const goToPrevItem = (e) => { - const prevLabel = e.target.closest("label").previousSibling; - if (prevLabel) { - prevLabel.querySelector("input[type=checkbox]").focus(); - prevLabel.scrollIntoView({ block: "center" }); - e.preventDefault(); - } else { - // If we're at the top of the list, move back up to the search box! - onMoveFocusUpToQuery(e); - } - }; - const goToNextItem = (e) => { + const goToPrevItem = React.useCallback( + (e) => { + const prevLabel = e.target.closest("label").previousSibling; + if (prevLabel) { + prevLabel.querySelector("input[type=checkbox]").focus(); + prevLabel.scrollIntoView({ block: "center" }); + e.preventDefault(); + } else { + // If we're at the top of the list, move back up to the search box! + onMoveFocusUpToQuery(e); + } + }, + [onMoveFocusUpToQuery] + ); + const goToNextItem = React.useCallback((e) => { const nextLabel = e.target.closest("label").nextSibling; if (nextLabel) { nextLabel.querySelector("input[type=checkbox]").focus(); nextLabel.scrollIntoView({ block: "center" }); e.preventDefault(); } - }; + }, []); // If the results aren't ready, we have some special case UI! if (loading) { @@ -167,38 +159,17 @@ function SearchResults({ <> {items.map((item, index) => ( - + ))} {loadingMore && } @@ -206,6 +177,70 @@ function SearchResults({ ); } +function SearchResultItem({ + item, + itemIdsToReconsider, + isWorn, + isInOutfit, + dispatchToOutfit, + checkboxRef, + goToPrevItem, + goToNextItem, +}) { + // It's important to use `useCallback` for `onRemove`, to avoid re-rendering + // the whole list of s! + const onRemove = React.useCallback( + () => + dispatchToOutfit({ + type: "removeItem", + itemId: item.id, + itemIdsToReconsider, + }), + [item.id, itemIdsToReconsider, dispatchToOutfit] + ); + + return ( + + ); +} + /** * useSearchResults manages the actual querying and state management of search! * It's hefty, infinite-scroll pagination is a bit of a thing!