2024-02-08 12:49:46 +04:00
|
|
|
import React, { cloneElement, forwardRef, ReactElement } from 'react';
|
2024-02-02 02:52:36 +04:00
|
|
|
import { SegmentedButtonProps } from './segmented-buttons.types';
|
|
|
|
|
|
|
|
|
|
export const SegmentedButtons = forwardRef<
|
|
|
|
|
HTMLDivElement,
|
|
|
|
|
SegmentedButtonProps
|
|
|
|
|
>(({ children, ...props }, ref) => {
|
|
|
|
|
if (children.length <= 1) {
|
2024-02-02 14:39:02 +04:00
|
|
|
throw 'You must build segmented button with 2 or more buttton';
|
2024-02-02 02:52:36 +04:00
|
|
|
}
|
|
|
|
|
|
2024-02-08 12:49:46 +04:00
|
|
|
const buttons = children.map((button: ReactElement, index) => {
|
|
|
|
|
const classes =
|
|
|
|
|
`m3-button-segment ${button.props.className ?? ''}`.trimEnd();
|
|
|
|
|
return cloneElement(button, {
|
|
|
|
|
className: classes,
|
|
|
|
|
key: index,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-02 02:52:36 +04:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={`m3 m3-segmented-buttons ${props.className ?? ''}`.trimEnd()}
|
|
|
|
|
ref={ref}
|
|
|
|
|
>
|
2024-02-08 12:49:46 +04:00
|
|
|
{buttons}
|
2024-02-02 02:52:36 +04:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-02-08 12:49:46 +04:00
|
|
|
});
|