diff --git a/packages/react-core/src/components/ToggleGroup/ToggleGroup.tsx b/packages/react-core/src/components/ToggleGroup/ToggleGroup.tsx index 7cce1326134..3258d0322a6 100644 --- a/packages/react-core/src/components/ToggleGroup/ToggleGroup.tsx +++ b/packages/react-core/src/components/ToggleGroup/ToggleGroup.tsx @@ -1,7 +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'; +import { ToggleGroupItem, ToggleGroupItemProps } from './ToggleGroupItem'; export interface ToggleGroupProps extends React.HTMLProps { /** Content rendered inside the toggle group */ @@ -24,12 +24,11 @@ export const ToggleGroup: React.FunctionComponent = ({ 'aria-label': ariaLabel, ...props }: ToggleGroupProps) => { - const toggleGroupItemList = React.Children.map(children, child => { - const childCompName = (child as any).type.name; - return childCompName !== ToggleGroupItem.name + const toggleGroupItemList = React.Children.map(children, child => + !(React.isValidElement(child) && child.type === ToggleGroupItem) ? child - : React.cloneElement(child as React.ReactElement, areAllGroupsDisabled ? { isDisabled: true } : {}); - }); + : React.cloneElement(child, areAllGroupsDisabled ? { isDisabled: true } : {}) + ); return (
{ test('item passes selection and event to onChange handler', async () => { const user = userEvent.setup(); - + render( ); @@ -60,4 +60,17 @@ describe('ToggleGroup', () => { ); expect(asFragment()).toMatchSnapshot(); }); + + test('should render non-ToggleGroupItem children', () => { + const { asFragment } = render( + + + {false && } + {'Test 3'} + {undefined} + {null} + + ); + expect(asFragment()).toMatchSnapshot(); + }); }); diff --git a/packages/react-core/src/components/ToggleGroup/__tests__/__snapshots__/ToggleGroup.test.tsx.snap b/packages/react-core/src/components/ToggleGroup/__tests__/__snapshots__/ToggleGroup.test.tsx.snap index 37ee48516c9..6ee2ca3ca73 100644 --- a/packages/react-core/src/components/ToggleGroup/__tests__/__snapshots__/ToggleGroup.test.tsx.snap +++ b/packages/react-core/src/components/ToggleGroup/__tests__/__snapshots__/ToggleGroup.test.tsx.snap @@ -129,3 +129,30 @@ exports[`ToggleGroup isDisabled 1`] = `
`; + +exports[`ToggleGroup should render non-ToggleGroupItem children 1`] = ` + +
+
+ +
+ Test 3 +
+
+`;