2022-02-10 19:00:20 +00:00
|
|
|
import React, { useState } from "react";
|
2021-03-15 12:17:56 +00:00
|
|
|
import styled from "styled-components";
|
2021-10-04 15:34:37 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2022-05-04 09:45:57 +00:00
|
|
|
import { ControlIcons } from "icons/ControlIcons";
|
|
|
|
|
import _ from "lodash";
|
2021-03-15 12:17:56 +00:00
|
|
|
|
|
|
|
|
const ItemWrapper = styled.div<{ selected: boolean }>`
|
2022-02-10 19:00:20 +00:00
|
|
|
min-width: 32px;
|
2021-03-15 12:17:56 +00:00
|
|
|
height: 32px;
|
2022-02-10 19:00:20 +00:00
|
|
|
padding: 0 2px;
|
2021-03-15 12:17:56 +00:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
2022-02-10 19:00:20 +00:00
|
|
|
border: 1px solid
|
|
|
|
|
${(props) => (props.selected ? Colors.GREY_10 : Colors.GREY_5)};
|
|
|
|
|
|
|
|
|
|
&.focused {
|
|
|
|
|
background: ${Colors.GREY_3};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 12:17:56 +00:00
|
|
|
cursor: pointer;
|
2021-09-06 07:06:15 +00:00
|
|
|
& {
|
2021-03-15 12:17:56 +00:00
|
|
|
margin-right: 4px;
|
|
|
|
|
}
|
2021-12-24 07:25:25 +00:00
|
|
|
& > div {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
&:hover {
|
2022-02-10 19:00:20 +00:00
|
|
|
background: ${Colors.GREY_3};
|
2021-12-24 07:25:25 +00:00
|
|
|
}
|
2021-03-15 12:17:56 +00:00
|
|
|
&&& svg {
|
|
|
|
|
path {
|
2022-02-10 19:00:20 +00:00
|
|
|
fill: ${Colors.GREY_7} !important;
|
2021-03-15 12:17:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const FlexWrapper = styled.div`
|
2021-10-04 15:34:37 +00:00
|
|
|
display: inline-flex;
|
2021-03-15 12:17:56 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export interface ButtonTabOption {
|
2022-05-04 09:45:57 +00:00
|
|
|
icon: string | JSX.Element;
|
2021-03-15 12:17:56 +00:00
|
|
|
value: string;
|
2022-02-10 19:00:20 +00:00
|
|
|
width?: number;
|
2021-03-15 12:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ButtonTabComponentProps {
|
|
|
|
|
options: ButtonTabOption[];
|
|
|
|
|
values: Array<string>;
|
|
|
|
|
selectButton: (value: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function ButtonTabComponent(props: ButtonTabComponentProps) {
|
2022-02-10 19:00:20 +00:00
|
|
|
const valueSet = new Set(props.values);
|
|
|
|
|
let firstValueIndex = 0;
|
|
|
|
|
for (const [i, x] of props.options.entries()) {
|
|
|
|
|
if (valueSet.has(x.value)) {
|
|
|
|
|
firstValueIndex = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [focusedIndex, setFocusedIndex] = useState<number>(-1);
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case "ArrowRight":
|
|
|
|
|
case "Right":
|
|
|
|
|
setFocusedIndex((prev) =>
|
|
|
|
|
prev === props.options.length - 1 ? 0 : prev + 1,
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
case "ArrowLeft":
|
|
|
|
|
case "Left":
|
|
|
|
|
setFocusedIndex((prev) =>
|
|
|
|
|
prev === 0 ? props.options.length - 1 : prev - 1,
|
2021-03-15 12:17:56 +00:00
|
|
|
);
|
2022-02-10 19:00:20 +00:00
|
|
|
break;
|
|
|
|
|
case "Enter":
|
|
|
|
|
case " ":
|
|
|
|
|
props.selectButton(props.options[focusedIndex].value);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FlexWrapper
|
|
|
|
|
onBlur={() => setFocusedIndex(-1)}
|
|
|
|
|
onFocus={() => setFocusedIndex(firstValueIndex)}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
role="tablist"
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
>
|
|
|
|
|
{props.options.map(
|
|
|
|
|
({ icon, value, width = 24 }: ButtonTabOption, index: number) => {
|
2022-05-04 09:45:57 +00:00
|
|
|
let ControlIcon;
|
|
|
|
|
if (_.isString(icon)) {
|
|
|
|
|
const Icon = ControlIcons[icon];
|
|
|
|
|
ControlIcon = <Icon height={24} width={width} />;
|
|
|
|
|
} else {
|
|
|
|
|
ControlIcon = icon;
|
|
|
|
|
}
|
2022-02-10 19:00:20 +00:00
|
|
|
const isSelected = valueSet.has(value);
|
|
|
|
|
return (
|
|
|
|
|
<ItemWrapper
|
|
|
|
|
aria-selected={isSelected}
|
|
|
|
|
className={`t--button-tab-${value} ${
|
|
|
|
|
index === focusedIndex ? "focused" : ""
|
|
|
|
|
}`}
|
|
|
|
|
key={index}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
props.selectButton(value);
|
|
|
|
|
setFocusedIndex(index);
|
|
|
|
|
}}
|
|
|
|
|
role="tab"
|
|
|
|
|
selected={isSelected}
|
|
|
|
|
>
|
2022-05-04 09:45:57 +00:00
|
|
|
{ControlIcon}
|
2022-02-10 19:00:20 +00:00
|
|
|
</ItemWrapper>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
)}
|
2021-03-15 12:17:56 +00:00
|
|
|
</FlexWrapper>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2021-03-15 12:17:56 +00:00
|
|
|
|
|
|
|
|
export default ButtonTabComponent;
|