Skip to content
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

hotfix: correct EuiFlexGroup and EuiFlexItem ref prop type and other small adjustments #7724

Merged
merged 6 commits into from
May 7, 2024
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
3 changes: 3 additions & 0 deletions changelogs/upcoming/7724.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed `EuiFlexGroup` and `EuiFlexItem` `ref` prop typing to support refs of the same type as the passed `component` type and allow `displayName` to be defined for easy component naming when using component wrappers like `styled()`
68 changes: 35 additions & 33 deletions src/components/flex/flex_group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import React, {
ElementType,
ForwardedRef,
forwardRef,
FunctionComponent,
PropsWithChildren,
Ref,
} from 'react';
import classNames from 'classnames';
Expand Down Expand Up @@ -54,32 +54,34 @@ type FlexGroupDirection = (typeof DIRECTIONS)[number];
type ComponentPropType = ElementType<CommonProps>;

export type EuiFlexGroupProps<TComponent extends ComponentPropType = 'div'> =
ComponentPropsWithoutRef<TComponent> & {
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()`).
*
* `<EuiFlexGroup>` accepts and forwards all extra props to the custom
* component.
*
* @example
* // Renders a <button> element
* <EuiFlexGroup component="button">
* Submit form
* </EuiFlexGroup>
* @default "div"
*/
component?: TComponent;
direction?: FlexGroupDirection;
gutterSize?: EuiFlexGroupGutterSize;
justifyContent?: FlexGroupJustifyContent;
responsive?: boolean;
wrap?: boolean;
};
PropsWithChildren &
CommonProps &
ComponentPropsWithoutRef<TComponent> & {
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()`).
*
* `<EuiFlexGroup>` accepts and forwards all extra props to the custom
* component.
*
* @example
* // Renders a <button> element
* <EuiFlexGroup component="button">
* Submit form
* </EuiFlexGroup>
* @default "div"
*/
component?: TComponent;
direction?: FlexGroupDirection;
gutterSize?: EuiFlexGroupGutterSize;
justifyContent?: FlexGroupJustifyContent;
responsive?: boolean;
wrap?: boolean;
};

const EuiFlexGroupInternal = <TComponent extends ComponentPropType>(
{
Expand Down Expand Up @@ -118,13 +120,13 @@ const EuiFlexGroupInternal = <TComponent extends ComponentPropType>(

// 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
export const EuiFlexGroup = forwardRef(EuiFlexGroupInternal) as (<
TComponent extends ComponentPropType = 'div',
TComponentRef = ReturnType<typeof EuiFlexGroupInternal>
>(
props: EuiFlexGroupProps<TComponent> & {
ref?: Ref<typeof EuiFlexGroupInternal>;
ref?: Ref<TComponentRef>;
}
) => ReturnType<typeof EuiFlexGroupInternal>;
) => ReturnType<typeof EuiFlexGroupInternal>) & { displayName?: string };

// Cast is required here because of the cast above
(EuiFlexGroup as FunctionComponent).displayName = 'EuiFlexGroup';
EuiFlexGroup.displayName = 'EuiFlexGroup';
13 changes: 6 additions & 7 deletions src/components/flex/flex_item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import React, {
ComponentType,
ForwardedRef,
forwardRef,
FunctionComponent,
Ref,
} from 'react';
import classNames from 'classnames';
Expand Down Expand Up @@ -110,13 +109,13 @@ const EuiFlexItemInternal = <TComponent extends ComponentPropType>(

// Cast forwardRef return type to work with the generic TComponent type
// and not fallback to implicit any typing
export const EuiFlexItem = forwardRef(EuiFlexItemInternal) as <
TComponent extends ComponentPropType
export const EuiFlexItem = forwardRef(EuiFlexItemInternal) as (<
TComponent extends ComponentPropType,
TComponentRef = ReturnType<typeof EuiFlexItemInternal>
>(
props: EuiFlexItemProps<TComponent> & {
ref?: Ref<typeof EuiFlexItemInternal>;
ref?: Ref<TComponentRef>;
}
) => ReturnType<typeof EuiFlexItemInternal>;
) => ReturnType<typeof EuiFlexItemInternal>) & { displayName?: string };

// Cast is required here because of the cast above
(EuiFlexItem as FunctionComponent).displayName = 'EuiFlexItem';
EuiFlexItem.displayName = 'EuiFlexItem';
Loading