UNPKG

@nafr/echo-ui

Version:

A UI library born for WAA

32 lines (31 loc) 1.22 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { forwardRef } from 'react'; import { ButtonGroupContextProvider } from './context'; import { cn } from '../../../lib/utils'; import { RADIUS, SIZE } from './constants'; import { useGroupStyle } from './styles'; export const ButtonGroup = forwardRef((props, ref) => { const { value = [], disabled = false, size = SIZE, radius = RADIUS, classNames, styles, className, style, onChange, ...restProps } = props; const handleGroupChange = (v) => { const newValue = v; let updatedValue; if (Array.isArray(value)) { updatedValue = value.includes(newValue) ? value.filter((val) => val !== newValue) : [...value, newValue]; } else updatedValue = newValue; onChange?.(updatedValue); }; const contextValue = { value, disabled, size, radius, classNames, styles, onChange: handleGroupChange, }; return (_jsx(ButtonGroupContextProvider, { value: contextValue, children: _jsx("div", { ref: ref, className: cn(useGroupStyle(), className), style: style, children: restProps.children }) })); });