149 lines
3.8 KiB
JavaScript
149 lines
3.8 KiB
JavaScript
import React from "react";
|
|
import {
|
|
Box,
|
|
Flex,
|
|
Icon,
|
|
Tab,
|
|
TabList,
|
|
Text,
|
|
Tooltip,
|
|
} from "@chakra-ui/react";
|
|
|
|
// Shared dimensions and surface styles for text and icon selectors.
|
|
const OUTER_RADIUS = { base: "10px", lg: "12px" };
|
|
const ITEM_RADIUS = { base: "7px", lg: "8px" };
|
|
const COMPACT_HEIGHT = { base: "36px", lg: "44px" };
|
|
|
|
/**
|
|
* items: [{ value, title, icon, tooltip?, isDisabled? }]
|
|
* mode: "icon" or "text"; tooltip falls back to title.
|
|
* selectionMode="tabs": Chakra Tabs controls the selected tab/keyboard behavior.
|
|
* selectionMode="options": each item is its own button; clicks select only that item.
|
|
* renderItem optionally wraps an item (e.g. with a guide target).
|
|
*/
|
|
export default function SegmentedSelector({
|
|
items = [],
|
|
mode = "text",
|
|
selectionMode = "options",
|
|
value,
|
|
onChange,
|
|
isDisabled = false,
|
|
compact = false,
|
|
renderItem,
|
|
...containerProps
|
|
}) {
|
|
const isTabs = selectionMode === "tabs";
|
|
|
|
const surfaceProps = {
|
|
p: 1,
|
|
gap: 1,
|
|
bg: "gray.100",
|
|
borderWidth: "1px",
|
|
borderColor: "gray.200",
|
|
borderRadius: OUTER_RADIUS,
|
|
boxShadow: "inner",
|
|
flexShrink: 0,
|
|
};
|
|
|
|
const children = items.map((item, index) => {
|
|
const title = item.title ?? item.label ?? "";
|
|
const tooltip = item.tooltip || title;
|
|
const selected = !isTabs && item.value === value;
|
|
const hasIcon = mode === "icon" && item.icon;
|
|
const optionDisabled = isDisabled || item.isDisabled;
|
|
|
|
const content = hasIcon ? (
|
|
<Icon as={item.icon} boxSize={{ base: 4, lg: 5 }} aria-hidden="true" />
|
|
) : (
|
|
<Text
|
|
as="span"
|
|
fontSize={{ base: "11px", lg: "12px" }}
|
|
fontWeight="800"
|
|
lineHeight="1"
|
|
whiteSpace="nowrap"
|
|
>
|
|
{title}
|
|
</Text>
|
|
);
|
|
|
|
const element = isTabs ? (
|
|
<Tab
|
|
aria-label={title}
|
|
isDisabled={item.isDisabled}
|
|
minW={mode === "icon" ? { base: "34px", lg: "auto" } : "auto"}
|
|
h={compact ? "full" : undefined}
|
|
px={{ base: 2, lg: 4 }}
|
|
py={compact ? 0 : { base: 1.5, lg: 2.5 }}
|
|
color="gray.600"
|
|
borderRadius={ITEM_RADIUS}
|
|
fontWeight="800"
|
|
transition="all 160ms ease"
|
|
_hover={{ bg: "white", color: "blue.600" }}
|
|
_selected={{ bg: "white", color: "blue.600", boxShadow: "sm" }}
|
|
_focusVisible={{ boxShadow: "outline" }}
|
|
>
|
|
{content}
|
|
</Tab>
|
|
) : (
|
|
<Flex
|
|
as="button"
|
|
type="button"
|
|
aria-label={title}
|
|
aria-pressed={selected}
|
|
disabled={optionDisabled}
|
|
onClick={() => {
|
|
if (!optionDisabled && !selected) onChange?.(item.value);
|
|
}}
|
|
cursor={optionDisabled ? "not-allowed" : "pointer"}
|
|
_focusVisible={{ boxShadow: "outline" }}
|
|
flexShrink={0}
|
|
h="full"
|
|
align="center"
|
|
justify="center"
|
|
px={{ base: 1.5, lg: 2 }}
|
|
bg={selected ? "white" : "transparent"}
|
|
color={selected ? "blue.600" : "gray.600"}
|
|
borderRadius={ITEM_RADIUS}
|
|
boxShadow={selected ? "sm" : "none"}
|
|
transition="background-color 160ms ease, color 160ms ease, box-shadow 160ms ease"
|
|
>
|
|
{content}
|
|
</Flex>
|
|
);
|
|
|
|
const wrappedItem = renderItem ? renderItem(element, item, index) : element;
|
|
|
|
return (
|
|
<Tooltip key={item.value ?? index} label={tooltip} hasArrow>
|
|
{wrappedItem}
|
|
</Tooltip>
|
|
);
|
|
});
|
|
|
|
if (isTabs) {
|
|
return (
|
|
<TabList
|
|
{...surfaceProps}
|
|
h={compact ? COMPACT_HEIGHT : undefined}
|
|
{...containerProps}
|
|
>
|
|
{children}
|
|
</TabList>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Box
|
|
role="group"
|
|
display="flex"
|
|
flexDirection="row"
|
|
w="fit-content"
|
|
h={COMPACT_HEIGHT}
|
|
{...surfaceProps}
|
|
{...containerProps}
|
|
>
|
|
{children}
|
|
</Box>
|
|
);
|
|
}
|