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:
Kyle Geib committed 2022-01-28 14:36:50 -05:00
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 = {
items: Array<string>,
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<Array<string>>([]);
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 (
<SlotWindow onClick={onClick}>
<SlotWindow onClick={onClick} disabled={disabled}>
{transitions((style, i) => (
<SlotText as={animated.span} style={style}>{getItem(i)}</SlotText>
))}
@@ -1,12 +1,16 @@
import styled from "styled-components";
export const SlotWindow = styled.div`
type SlotWindowProps = {
disabled: boolean,
};
export const SlotWindow = styled.div<SlotWindowProps>`
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};