From 023249a944c816f842d50b6e5c011ed26510e049 Mon Sep 17 00:00:00 2001 From: jainex Date: Wed, 2 Aug 2023 06:06:55 +0530 Subject: [PATCH] migration of HeaderBase to use TS Box version (#20250) * migration of HeaderBase to use TS Box version * migration of HeaderBase to use TS Box version * update snapshot * fix lint error * Some small updates to types and removing deprecated storyook functions --------- Co-authored-by: georgewrmarshall --- .../__snapshots__/header-base.test.tsx.snap | 4 +- .../header-base/header-base.stories.tsx | 10 +- .../header-base/header-base.tsx | 210 +++++++++--------- .../header-base/header-base.types.ts | 22 +- .../component-library/header-base/index.ts | 6 +- .../__snapshots__/modal-header.test.tsx.snap | 4 +- .../modal-header/modal-header.types.ts | 4 +- .../popover-header.test.tsx.snap | 4 +- .../popover-header/popover-header.types.ts | 4 +- 9 files changed, 145 insertions(+), 123 deletions(-) diff --git a/ui/components/component-library/header-base/__snapshots__/header-base.test.tsx.snap b/ui/components/component-library/header-base/__snapshots__/header-base.test.tsx.snap index c6217297f..a67e3648e 100644 --- a/ui/components/component-library/header-base/__snapshots__/header-base.test.tsx.snap +++ b/ui/components/component-library/header-base/__snapshots__/header-base.test.tsx.snap @@ -3,12 +3,12 @@ exports[`HeaderBase should render HeaderBase element correctly 1`] = `
should render HeaderBase element correctly
diff --git a/ui/components/component-library/header-base/header-base.stories.tsx b/ui/components/component-library/header-base/header-base.stories.tsx index 6079b9f69..abe880dcf 100644 --- a/ui/components/component-library/header-base/header-base.stories.tsx +++ b/ui/components/component-library/header-base/header-base.stories.tsx @@ -1,6 +1,5 @@ import React from 'react'; -import { ComponentStory, ComponentMeta } from '@storybook/react'; -import Box from '../../ui/box'; +import { StoryFn, Meta } from '@storybook/react'; import { IconName, Button, @@ -8,6 +7,7 @@ import { ButtonIcon, ButtonIconSize, Text, + Box, } from '..'; import { @@ -27,11 +27,9 @@ export default { page: README, }, }, -} as ComponentMeta; +} as Meta; -const Template: ComponentStory = (args) => ( - -); +const Template: StoryFn = (args) => ; export const DefaultStory = Template.bind({}); diff --git a/ui/components/component-library/header-base/header-base.tsx b/ui/components/component-library/header-base/header-base.tsx index f10315e3f..371340762 100644 --- a/ui/components/component-library/header-base/header-base.tsx +++ b/ui/components/component-library/header-base/header-base.tsx @@ -1,114 +1,122 @@ import React, { useRef, useEffect, useMemo, useState } from 'react'; import classnames from 'classnames'; import { - BLOCK_SIZES, - DISPLAY, + BlockSize, + Display, JustifyContent, } from '../../../helpers/constants/design-system'; -import Box from '../../ui/box'; +import { Box } from '..'; -import { HeaderBaseProps } from './header-base.types'; +import type { PolymorphicRef, BoxProps } from '../box'; -export const HeaderBase: React.FC = ({ - startAccessory, - endAccessory, - className = '', - children, - childrenWrapperProps, - startAccessoryWrapperProps, - endAccessoryWrapperProps, - ...props -}) => { - const startAccessoryRef = useRef(null); - const endAccessoryRef = useRef(null); - const [accessoryMinWidth, setAccessoryMinWidth] = useState(); +import { HeaderBaseProps, HeaderBaseComponent } from './header-base.types'; - useEffect(() => { - function handleResize() { - if (startAccessoryRef.current && endAccessoryRef.current) { - const accMinWidth = Math.max( - startAccessoryRef.current.scrollWidth, - endAccessoryRef.current.scrollWidth, - ); - setAccessoryMinWidth(accMinWidth); - } else if (startAccessoryRef.current && !endAccessoryRef.current) { - setAccessoryMinWidth(startAccessoryRef.current.scrollWidth); - } else if (!startAccessoryRef.current && endAccessoryRef.current) { - setAccessoryMinWidth(endAccessoryRef.current.scrollWidth); - } else { - setAccessoryMinWidth(0); +export const HeaderBase: HeaderBaseComponent = React.forwardRef( + ( + { + startAccessory, + endAccessory, + className = '', + children, + childrenWrapperProps, + startAccessoryWrapperProps, + endAccessoryWrapperProps, + ...props + }: HeaderBaseProps, + ref?: PolymorphicRef, + ) => { + const startAccessoryRef = useRef(null); + const endAccessoryRef = useRef(null); + const [accessoryMinWidth, setAccessoryMinWidth] = useState(); + + useEffect(() => { + function handleResize() { + if (startAccessoryRef.current && endAccessoryRef.current) { + const accMinWidth = Math.max( + startAccessoryRef.current.scrollWidth, + endAccessoryRef.current.scrollWidth, + ); + setAccessoryMinWidth(accMinWidth); + } else if (startAccessoryRef.current && !endAccessoryRef.current) { + setAccessoryMinWidth(startAccessoryRef.current.scrollWidth); + } else if (!startAccessoryRef.current && endAccessoryRef.current) { + setAccessoryMinWidth(endAccessoryRef.current.scrollWidth); + } else { + setAccessoryMinWidth(0); + } } - } - handleResize(); - window.addEventListener('resize', handleResize); + handleResize(); + window.addEventListener('resize', handleResize); - return () => { - window.removeEventListener('resize', handleResize); - }; - }, [startAccessoryRef, endAccessoryRef, children]); - - const getTitleStyles = useMemo(() => { - if (startAccessory && !endAccessory) { - return { - marginRight: `${accessoryMinWidth}px`, + return () => { + window.removeEventListener('resize', handleResize); }; - } else if (!startAccessory && endAccessory) { - return { - marginLeft: `${accessoryMinWidth}px`, - }; - } - return {}; - }, [accessoryMinWidth, startAccessory, endAccessory]); + }, [startAccessoryRef, endAccessoryRef, children]); - return ( - - {startAccessory && ( - - {startAccessory} - - )} - {children && ( - - {children} - - )} - {endAccessory && ( - - {endAccessory} - - )} - - ); -}; + const getTitleStyles = useMemo(() => { + if (startAccessory && !endAccessory) { + return { + marginRight: `${accessoryMinWidth}px`, + }; + } else if (!startAccessory && endAccessory) { + return { + marginLeft: `${accessoryMinWidth}px`, + }; + } + return {}; + }, [accessoryMinWidth, startAccessory, endAccessory]); + + return ( + )} + > + {startAccessory && ( + )} + > + {startAccessory} + + )} + {children && ( + )} + > + {children} + + )} + {endAccessory && ( + )} + > + {endAccessory} + + )} + + ); + }, +); diff --git a/ui/components/component-library/header-base/header-base.types.ts b/ui/components/component-library/header-base/header-base.types.ts index a871ce744..8c5938efb 100644 --- a/ui/components/component-library/header-base/header-base.types.ts +++ b/ui/components/component-library/header-base/header-base.types.ts @@ -1,7 +1,12 @@ import React from 'react'; -import type { BoxProps } from '../../ui/box/box.d'; -export interface HeaderBaseProps extends BoxProps { +import type { + StyleUtilityProps, + PolymorphicComponentPropWithRef, + BoxProps, +} from '../box'; + +export interface HeaderBaseStyleUtilityProps extends StyleUtilityProps { /** * The children is the title area of the HeaderBase */ @@ -9,7 +14,7 @@ export interface HeaderBaseProps extends BoxProps { /** * Use the `childrenWrapperProps` prop to define the props to the children wrapper */ - childrenWrapperProps?: BoxProps; + childrenWrapperProps?: BoxProps<'div'>; /** * The start(default left) content area of HeaderBase */ @@ -17,7 +22,7 @@ export interface HeaderBaseProps extends BoxProps { /** * Use the `startAccessoryWrapperProps` prop to define the props to the start accessory wrapper */ - startAccessoryWrapperProps?: BoxProps; + startAccessoryWrapperProps?: BoxProps<'div'>; /** * The end (default right) content area of HeaderBase */ @@ -25,9 +30,16 @@ export interface HeaderBaseProps extends BoxProps { /** * Use the `endAccessoryWrapperProps` prop to define the props to the end accessory wrapper */ - endAccessoryWrapperProps?: BoxProps; + endAccessoryWrapperProps?: BoxProps<'div'>; /** * An additional className to apply to the HeaderBase */ className?: string; } + +export type HeaderBaseProps = + PolymorphicComponentPropWithRef; + +export type HeaderBaseComponent = ( + props: HeaderBaseProps, +) => React.ReactElement | null; diff --git a/ui/components/component-library/header-base/index.ts b/ui/components/component-library/header-base/index.ts index 1b47557f8..a05425161 100644 --- a/ui/components/component-library/header-base/index.ts +++ b/ui/components/component-library/header-base/index.ts @@ -1,2 +1,6 @@ export { HeaderBase } from './header-base'; -export type { HeaderBaseProps } from './header-base.types'; +export type { + HeaderBaseProps, + HeaderBaseComponent, + HeaderBaseStyleUtilityProps, +} from './header-base.types'; diff --git a/ui/components/component-library/modal-header/__snapshots__/modal-header.test.tsx.snap b/ui/components/component-library/modal-header/__snapshots__/modal-header.test.tsx.snap index 67298b339..c927dbe36 100644 --- a/ui/components/component-library/modal-header/__snapshots__/modal-header.test.tsx.snap +++ b/ui/components/component-library/modal-header/__snapshots__/modal-header.test.tsx.snap @@ -3,11 +3,11 @@ exports[`ModalHeader should render ModalHeader correctly 1`] = `