ISSUE-020 - Plain, static SlotSelector component defined. Not too fancy looking.
To save space and reduce redundant data I created a utility function called findPrefixCount() and wrote a suite of tests for it.
This commit is contained in:
1 parent
6a8e5f28b2
commit
35ca424dd6
6 files changed
+163
-20
No files matched your search
@@ -0,0 +1,49 @@
|
|||||||
|
import { useMemo, useState } from "react";
|
||||||
|
import { findPrefixCount } from "../../../../utils/strings";
|
||||||
|
import { SlotText, SlotWindow } from "./styles";
|
||||||
|
|
||||||
|
type SlotSelectorProps = {
|
||||||
|
items: Array<string>,
|
||||||
|
onChange: (value: string) => void, // eslint-disable-line no-unused-vars
|
||||||
|
removePrefix?: boolean,
|
||||||
|
formatter?: (value: string) => string, // eslint-disable-line no-unused-vars
|
||||||
|
initialIndex?: number,
|
||||||
|
}
|
||||||
|
export function SlotSelector({
|
||||||
|
items,
|
||||||
|
onChange,
|
||||||
|
removePrefix = false,
|
||||||
|
formatter = (x) => x,
|
||||||
|
initialIndex = 0,
|
||||||
|
}: SlotSelectorProps) {
|
||||||
|
const [index, setIndex] = useState(initialIndex);
|
||||||
|
const [prefixMask, setPrefixMask] = useState(0);
|
||||||
|
const [formattedItems, setFormattedItems] = useState<Array<string>>([]);
|
||||||
|
|
||||||
|
useMemo(() => {
|
||||||
|
const tempFormattedItems = items.map(item => formatter(item));
|
||||||
|
setFormattedItems(tempFormattedItems);
|
||||||
|
setPrefixMask(findPrefixCount(tempFormattedItems));
|
||||||
|
}, [items]);
|
||||||
|
|
||||||
|
const onClick = () => {
|
||||||
|
const nextIndex = (index + 1) % items.length;
|
||||||
|
setIndex(nextIndex);
|
||||||
|
onChange(items[nextIndex]);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SlotWindow
|
||||||
|
onClick={onClick}
|
||||||
|
>
|
||||||
|
<SlotText>
|
||||||
|
{formattedItems.length &&
|
||||||
|
removePrefix && formattedItems[0].length !== prefixMask ?
|
||||||
|
formattedItems[index].slice(prefixMask)
|
||||||
|
:
|
||||||
|
formattedItems[index]
|
||||||
|
}
|
||||||
|
</SlotText>
|
||||||
|
</SlotWindow>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from "./SlotSelector";
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import styled from "styled-components";
|
||||||
|
|
||||||
|
export const SlotWindow = styled.div`
|
||||||
|
position: relative;
|
||||||
|
height: 1.2em;
|
||||||
|
height: 100%;
|
||||||
|
width: 200px;
|
||||||
|
|
||||||
|
border-radius: 0.6em;
|
||||||
|
background-color: white;
|
||||||
|
box-shadow: inset 0 0 50px -50px ${p => p.theme.colors.darkGrey};
|
||||||
|
border: 1px solid ${p => p.theme.colors.darkGrey};
|
||||||
|
|
||||||
|
cursor: pointer;
|
||||||
|
|
||||||
|
overflow: hidden;
|
||||||
|
white-space: nowrap;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const SlotText = styled.span`
|
||||||
|
margin-left: .4em;
|
||||||
|
position: relative;
|
||||||
|
font-family: ${p => p.theme.fonts.f1Regular};
|
||||||
|
color: ${p => p.theme.colors.darkGrey};
|
||||||
|
user-select: none;
|
||||||
|
`;
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import { useState } from "react";
|
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { Inline } from "@bedrock-layout/inline";
|
import { Inline } from "@bedrock-layout/inline";
|
||||||
import { PadBox } from "@bedrock-layout/padbox";
|
import { PadBox } from "@bedrock-layout/padbox";
|
||||||
@@ -13,31 +12,17 @@ import {
|
|||||||
|
|
||||||
import { Toggle } from "../../Common/Inputs/Toggle";
|
import { Toggle } from "../../Common/Inputs/Toggle";
|
||||||
import { MockupBlock } from "../../Common/MockupBlock.styled";
|
import { MockupBlock } from "../../Common/MockupBlock.styled";
|
||||||
|
import { SlotSelector } from "../../Common/Inputs/SlotSelector";
|
||||||
|
|
||||||
|
|
||||||
export function OverlayControl() {
|
export function OverlayControl() {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector);
|
const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* For prototyping use only */
|
|
||||||
const [idNum, setIdNum] = useState(0);
|
|
||||||
|
|
||||||
const nextIdNum = () => {
|
|
||||||
const nextIdNum = (idNum + 1) % overlayIds.length;
|
|
||||||
setIdNum(nextIdNum);
|
|
||||||
dispatch(setCurrentOverlayId(overlayIds[nextIdNum]));
|
|
||||||
};
|
|
||||||
/* For prototyping use only */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const onToggle = (value: boolean) => {
|
const onToggle = (value: boolean) => {
|
||||||
if (currentOverlayItem == null) {
|
if (currentOverlayItem == null) {
|
||||||
dispatch(initNewOverlayItem({
|
dispatch(initNewOverlayItem({
|
||||||
overlayId: overlayIds[idNum],
|
overlayId: overlayIds[0],
|
||||||
initialPosition: { x: 0, y: 0 },
|
initialPosition: { x: 0, y: 0 },
|
||||||
setVisible: true,
|
setVisible: true,
|
||||||
}));
|
}));
|
||||||
@@ -55,9 +40,12 @@ export function OverlayControl() {
|
|||||||
onToggle={onToggle}
|
onToggle={onToggle}
|
||||||
/>
|
/>
|
||||||
<PadBox padding="sm">
|
<PadBox padding="sm">
|
||||||
<MockupBlock color="green" height="1.5rem" width="100%">
|
<SlotSelector
|
||||||
Slot selector
|
items={overlayIds}
|
||||||
</MockupBlock>
|
onChange={(value) => dispatch(setCurrentOverlayId(value))}
|
||||||
|
removePrefix
|
||||||
|
formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")}
|
||||||
|
/>
|
||||||
</PadBox>
|
</PadBox>
|
||||||
</Inline>
|
</Inline>
|
||||||
<PadBox padding="sm">
|
<PadBox padding="sm">
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { findPrefixCount } from "./strings";
|
||||||
|
|
||||||
|
describe("strings", () => {
|
||||||
|
describe("findPrefixCount", () => {
|
||||||
|
describe("finds matches", () => {
|
||||||
|
it("gets correct count all the same", () => {
|
||||||
|
expect(findPrefixCount([
|
||||||
|
"hello",
|
||||||
|
"hello",
|
||||||
|
"hello",
|
||||||
|
"hello",
|
||||||
|
], true)).toBe(5);
|
||||||
|
});
|
||||||
|
it("gets correct count for hello prefix", () => {
|
||||||
|
expect(findPrefixCount([
|
||||||
|
"hello world",
|
||||||
|
"hello mother",
|
||||||
|
"hello father",
|
||||||
|
"hello!",
|
||||||
|
], true)).toBe(5);
|
||||||
|
});
|
||||||
|
it("gets zero when nothing matches", () => {
|
||||||
|
expect(findPrefixCount([
|
||||||
|
"cat",
|
||||||
|
"dog",
|
||||||
|
"rat",
|
||||||
|
], true)).toBe(0);
|
||||||
|
});
|
||||||
|
it("returns 0 when only given a single string", () => {
|
||||||
|
expect(findPrefixCount([
|
||||||
|
"by myself",
|
||||||
|
], true)).toBe(0);
|
||||||
|
});
|
||||||
|
it("returns 0 when given no strings", () => {
|
||||||
|
expect(findPrefixCount([], true)).toBe(0);
|
||||||
|
});
|
||||||
|
it("is case sensitive", () => {
|
||||||
|
expect(findPrefixCount([
|
||||||
|
"Hello",
|
||||||
|
"HELLO",
|
||||||
|
"Hello",
|
||||||
|
], true)).toBe(1);
|
||||||
|
});
|
||||||
|
it("is correctly case-insensitive", () => {
|
||||||
|
expect(findPrefixCount([
|
||||||
|
"Hello",
|
||||||
|
"HELLO",
|
||||||
|
"Hello",
|
||||||
|
], false)).toBe(5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Gives you the number of characters that prefix all the given strings.
|
||||||
|
* I use this to remove redundant repeated characters shared between file names.
|
||||||
|
* Basically, this is a method to do the " you find in a list.
|
||||||
|
* @param strings
|
||||||
|
* @returns number of characters that all strings share from the start
|
||||||
|
*/
|
||||||
|
export function findPrefixCount(strings: Array<string>, caseSensitive = true): number {
|
||||||
|
if (strings.length <= 1) {
|
||||||
|
return 0; // Cannot find anything. So, most neutral return is 0.
|
||||||
|
}
|
||||||
|
|
||||||
|
let maskLength = strings[0].length;
|
||||||
|
for (let x = 1; x < strings.length; ++x) {
|
||||||
|
for (let c = 0; c < maskLength; c++) {
|
||||||
|
const left = caseSensitive ? strings[0][c] : strings[0][c].toLowerCase();
|
||||||
|
const right = caseSensitive ? strings[x][c] : strings[x][c].toLowerCase();
|
||||||
|
if (left !== right) {
|
||||||
|
maskLength = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return maskLength;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user