-
Notifications
You must be signed in to change notification settings - Fork 857
fix: Simplify EuiFlex generic types #7792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| **Bug fixes** | ||
|
|
||
| - Fixed `EuiFlexGroup` and `EuiFlexItem` types to correctly accept global attribute props and simplify type resolution when used with `styled()`-like wrappers |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,12 @@ | |
|
|
||
| import React, { | ||
| ComponentPropsWithoutRef, | ||
| ComponentType, | ||
| ElementType, | ||
| ForwardedRef, | ||
| forwardRef, | ||
| FunctionComponent, | ||
| PropsWithChildren, | ||
| ReactElement, | ||
| Ref, | ||
| } from 'react'; | ||
| import classNames from 'classnames'; | ||
|
|
@@ -51,9 +52,7 @@ export const DIRECTIONS = [ | |
| ] as const; | ||
| type FlexGroupDirection = (typeof DIRECTIONS)[number]; | ||
|
|
||
| type ComponentPropType = ElementType<CommonProps>; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| export type EuiFlexGroupProps<TComponent extends ComponentPropType = 'div'> = | ||
| export type EuiFlexGroupProps<TComponent extends ElementType = 'div'> = | ||
| PropsWithChildren & | ||
| CommonProps & | ||
| ComponentPropsWithoutRef<TComponent> & { | ||
|
|
@@ -83,7 +82,7 @@ export type EuiFlexGroupProps<TComponent extends ComponentPropType = 'div'> = | |
| wrap?: boolean; | ||
| }; | ||
|
|
||
| const EuiFlexGroupInternal = <TComponent extends ComponentPropType>( | ||
| const EuiFlexGroupInternal = <TComponent extends ElementType>( | ||
| { | ||
| className, | ||
| component = 'div' as TComponent, | ||
|
|
@@ -96,7 +95,7 @@ const EuiFlexGroupInternal = <TComponent extends ComponentPropType>( | |
| ...rest | ||
| }: EuiFlexGroupProps<TComponent>, | ||
| ref: ForwardedRef<TComponent> | ||
| ) => { | ||
| ): ReactElement<EuiFlexGroupProps<TComponent>, TComponent> => { | ||
| const styles = useEuiMemoizedStyles(euiFlexGroupStyles); | ||
| const cssStyles = [ | ||
| styles.euiFlexGroup, | ||
|
|
@@ -110,23 +109,24 @@ const EuiFlexGroupInternal = <TComponent extends ComponentPropType>( | |
|
|
||
| const classes = classNames('euiFlexGroup', className); | ||
|
|
||
| // Cast the resolved component prop type to ComponentType to help TS | ||
| // process multiple infers and the overall type complexity. | ||
| // This might not be needed in TypeScript 5 | ||
| const Component = component as ComponentType<CommonProps & typeof rest>; | ||
| // Cast `component` to FunctionComponent to simplify its type. | ||
| // Note that FunctionComponent type is used here for purely typing | ||
| // convenience since we specify the return type above, and function | ||
| // components don't support `ref`s, but that doesn't matter in this case. | ||
| const Component = component as FunctionComponent<CommonProps & typeof rest>; | ||
|
|
||
| return <Component {...rest} ref={ref} className={classes} css={cssStyles} />; | ||
| }; | ||
|
|
||
| // Cast forwardRef return type to work with the generic TComponent type | ||
| // and not fallback to implicit any typing | ||
| export const EuiFlexGroup = forwardRef(EuiFlexGroupInternal) as (< | ||
| TComponent extends ComponentPropType = 'div', | ||
| TComponentRef = ReturnType<typeof EuiFlexGroupInternal> | ||
| TComponent extends ElementType = 'div', | ||
| TComponentRef = ReactElement<any, TComponent> | ||
| >( | ||
| props: EuiFlexGroupProps<TComponent> & { | ||
| ref?: Ref<TComponentRef>; | ||
| } | ||
| ) => ReturnType<typeof EuiFlexGroupInternal>) & { displayName?: string }; | ||
| ) => ReactElement) & { displayName?: string }; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loosens the return type but shouldn't have any impact on consumers of these two components. In case a stricter type is needed for |
||
|
|
||
| EuiFlexGroup.displayName = 'EuiFlexGroup'; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dts-generatorlikes to expand all types and for some reason this causes longer type checking time