@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
35 lines (34 loc) • 1.26 kB
JavaScript
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 (React.createElement("div", { ...elemProps }, 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,
},
});