From 536d81fb6150710331b623d927d60278bde7f33b Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Fri, 28 Jan 2022 14:36:50 -0500 Subject: [PATCH] ISSUE-038 - Switched SlotSelector from using strings for onChange() to simply returning the selected index. As well as just being a little bit better this was necessary to support specifically typed values. We were always going to be selecting between strictly strings. This should allow us to be more flexible. Also, to avoid a minor bug with switching the Overlay tool with it being invisible SlotSelector now supports being disabled. --- .../Common/Inputs/SlotSelector/SlotSelector.tsx | 13 ++++++++++--- src/components/Common/Inputs/SlotSelector/styles.ts | 8 ++++++-- .../OverlayTool/OverlayControl/OverlayControl.tsx | 3 ++- .../AngledFlagPrototypeControls.tsx | 6 +++--- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx index 196d88e..518a784 100644 --- a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx +++ b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx @@ -6,10 +6,11 @@ import { SlotText, SlotWindow } from "./styles"; type SlotSelectorProps = { items: Array, - onChange: (value: string) => void, + onChange: (index: number) => void, removePrefix?: boolean, formatter?: (value: string) => string, initialIndex?: number, + disabled?: boolean, }; export function SlotSelector({ items, @@ -17,11 +18,16 @@ export function SlotSelector({ removePrefix = false, formatter = (x) => x, initialIndex = 0, + disabled = false, }: SlotSelectorProps) { const [index, setIndex] = useState(initialIndex); const [prefixMask, setPrefixMask] = useState(0); const [formattedItems, setFormattedItems] = useState>([]); + useEffect(() => { + setIndex(initialIndex); + }, [items]); + // useMemo makes these functions only run once when mounting. Not every re-render. useMemo(() => { const tempFormattedItems = items.map(item => formatter(item)); @@ -30,9 +36,10 @@ export function SlotSelector({ }, [items]); const onClick = () => { + if (disabled) return; const nextIndex = (index + 1) % items.length; setIndex(nextIndex); - onChange(items[nextIndex]); + onChange(nextIndex); }; const getItem = (i: number) => @@ -56,7 +63,7 @@ export function SlotSelector({ }, [index]); return ( - + {transitions((style, i) => ( {getItem(i)} ))} diff --git a/src/components/Common/Inputs/SlotSelector/styles.ts b/src/components/Common/Inputs/SlotSelector/styles.ts index 132395a..9e2b68f 100644 --- a/src/components/Common/Inputs/SlotSelector/styles.ts +++ b/src/components/Common/Inputs/SlotSelector/styles.ts @@ -1,12 +1,16 @@ import styled from "styled-components"; -export const SlotWindow = styled.div` +type SlotWindowProps = { + disabled: boolean, +}; +export const SlotWindow = styled.div` position: relative; height: 1.5em; width: auto; border-radius: 0.6em; - background-color: white; + background-color: ${({ disabled }) => disabled + ? p => p.theme.colors.lightGrey : "white"}; box-shadow: inset 0 0 50px -50px ${p => p.theme.colors.darkGrey}; border: 1px solid ${p => p.theme.colors.darkGrey}; diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index 261dd2d..b6b8c09 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -46,9 +46,10 @@ export function OverlayControl() { /> dispatch(setCurrentOverlayId(value))} + onChange={(index) => dispatch(setCurrentOverlayId(overlayIds[index]))} removePrefix formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")} + disabled={!visible} /> (value: string) => { + const onChange = (flag: "flagA" | "flagB") => (index: number) => { dispatch(setPrototypeState({ angledFlag: { - flagA: flag === "flagA" ? value : prototypeState?.angledFlag?.flagA ?? "GBR", - flagB: flag === "flagB" ? value : prototypeState?.angledFlag?.flagB ?? "GBR", + flagA: flag === "flagA" ? countries[index] : prototypeState?.angledFlag?.flagA ?? "GBR", + flagB: flag === "flagB" ? countries[index] : prototypeState?.angledFlag?.flagB ?? "GBR", }, })); };