From 9b55791af7234ae9741034d1b0da02ed1bb79437 Mon Sep 17 00:00:00 2001 From: Dhruv <79097544+dhruvv173@users.noreply.github.com> Date: Wed, 2 Aug 2023 02:49:32 +0530 Subject: [PATCH] fix/migrate AvatarAccount to TS (#20208) * migrating AvatarAccount to TS * updates * reverting changes to diameter * suggested changes * adding explicit types for AvatarAccountDiameter * updating documentation * Some small updates to types --------- Co-authored-by: georgewrmarshall --- .../avatar-account/README.mdx | 7 +- ...t.js.snap => avatar-account.test.tsx.snap} | 0 .../avatar-account/avatar-account.js | 76 ------------------- ....stories.js => avatar-account.stories.tsx} | 23 +++--- ...ccount.test.js => avatar-account.test.tsx} | 0 .../avatar-account/avatar-account.tsx | 49 ++++++++++++ .../avatar-account/avatar-account.types.ts | 50 ++++++++++-- .../avatar-account/{index.js => index.ts} | 1 + 8 files changed, 104 insertions(+), 102 deletions(-) rename ui/components/component-library/avatar-account/__snapshots__/{avatar-account.test.js.snap => avatar-account.test.tsx.snap} (100%) delete mode 100644 ui/components/component-library/avatar-account/avatar-account.js rename ui/components/component-library/avatar-account/{avatar-account.stories.js => avatar-account.stories.tsx} (72%) rename ui/components/component-library/avatar-account/{avatar-account.test.js => avatar-account.test.tsx} (100%) create mode 100644 ui/components/component-library/avatar-account/avatar-account.tsx rename ui/components/component-library/avatar-account/{index.js => index.ts} (71%) diff --git a/ui/components/component-library/avatar-account/README.mdx b/ui/components/component-library/avatar-account/README.mdx index 5207f6421..9150c7d54 100644 --- a/ui/components/component-library/avatar-account/README.mdx +++ b/ui/components/component-library/avatar-account/README.mdx @@ -13,15 +13,10 @@ The `AvatarAccount` is a type of avatar reserved for representing accounts. ## Props -The `AvatarAccount` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props +The `AvatarAccount` accepts all props below as well as all [Box](/docs/components-componentlibrary-box--docs#props) component props -`AvatarAccount` accepts all [AvatarBase](/docs/components-componentlibrary-avatarbase--default-story)) -component props - - - ### Size Use the `size` prop and the `AvatarAccountSize` enum from `../../component-library` to change the size of `AvatarAccount` diff --git a/ui/components/component-library/avatar-account/__snapshots__/avatar-account.test.js.snap b/ui/components/component-library/avatar-account/__snapshots__/avatar-account.test.tsx.snap similarity index 100% rename from ui/components/component-library/avatar-account/__snapshots__/avatar-account.test.js.snap rename to ui/components/component-library/avatar-account/__snapshots__/avatar-account.test.tsx.snap diff --git a/ui/components/component-library/avatar-account/avatar-account.js b/ui/components/component-library/avatar-account/avatar-account.js deleted file mode 100644 index 286024253..000000000 --- a/ui/components/component-library/avatar-account/avatar-account.js +++ /dev/null @@ -1,76 +0,0 @@ -import React from 'react'; -import classnames from 'classnames'; -import PropTypes from 'prop-types'; -import Jazzicon from '../../ui/jazzicon/jazzicon.component'; -import BlockieIdenticon from '../../ui/identicon/blockieIdenticon/blockieIdenticon.component'; - -import Box from '../../ui/box/box'; - -import { AvatarBase } from '../avatar-base'; -import { - AvatarAccountDiameter, - AvatarAccountVariant, - AvatarAccountSize, -} from './avatar-account.types'; - -export const AvatarAccount = React.forwardRef( - ( - { - size = AvatarAccountSize.Md, - address, - className, - variant = AvatarAccountVariant.Jazzicon, - ...props - }, - ref, - ) => ( - - {variant === AvatarAccountVariant.Jazzicon ? ( - - ) : ( - - )} - - ), -); - -AvatarAccount.propTypes = { - /** - * The size of the AvatarAccount. - * Possible values could be 'AvatarAccountSize.Xs', 'AvatarAccountSize.Sm', 'AvatarAccountSize.Md', 'AvatarAccountSize.Lg', 'AvatarAccountSize.Xl' - * Defaults to AvatarAccountSize.Md - */ - size: PropTypes.oneOf(Object.values(AvatarAccountSize)), - /** - * The variant of the avatar to be rendered, it can render either a AvatarAccountVariant.Jazzicon or a AvatarAccountVariant.Blockie - */ - variant: PropTypes.oneOf(Object.values(AvatarAccountVariant)), - /** - * Address used for generating random image - */ - address: PropTypes.string.isRequired, - /** - * Add custom css class - */ - className: PropTypes.string, - /** - * AvatarAccount also accepts all Box props including but not limited to - * className, as(change root element of HTML element) and margin props - */ - ...Box.propTypes, -}; - -AvatarAccount.displayName = 'AvatarAccount'; diff --git a/ui/components/component-library/avatar-account/avatar-account.stories.js b/ui/components/component-library/avatar-account/avatar-account.stories.tsx similarity index 72% rename from ui/components/component-library/avatar-account/avatar-account.stories.js rename to ui/components/component-library/avatar-account/avatar-account.stories.tsx index 6041e5944..b41f9d8d9 100644 --- a/ui/components/component-library/avatar-account/avatar-account.stories.js +++ b/ui/components/component-library/avatar-account/avatar-account.stories.tsx @@ -1,6 +1,7 @@ import React from 'react'; -import Box from '../../ui/box/box'; -import { AlignItems, DISPLAY } from '../../../helpers/constants/design-system'; +import { StoryFn, Meta } from '@storybook/react'; +import { Box } from '../box'; +import { AlignItems, Display } from '../../../helpers/constants/design-system'; import { AvatarAccount } from './avatar-account'; import { AvatarAccountVariant, @@ -20,9 +21,7 @@ export default { argTypes: { size: { control: 'select', - options: Object.values(AvatarAccountSize).map( - (value) => value.toLowerCase(), // Removes reverse mapping from enum this is a temporary fix until we are using typescript for everything - ), + options: Object.values(AvatarAccountSize), }, address: { control: 'text' }, variant: { @@ -35,14 +34,14 @@ export default { size: AvatarAccountSize.Md, variant: AvatarAccountVariant.Jazzicon, }, -}; +} as Meta; export const DefaultStory = (args) => ; DefaultStory.storyName = 'Default'; -export const Size = (args) => ( - +export const Size: StoryFn = (args) => ( + @@ -51,15 +50,15 @@ export const Size = (args) => ( ); -export const Variant = (args) => ( - +export const Variant: StoryFn = (args) => ( + ); -export const Address = (args) => ( - +export const Address: StoryFn = (args) => ( + ( + { + size = AvatarAccountSize.Md, + address, + className = '', + variant = AvatarAccountVariant.Jazzicon, + ...props + }: AvatarAccountProps, + ref?: PolymorphicRef, + ) => ( + )} + > + {variant === AvatarAccountVariant.Jazzicon ? ( + + ) : ( + + )} + + ), +); diff --git a/ui/components/component-library/avatar-account/avatar-account.types.ts b/ui/components/component-library/avatar-account/avatar-account.types.ts index cde1d9a9c..16a2cd7d8 100644 --- a/ui/components/component-library/avatar-account/avatar-account.types.ts +++ b/ui/components/component-library/avatar-account/avatar-account.types.ts @@ -1,4 +1,5 @@ -import { Size } from '../../../helpers/constants/design-system'; +import type { PolymorphicComponentPropWithRef } from '../box'; +import { AvatarBaseStyleUtilityProps } from '../avatar-base/avatar-base.types'; export enum AvatarAccountVariant { Jazzicon = 'jazzicon', @@ -6,17 +7,50 @@ export enum AvatarAccountVariant { } export enum AvatarAccountSize { - Xs = Size.XS, - Sm = Size.SM, - Md = Size.MD, - Lg = Size.LG, - Xl = Size.XL, + Xs = 'xs', + Sm = 'sm', + Md = 'md', + Lg = 'lg', + Xl = 'xl', } -export const AvatarAccountDiameter = { +export const AvatarAccountDiameter: Record = { [AvatarAccountSize.Xs]: 16, [AvatarAccountSize.Sm]: 24, [AvatarAccountSize.Md]: 32, [AvatarAccountSize.Lg]: 40, [AvatarAccountSize.Xl]: 48, -} as const; +}; + +export interface AvatarAccountStyleUtilityProps + extends Omit { + /** + * The size of the AvatarAccount. + * Possible values could be 'AvatarAccountSize.Xs', 'AvatarAccountSize.Sm', 'AvatarAccountSize.Md', 'AvatarAccountSize.Lg', 'AvatarAccountSize.Xl' + * Defaults to AvatarAccountSize.Md + */ + size?: AvatarAccountSize; + /** + * The variant of the avatar to be rendered, it can render either a AvatarAccountVariant.Jazzicon or a AvatarAccountVariant.Blockie + */ + variant?: AvatarAccountVariant; + /** + * Address used for generating random image + */ + address: string; + /** + * Add custom css class + */ + className?: string; + /** + * AvatarAccount also accepts all Box props including but not limited to + * className, as(change root element of HTML element) and margin props + */ +} + +export type AvatarAccountProps = + PolymorphicComponentPropWithRef; + +export type AvatarAccountComponent = ( + props: AvatarAccountProps, +) => React.ReactElement | null; diff --git a/ui/components/component-library/avatar-account/index.js b/ui/components/component-library/avatar-account/index.ts similarity index 71% rename from ui/components/component-library/avatar-account/index.js rename to ui/components/component-library/avatar-account/index.ts index dfab0a38e..c88277a8b 100644 --- a/ui/components/component-library/avatar-account/index.js +++ b/ui/components/component-library/avatar-account/index.ts @@ -4,3 +4,4 @@ export { AvatarAccountVariant, AvatarAccountDiameter, } from './avatar-account.types'; +export type { AvatarAccountProps } from './avatar-account.types';