UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

36 lines (35 loc) 1.34 kB
import { jsx as _jsx } from "react/jsx-runtime"; import * as React from 'react'; import { createComponent } from '@workday/canvas-kit-react/common'; import { SegmentedControlButton } from './SegmentedControlButton'; const onButtonClick = (existingOnClick, onChange, index, event) => { if (existingOnClick) { existingOnClick(event); } const target = event.currentTarget; if (target && onChange) { if (target.value) { onChange(target.value); } else { onChange(index); } } }; export const SegmentedControl = createComponent('div')({ displayName: 'SegmentedControl', Component: ({ value = 0, children, onChange, ...elemProps }, ref) => { return (_jsx("div", { ...elemProps, children: React.Children.map(children, (child, index) => { if (typeof child.type === typeof SegmentedControlButton) { return React.cloneElement(child, { toggled: typeof value === 'number' ? index === value : child.props.value === value, onClick: onButtonClick.bind(undefined, child.props.onClick, onChange, index), }); } return child; }) })); }, subComponents: { Button: SegmentedControlButton, }, });