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.
This commit is contained in:
1 parent
8a44d7af65
commit
536d81fb61
4 files changed
+21
-9
No files matched your search
@@ -6,10 +6,11 @@ import { SlotText, SlotWindow } from "./styles";
|
|||||||
|
|
||||||
type SlotSelectorProps = {
|
type SlotSelectorProps = {
|
||||||
items: Array<string>,
|
items: Array<string>,
|
||||||
onChange: (value: string) => void,
|
onChange: (index: number) => void,
|
||||||
removePrefix?: boolean,
|
removePrefix?: boolean,
|
||||||
formatter?: (value: string) => string,
|
formatter?: (value: string) => string,
|
||||||
initialIndex?: number,
|
initialIndex?: number,
|
||||||
|
disabled?: boolean,
|
||||||
};
|
};
|
||||||
export function SlotSelector({
|
export function SlotSelector({
|
||||||
items,
|
items,
|
||||||
@@ -17,11 +18,16 @@ export function SlotSelector({
|
|||||||
removePrefix = false,
|
removePrefix = false,
|
||||||
formatter = (x) => x,
|
formatter = (x) => x,
|
||||||
initialIndex = 0,
|
initialIndex = 0,
|
||||||
|
disabled = false,
|
||||||
}: SlotSelectorProps) {
|
}: SlotSelectorProps) {
|
||||||
const [index, setIndex] = useState(initialIndex);
|
const [index, setIndex] = useState(initialIndex);
|
||||||
const [prefixMask, setPrefixMask] = useState(0);
|
const [prefixMask, setPrefixMask] = useState(0);
|
||||||
const [formattedItems, setFormattedItems] = useState<Array<string>>([]);
|
const [formattedItems, setFormattedItems] = useState<Array<string>>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setIndex(initialIndex);
|
||||||
|
}, [items]);
|
||||||
|
|
||||||
// useMemo makes these functions only run once when mounting. Not every re-render.
|
// useMemo makes these functions only run once when mounting. Not every re-render.
|
||||||
useMemo(() => {
|
useMemo(() => {
|
||||||
const tempFormattedItems = items.map(item => formatter(item));
|
const tempFormattedItems = items.map(item => formatter(item));
|
||||||
@@ -30,9 +36,10 @@ export function SlotSelector({
|
|||||||
}, [items]);
|
}, [items]);
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
|
if (disabled) return;
|
||||||
const nextIndex = (index + 1) % items.length;
|
const nextIndex = (index + 1) % items.length;
|
||||||
setIndex(nextIndex);
|
setIndex(nextIndex);
|
||||||
onChange(items[nextIndex]);
|
onChange(nextIndex);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getItem = (i: number) =>
|
const getItem = (i: number) =>
|
||||||
@@ -56,7 +63,7 @@ export function SlotSelector({
|
|||||||
}, [index]);
|
}, [index]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SlotWindow onClick={onClick}>
|
<SlotWindow onClick={onClick} disabled={disabled}>
|
||||||
{transitions((style, i) => (
|
{transitions((style, i) => (
|
||||||
<SlotText as={animated.span} style={style}>{getItem(i)}</SlotText>
|
<SlotText as={animated.span} style={style}>{getItem(i)}</SlotText>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
|
|
||||||
export const SlotWindow = styled.div`
|
type SlotWindowProps = {
|
||||||
|
disabled: boolean,
|
||||||
|
};
|
||||||
|
export const SlotWindow = styled.div<SlotWindowProps>`
|
||||||
position: relative;
|
position: relative;
|
||||||
height: 1.5em;
|
height: 1.5em;
|
||||||
width: auto;
|
width: auto;
|
||||||
|
|
||||||
border-radius: 0.6em;
|
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};
|
box-shadow: inset 0 0 50px -50px ${p => p.theme.colors.darkGrey};
|
||||||
border: 1px solid ${p => p.theme.colors.darkGrey};
|
border: 1px solid ${p => p.theme.colors.darkGrey};
|
||||||
|
|
||||||
|
|||||||
@@ -46,9 +46,10 @@ export function OverlayControl() {
|
|||||||
/>
|
/>
|
||||||
<SlotSelector
|
<SlotSelector
|
||||||
items={overlayIds}
|
items={overlayIds}
|
||||||
onChange={(value) => dispatch(setCurrentOverlayId(value))}
|
onChange={(index) => dispatch(setCurrentOverlayId(overlayIds[index]))}
|
||||||
removePrefix
|
removePrefix
|
||||||
formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")}
|
formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")}
|
||||||
|
disabled={!visible}
|
||||||
/>
|
/>
|
||||||
</Inline>
|
</Inline>
|
||||||
<ProgressSlider
|
<ProgressSlider
|
||||||
|
|||||||
+3
-3
@@ -16,11 +16,11 @@ export function AngledFlagPrototypeControls() {
|
|||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const { prototypeState } = useSelector(workspaceSelector);
|
const { prototypeState } = useSelector(workspaceSelector);
|
||||||
|
|
||||||
const onChange = (flag: "flagA" | "flagB") => (value: string) => {
|
const onChange = (flag: "flagA" | "flagB") => (index: number) => {
|
||||||
dispatch(setPrototypeState({
|
dispatch(setPrototypeState({
|
||||||
angledFlag: {
|
angledFlag: {
|
||||||
flagA: flag === "flagA" ? value : prototypeState?.angledFlag?.flagA ?? "GBR",
|
flagA: flag === "flagA" ? countries[index] : prototypeState?.angledFlag?.flagA ?? "GBR",
|
||||||
flagB: flag === "flagB" ? value : prototypeState?.angledFlag?.flagB ?? "GBR",
|
flagB: flag === "flagB" ? countries[index] : prototypeState?.angledFlag?.flagB ?? "GBR",
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user