import type { ButtonProps as ChakraButtonProps, ButtonGroupProps as ChakraButtonGroupProps } from '@chakra-ui/react'; import { Button as ChakraButton, ButtonGroup as ChakraButtonGroup, } from '@chakra-ui/react'; import * as React from 'react'; import { Skeleton } from './skeleton'; interface ButtonLoadingProps { loading?: boolean; loadingText?: React.ReactNode; loadingSkeleton?: boolean; } export interface ButtonProps extends ChakraButtonProps, ButtonLoadingProps { expanded?: boolean; selected?: boolean; highlighted?: boolean; } export const Button = React.forwardRef( function Button(props, ref) { const { loading, disabled, children, expanded, selected, highlighted, loadingSkeleton = false, ...rest } = props; return ( }> { children } ); }, ); export interface ButtonGroupProps extends ChakraButtonGroupProps {} export const ButtonGroup = React.forwardRef( function ButtonGroup(props, ref) { const { ...rest } = props; return ( ); }, ); export interface ButtonGroupRadioProps extends Omit { children: Array>; onChange?: (value: string) => void; defaultValue?: string; loading?: boolean; equalWidth?: boolean; } export const ButtonGroupRadio = React.forwardRef( function ButtonGroupRadio(props, ref) { const { children, onChange, variant = 'segmented', defaultValue, loading = false, equalWidth = false, ...rest } = props; const firstChildValue = React.useMemo(() => { const firstChild = Array.isArray(children) ? children[0] : undefined; return typeof firstChild?.props.value === 'string' ? firstChild.props.value : undefined; }, [ children ]); const [ value, setValue ] = React.useState(defaultValue ?? firstChildValue); const handleItemClick = React.useCallback((event: React.MouseEvent) => { const value = event.currentTarget.value; setValue(value); onChange?.(value); }, [ onChange ]); const clonedChildren = React.Children.map(children, (child: React.ReactElement) => { return React.cloneElement(child, { onClick: handleItemClick, selected: value === child.props.value, variant, }); }); const childrenLength = React.Children.count(children); return ( { clonedChildren } ); }, );