| 1 |
import { Children, cloneElement, isValidElement } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/ToggleGroup/toggle-group'; |
| 4 |
import { ToggleGroupItem, ToggleGroupItemProps } from './ToggleGroupItem'; |
| 5 |
|
| 6 |
export interface ToggleGroupProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** Content rendered inside the toggle group */ |
| 8 |
children?: React.ReactNode; |
| 9 |
/** Additional classes added to the toggle group */ |
| 10 |
className?: string; |
| 11 |
/** Modifies the toggle group to include compact styling. */ |
| 12 |
isCompact?: boolean; |
| 13 |
/** Modifies the toggle group items to fill the available space. */ |
| 14 |
isFill?: boolean; |
| 15 |
/** Disable all toggle group items under this component. */ |
| 16 |
areAllGroupsDisabled?: boolean; |
| 17 |
/** Accessible label for the toggle group */ |
| 18 |
'aria-label'?: string; |
| 19 |
} |
| 20 |
|
| 21 |
export const ToggleGroup: React.FunctionComponent<ToggleGroupProps> = ({ |
| 22 |
className, |
| 23 |
children, |
| 24 |
isCompact = false, |
| 25 |
isFill = false, |
| 26 |
areAllGroupsDisabled = false, |
| 27 |
'aria-label': ariaLabel, |
| 28 |
...props |
| 29 |
}: ToggleGroupProps) => { |
| 30 |
const toggleGroupItemList = Children.map(children, (child) => |
| 31 |
!(isValidElement(child) && child.type === ToggleGroupItem) |
| 32 |
? child |
| 33 |
: cloneElement<ToggleGroupItemProps>(child, areAllGroupsDisabled ? { isDisabled: true } : {}) |
| 34 |
); |
| 35 |
|
| 36 |
return ( |
| 37 |
<div |
| 38 |
className={css( |
| 39 |
styles.toggleGroup, |
| 40 |
isCompact && styles.modifiers.compact, |
| 41 |
isFill && styles.modifiers.fill, |
| 42 |
className |
| 43 |
)} |
| 44 |
role="group" |
| 45 |
aria-label={ariaLabel} |
| 46 |
{...props} |
| 47 |
> |
| 48 |
{toggleGroupItemList} |
| 49 |
</div> |
| 50 |
); |
| 51 |
}; |
| 52 |
ToggleGroup.displayName = 'ToggleGroup'; |