From fa70aec286bb275f8eecb5d60a68ee7fef66b82e Mon Sep 17 00:00:00 2001 From: Binij Shrestha <93417120+thebinij@users.noreply.github.com> Date: Thu, 8 Jun 2023 23:46:00 +0545 Subject: [PATCH] added validTag string literal union (#19258) * added validTag string literal union * Adding deprecation message --------- Co-authored-by: georgewrmarshall Co-authored-by: Garrett Bear --- ui/components/component-library/text/index.ts | 2 +- .../component-library/text/text.test.tsx | 30 ++++++++--------- ui/components/component-library/text/text.tsx | 14 ++++---- .../component-library/text/text.types.ts | 32 +++++++++++++++++-- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/ui/components/component-library/text/index.ts b/ui/components/component-library/text/index.ts index 296bfc212..0e4a6d9a9 100644 --- a/ui/components/component-library/text/index.ts +++ b/ui/components/component-library/text/index.ts @@ -1,3 +1,3 @@ export { Text } from './text'; export { ValidTag, TextDirection, InvisibleCharacter } from './text.types'; -export type { TextProps } from './text.types'; +export type { TextProps, ValidTagType } from './text.types'; diff --git a/ui/components/component-library/text/text.test.tsx b/ui/components/component-library/text/text.test.tsx index b34eb75d7..1a22db879 100644 --- a/ui/components/component-library/text/text.test.tsx +++ b/ui/components/component-library/text/text.test.tsx @@ -10,7 +10,7 @@ import { TextVariant, Color, } from '../../../helpers/constants/design-system'; -import { TextDirection, ValidTag } from './text.types'; +import { TextDirection } from './text.types'; import { Text } from '.'; describe('Text', () => { @@ -21,20 +21,20 @@ describe('Text', () => { it('should render the Text with correct html elements', () => { const { getByText, container } = render( <> - p - h1 - h2 - h3 - h4 - h5 - h6 - span - strong - em - li - div - dt - dd + p + h1 + h2 + h3 + h4 + h5 + h6 + span + strong + em + li + div + dt + dd , ); expect(container.querySelector('p')).toBeDefined(); diff --git a/ui/components/component-library/text/text.tsx b/ui/components/component-library/text/text.tsx index 7ada78ffb..3bd9b423e 100644 --- a/ui/components/component-library/text/text.tsx +++ b/ui/components/component-library/text/text.tsx @@ -6,23 +6,23 @@ import { TextVariant, TextColor, } from '../../../helpers/constants/design-system'; -import { TextProps, ValidTag } from './text.types'; +import { TextProps } from './text.types'; const getTextElementDefault = (variant: TextVariant) => { switch (variant) { case TextVariant.displayMd: - return ValidTag.H1; + return 'h1'; case TextVariant.headingLg: - return ValidTag.H2; + return 'h2'; case TextVariant.headingMd: - return ValidTag.H3; + return 'h3'; case TextVariant.headingSm: - return ValidTag.H4; + return 'h4'; case TextVariant.inherit: - return ValidTag.Span; + return 'span'; // TextVariant.bodyLgMedium, TextVariant.bodyMd, TextVariant.bodyMdBold, TextVariant.bodySm, TextVariant.bodySmBold, TextVariant.bodyXs use default 'p' tag default: - return ValidTag.P; + return 'p'; } }; diff --git a/ui/components/component-library/text/text.types.ts b/ui/components/component-library/text/text.types.ts index 1f2e96bfe..56f9d9533 100644 --- a/ui/components/component-library/text/text.types.ts +++ b/ui/components/component-library/text/text.types.ts @@ -23,6 +23,14 @@ export enum TextDirection { */ export const InvisibleCharacter = '\u200B'; +/** + * @deprecated ValidTag enum is deprecated in favour of a union of strings. + * To change the root html element tag of the Text component, use the `as` prop and string value. + * e.g. `Hello World` + * + * Contribute to replacing the enum with a union of string by submitting a PR + */ + export enum ValidTag { Dd = 'dd', Div = 'div', @@ -44,11 +52,31 @@ export enum ValidTag { Header = 'header', } +export type ValidTagType = + | 'dd' + | 'div' + | 'dt' + | 'em' + | 'h1' + | 'h2' + | 'h3' + | 'h4' + | 'h5' + | 'h6' + | 'li' + | 'p' + | 'span' + | 'strong' + | 'ul' + | 'label' + | 'input' + | 'header'; + export interface TextProps extends BoxProps { /** * The text content of the Text component */ - children: React.ReactNode; + children?: React.ReactNode; /** * The variation of font styles including sizes and weights of the Text component * Possible values: @@ -107,7 +135,7 @@ export interface TextProps extends BoxProps { /** * Changes the root html element tag of the Text component. */ - as?: ValidTag; + as?: ValidTagType; /** * Additional className to assign the Text component */