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

added validTag string literal union #19258

Merged
merged 6 commits into from
Jun 8, 2023
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
2 changes: 1 addition & 1 deletion ui/components/component-library/text/index.ts
Original file line number Diff line number Diff line change
@@ -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';
30 changes: 15 additions & 15 deletions ui/components/component-library/text/text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -21,20 +21,20 @@ describe('Text', () => {
it('should render the Text with correct html elements', () => {
const { getByText, container } = render(
<>
<Text as={ValidTag.P}>p</Text>
<Text as={ValidTag.H1}>h1</Text>
<Text as={ValidTag.H2}>h2</Text>
<Text as={ValidTag.H3}>h3</Text>
<Text as={ValidTag.H4}>h4</Text>
<Text as={ValidTag.H5}>h5</Text>
<Text as={ValidTag.H6}>h6</Text>
<Text as={ValidTag.Span}>span</Text>
<Text as={ValidTag.Strong}>strong</Text>
<Text as={ValidTag.Em}>em</Text>
<Text as={ValidTag.Li}>li</Text>
<Text as={ValidTag.Div}>div</Text>
<Text as={ValidTag.Dt}>dt</Text>
<Text as={ValidTag.Dd}>dd</Text>
<Text as="p">p</Text>
<Text as="h1">h1</Text>
<Text as="h2">h2</Text>
<Text as="h3">h3</Text>
<Text as="h4">h4</Text>
<Text as="h5">h5</Text>
<Text as="h6">h6</Text>
<Text as="span">span</Text>
<Text as="strong">strong</Text>
<Text as="em">em</Text>
<Text as="li">li</Text>
<Text as="div">div</Text>
<Text as="dt">dt</Text>
<Text as="dd">dd</Text>
</>,
);
expect(container.querySelector('p')).toBeDefined();
Expand Down
14 changes: 7 additions & 7 deletions ui/components/component-library/text/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
};

Expand Down
32 changes: 30 additions & 2 deletions ui/components/component-library/text/text.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<Text as="h1">Hello World</Text>`
*
* Contribute to replacing the enum with a union of string by submitting a PR
*/

export enum ValidTag {
Dd = 'dd',
Div = 'div',
Expand All @@ -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:
Expand Down Expand Up @@ -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
*/
Expand Down