Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/react-core/src/components/ToggleGroup/ToggleGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/ToggleGroup/toggle-group';
import { ToggleGroupItem } from './ToggleGroupItem';

export interface ToggleGroupProps extends React.HTMLProps<HTMLDivElement> {
/** Content rendered inside the toggle group */
Expand All @@ -9,6 +10,8 @@ export interface ToggleGroupProps extends React.HTMLProps<HTMLDivElement> {
className?: string;
/** Modifies the toggle group to include compact styling. */
isCompact?: boolean;
/** Disable all toggle group items under this component. */
areAllGroupsDisabled?: boolean;
/** Accessible label for the toggle group */
'aria-label'?: string;
}
Expand All @@ -17,12 +20,15 @@ export const ToggleGroup: React.FunctionComponent<ToggleGroupProps> = ({
className,
children,
isCompact = false,
areAllGroupsDisabled = false,
'aria-label': ariaLabel,
...props
}: ToggleGroupProps) => {
const toggleGroupItemList = [] as any[];
React.Children.forEach(children, child => {
toggleGroupItemList.push(child);
const toggleGroupItemList = React.Children.map(children, child => {
const childCompName = (child as any).type.name;
return childCompName !== ToggleGroupItem.name
? child
: React.cloneElement(child as React.ReactElement, areAllGroupsDisabled ? { isDisabled: true } : {});
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ exports[`isCompact 1`] = `
role="group"
>
<ToggleGroupItem
key=".0"
text="Test"
/>
<ToggleGroupItem
key=".1"
text="Test"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ propComponents: ['ToggleGroup', 'ToggleGroupItem']
---
import './toggleGroup.css';

import { ToggleGroup, ToggleGroupItem} from '@patternfly/react-core';
import { ToggleGroup, ToggleGroupItem, Button, Stack, StackItem } from '@patternfly/react-core';
import UndoIcon from '@patternfly/react-icons/dist/esm/icons/undo-icon';
import CopyIcon from '@patternfly/react-icons/dist/esm/icons/copy-icon';
import ShareSquareIcon from '@patternfly/react-icons/dist/esm/icons/share-square-icon';
Expand All @@ -16,15 +16,16 @@ import ShareSquareIcon from '@patternfly/react-icons/dist/esm/icons/share-square
### Default with multiple selectable
```js
import React from 'react';
import { ToggleGroup, ToggleGroupItem } from '@patternfly/react-core';
import { ToggleGroup, ToggleGroupItem, Button, Stack, StackItem } from '@patternfly/react-core';

class DefaultToggleGroupExample extends React.Component {
constructor(props) {
super(props);
this.state = {
isSelected: {
first: false,
second: false
second: false,
disableAll: false
}
};
this.handleItemClick = (isSelected, event) => {
Expand All @@ -36,17 +37,29 @@ class DefaultToggleGroupExample extends React.Component {
};
});
};
this.disableAllClick = () => {
this.setState(prevState => ({ disableAll: !prevState.disableAll }));
};
}

render() {
const { isSelected } = this.state;

return (
<ToggleGroup aria-label="Default with multiple selectable">
<ToggleGroupItem text="Option 1" key={0} buttonId="first" isSelected={isSelected.first} onChange={this.handleItemClick} />
<ToggleGroupItem text="Option 2" key={1} buttonId="second" isSelected={isSelected.second} onChange={this.handleItemClick} />
<ToggleGroupItem text="Option 3" key={2} isDisabled/>
</ToggleGroup>
<Stack hasGutter>
<StackItem>
<Button onClick={this.disableAllClick}>
{this.state.disableAll ? "Enable back" : "Disable all"}
</Button>
</StackItem>
<StackItem>
<ToggleGroup areAllGroupsDisabled={this.state.disableAll} aria-label="Default with multiple selectable">
<ToggleGroupItem text="Option 1" key={0} buttonId="first" isSelected={isSelected.first} onChange={this.handleItemClick} />
<ToggleGroupItem text="Option 2" key={1} buttonId="second" isSelected={isSelected.second} onChange={this.handleItemClick} />
<ToggleGroupItem text="Option 3" key={2} isDisabled/>
</ToggleGroup>
</StackItem>
</Stack>
);
}
}
Expand Down