From 0909c6029b988ace82c9d62efa143ff61ebdc56c Mon Sep 17 00:00:00 2001 From: Alexandr Isaev Date: Thu, 16 May 2024 12:27:25 +0300 Subject: [PATCH] feat(Text): allow pass thml attributes durind Box inheritance --- src/components/Text/Text.tsx | 13 ++-- src/components/Text/__tests__/Text.test.tsx | 67 +++++++++++++++++++ .../__snapshots__/Text.test.tsx.snap | 59 ++++++++++++++++ src/components/layout/Box/Box.tsx | 11 ++- src/components/layout/Flex/Flex.tsx | 22 +++--- 5 files changed, 153 insertions(+), 19 deletions(-) create mode 100644 src/components/Text/__tests__/Text.test.tsx create mode 100644 src/components/Text/__tests__/__snapshots__/Text.test.tsx.snap diff --git a/src/components/Text/Text.tsx b/src/components/Text/Text.tsx index ca511f1598..55dbdfa054 100644 --- a/src/components/Text/Text.tsx +++ b/src/components/Text/Text.tsx @@ -15,18 +15,13 @@ export interface TextProps * Ability to override default html tag */ as?: C; - style?: React.CSSProperties; - className?: string; - id?: string; - children?: React.ReactNode; - title?: string; ellipsisLines?: number; } type TextRef = React.ComponentPropsWithRef['ref']; -type TextPropsWithoutRef = TextProps & - Omit, keyof TextProps>; +type TextPropsWithTypedAttrs = TextProps & + Omit, keyof TextProps>; /** * A component for working with typography. @@ -68,7 +63,7 @@ export const Text = React.forwardRef(function Text, + }: TextPropsWithTypedAttrs, ref?: TextRef, ) { const Tag: React.ElementType = as || 'span'; @@ -104,6 +99,6 @@ export const Text = React.forwardRef(function Text({ ref, ...props -}: TextPropsWithoutRef & {ref?: TextRef}) => React.ReactElement) & {displayName: string}; +}: TextPropsWithTypedAttrs & {ref?: TextRef}) => React.ReactElement) & {displayName: string}; Text.displayName = 'Text'; diff --git a/src/components/Text/__tests__/Text.test.tsx b/src/components/Text/__tests__/Text.test.tsx new file mode 100644 index 0000000000..ca93be8b14 --- /dev/null +++ b/src/components/Text/__tests__/Text.test.tsx @@ -0,0 +1,67 @@ +import React from 'react'; + +import {render} from '../../../../test-utils/utils'; +import {Button} from '../../Button'; +import {Text} from '../Text'; + +describe('Text', () => { + describe('should return expected html', () => { + test('if no props passed', () => { + const {container} = render(Hello World!); + + expect(container).toMatchSnapshot(); + }); + + test('if qa attr passed and variant changed', () => { + const {container} = render( + + Hello World! + , + ); + + expect(container).toMatchSnapshot(); + }); + + test('if html attributes passed and changed default html tag and with typed ref', () => { + const ComponentWithRef = () => { + const ref = React.useRef(null); + + return ( + + Hello World! + + ); + }; + + const {container} = render(); + + expect(container).toMatchSnapshot(); + }); + + test('if passed props what converted to classNames and styles', () => { + const {container} = render( + + Hello World! + , + ); + + expect(container).toMatchSnapshot(); + }); + + test('with another component substitution', () => { + const {container} = render( + + Hello World! + , + ); + + expect(container).toMatchSnapshot(); + }); + }); +}); diff --git a/src/components/Text/__tests__/__snapshots__/Text.test.tsx.snap b/src/components/Text/__tests__/__snapshots__/Text.test.tsx.snap new file mode 100644 index 0000000000..d8d2075752 --- /dev/null +++ b/src/components/Text/__tests__/__snapshots__/Text.test.tsx.snap @@ -0,0 +1,59 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Text should return expected html if html attributes passed and changed default html tag and with typed ref 1`] = ` +
+ +
+`; + +exports[`Text should return expected html if no props passed 1`] = ` +
+ + Hello World! + +
+`; + +exports[`Text should return expected html if passed props what converted to classNames and styles 1`] = ` +
+ + Hello World! + +
+`; + +exports[`Text should return expected html if qa attr passed and variant changed 1`] = ` +
+ + Hello World! + +
+`; + +exports[`Text should return expected html with another component substitution 1`] = ` +
+ +
+`; diff --git a/src/components/layout/Box/Box.tsx b/src/components/layout/Box/Box.tsx index 18275dc267..3e0d828494 100644 --- a/src/components/layout/Box/Box.tsx +++ b/src/components/layout/Box/Box.tsx @@ -35,6 +35,11 @@ export interface BoxProps spacing?: SpacingProps; } +type BoxRef = React.ComponentPropsWithRef['ref']; + +type BoxPropsWithTypedAttrs = BoxProps & + Omit, keyof BoxProps>; + /** * Basic block to build other components and for standalone usage as a smart block with build in support of most usable css properties and shortcut `spacing` properties. * ```tsx @@ -63,7 +68,7 @@ export const Box = React.forwardRef(function Box, ) { const Tag: React.ElementType = as || 'div'; @@ -88,4 +93,6 @@ export const Box = React.forwardRef(function Box( + props: BoxPropsWithTypedAttrs & {ref?: BoxRef}, +) => React.ReactElement) & {displayName: string}; diff --git a/src/components/layout/Flex/Flex.tsx b/src/components/layout/Flex/Flex.tsx index 1b8e5469a5..22ca7d3c2c 100644 --- a/src/components/layout/Flex/Flex.tsx +++ b/src/components/layout/Flex/Flex.tsx @@ -1,6 +1,5 @@ import React from 'react'; -import type {QAProps} from '../../types'; import {block} from '../../utils/cn'; import {Box} from '../Box/Box'; import type {BoxProps} from '../Box/Box'; @@ -12,7 +11,7 @@ import './Flex.scss'; const b = block('flex'); -export interface FlexProps extends QAProps, BoxProps { +export interface FlexProps extends BoxProps { /** * `flex-direction` property */ @@ -90,6 +89,11 @@ export interface FlexProps extends QAProps, space?: Space | MediaPartial; } +type FlexRef = React.ComponentPropsWithRef['ref']; + +type FlexPropsWithTypedAttrs = FlexProps & + Omit, keyof FlexProps>; + /** * Flexbox model utility component. * @@ -124,11 +128,9 @@ export interface FlexProps extends QAProps, * --- * Storybook - https://preview.gravity-ui.com/uikit/?path=/docs/layout--playground#flex */ -export const Flex = React.forwardRef(function Flex( - props: FlexProps, - ref: React.ComponentPropsWithRef['ref'], -) { +export const Flex = function Flex(props: FlexProps) { const { + as: propsAs, direction, grow, basis, @@ -151,6 +153,8 @@ export const Flex = React.forwardRef(function Flex {space @@ -209,4 +213,6 @@ export const Flex = React.forwardRef(function Flex ); -}); +} as (( + props: FlexPropsWithTypedAttrs & {ref?: FlexRef}, +) => React.ReactElement) & {displayName: string};