Files
Termix/src/ui/components/button-group.tsx
T

64 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-05-11 01:23:11 -05:00
import {
Children,
type ReactElement,
cloneElement,
isValidElement,
} from "react";
2026-05-11 01:23:11 -05:00
import { type ButtonProps } from "@/components/button";
2025-09-12 14:42:00 -05:00
import { cn } from "@/lib/utils";
interface ButtonGroupProps {
className?: string;
2025-09-12 14:42:00 -05:00
orientation?: "horizontal" | "vertical";
children: ReactElement<ButtonProps>[] | React.ReactNode;
}
export const ButtonGroup = ({
className,
2025-09-12 14:42:00 -05:00
orientation = "horizontal",
children,
}: ButtonGroupProps) => {
2025-09-12 14:42:00 -05:00
const isHorizontal = orientation === "horizontal";
const isVertical = orientation === "vertical";
// Normalize and filter only valid React elements
2025-09-12 14:42:00 -05:00
const childArray = Children.toArray(children).filter(
(child): child is ReactElement<ButtonProps> => isValidElement(child),
);
const totalButtons = childArray.length;
return (
<div
className={cn(
2025-09-12 14:42:00 -05:00
"flex",
{
2025-09-12 14:42:00 -05:00
"flex-col": isVertical,
"w-fit": isVertical,
},
2025-09-12 14:42:00 -05:00
className,
)}
>
{childArray.map((child, index) => {
const isFirst = index === 0;
const isLast = index === totalButtons - 1;
return cloneElement(child, {
className: cn(
{
2025-09-12 14:42:00 -05:00
"rounded-l-none": isHorizontal && !isFirst,
"rounded-r-none": isHorizontal && !isLast,
"border-l-0": isHorizontal && !isFirst,
2025-09-12 14:42:00 -05:00
"rounded-t-none": isVertical && !isFirst,
"rounded-b-none": isVertical && !isLast,
"border-t-0": isVertical && !isFirst,
},
2025-09-12 14:42:00 -05:00
child.props.className,
),
});
})}
</div>
);
2025-09-12 14:42:00 -05:00
};