diff --git a/src-docs/src/views/flex/flex_example.js b/src-docs/src/views/flex/flex_example.js index f395d74bd18..9fd7a0e7c83 100644 --- a/src-docs/src/views/flex/flex_example.js +++ b/src-docs/src/views/flex/flex_example.js @@ -246,7 +246,7 @@ export const FlexExample = { ), }, { - title: 'Override output component type', + title: 'Spans instead of divs', source: [ { type: GuideSectionTypes.JS, @@ -255,12 +255,9 @@ export const FlexExample = { ], text: (

- Pass the component property to{' '} + Specify component=“span” on{' '} EuiFlexGroup and/or EuiFlexItem to - change the rendered component type from the default{' '} - div. It can be any valid React component type like - a tag name string such as div - or span or a React component. + change from the default div.

), snippet: componentSpanSnippet, diff --git a/src/components/flex/flex_group.test.tsx b/src/components/flex/flex_group.test.tsx index fdb16b171d9..f9a252ca418 100644 --- a/src/components/flex/flex_group.test.tsx +++ b/src/components/flex/flex_group.test.tsx @@ -6,9 +6,12 @@ * Side Public License, v 1. */ -import React, { JSX } from 'react'; +import React from 'react'; import { requiredProps } from '../../test'; -import { shouldRenderCustomStyles } from '../../test/internal'; +import { + shouldRenderCustomStyles, + testOnReactVersion, +} from '../../test/internal'; import { render } from '../../test/rtl'; import { @@ -17,6 +20,7 @@ import { ALIGN_ITEMS, JUSTIFY_CONTENTS, DIRECTIONS, + COMPONENT_TYPES, } from './flex_group'; describe('EuiFlexGroup', () => { @@ -94,21 +98,25 @@ describe('EuiFlexGroup', () => { }); describe('component', () => { - ['div', 'span'].forEach((value) => { + COMPONENT_TYPES.forEach((value) => { test(`${value} is rendered`, () => { - const { container } = render( - - ); + const { container } = render(); expect(container.firstChild!.nodeName).toEqual(value.toUpperCase()); }); }); - test('custom component is rendered', () => { - const component = () => Custom component test; - const { getByText } = render(); - expect(getByText('Custom component test')).toBeInTheDocument(); - }); + // React 18 throws a false error on test unmount for components w/ ref callbacks + // that throw in a `useEffect`. Note: This only affects the test env, not prod + // @see https://github.com/facebook/react/issues/25675#issuecomment-1363957941 + // TODO: Remove `testOnReactVersion` once the above bug is fixed + testOnReactVersion(['16', '17'])( + `invalid component types throw an error`, + () => { + // @ts-expect-error intentionally passing an invalid value + expect(() => render()).toThrow(); + } + ); }); }); }); diff --git a/src/components/flex/flex_group.tsx b/src/components/flex/flex_group.tsx index 41311af37a5..f91effc987a 100644 --- a/src/components/flex/flex_group.tsx +++ b/src/components/flex/flex_group.tsx @@ -6,15 +6,7 @@ * Side Public License, v 1. */ -import React, { - ComponentPropsWithoutRef, - ComponentType, - ElementType, - ForwardedRef, - forwardRef, - FunctionComponent, - Ref, -} from 'react'; +import React, { HTMLAttributes, Ref, forwardRef, useEffect } from 'react'; import classNames from 'classnames'; import { CommonProps } from '../common'; @@ -51,80 +43,80 @@ export const DIRECTIONS = [ ] as const; type FlexGroupDirection = (typeof DIRECTIONS)[number]; -type ComponentPropType = ElementType; +export const COMPONENT_TYPES = ['div', 'span'] as const; +type FlexGroupComponentType = (typeof COMPONENT_TYPES)[number]; -export type EuiFlexGroupProps = - ComponentPropsWithoutRef & { - alignItems?: FlexGroupAlignItems; - /** - * Customize the component type that is rendered. - * - * It can be any valid React component type like a tag name string - * such as `'div'` or `'span'`, a React component (a function, a class, - * or an exotic component like `memo()`). - * - * `` accepts and forwards all extra props to the custom - * component. - * - * @example - * // Renders a