UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

39 lines (38 loc) 1.6 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); } } }; /** * @deprecated ⚠️ `SegmentedControl` in Main has been deprecated and will be removed in a future major version. Please use [`SegmentedControl` in Preview](https://workday.github.io/canvas-kit/?path=/docs/preview-segmented-control--docs) instead. */ 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, }, });