Skip to content

Commit

Permalink
Feat(web-react): Add prefixed utility class #DS-1618
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj committed Jan 13, 2025
1 parent 591b1f5 commit dd814b8
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 23 deletions.
7 changes: 6 additions & 1 deletion packages/web-react/src/components/Box/__tests__/Box.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { restPropsTest, stylePropsTest } from '@local/tests';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import { SpiritBoxProps } from '../../../types';
import Box from '../Box';

const dataProvider = [
Expand Down Expand Up @@ -73,7 +74,11 @@ const dataProvider = [
},
];

const BoxPrefixTestComponent = ({ children }: SpiritBoxProps) => <Box backgroundColor="primary">{children}</Box>;

describe('Box', () => {
classNamePrefixProviderTest(BoxPrefixTestComponent, 'bg-primary');

stylePropsTest(Box);

restPropsTest(Box, 'div');
Expand Down
17 changes: 11 additions & 6 deletions packages/web-react/src/components/Box/useBoxStyleProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import { ElementType } from 'react';
import { BorderColors } from '../../constants';
import { useClassNamePrefix } from '../../hooks';
import { SpiritBoxProps } from '../../types';

export interface UseBoxStyleProps<T> {
Expand All @@ -14,17 +15,21 @@ export const useBoxStyleProps = (
props: Partial<SpiritBoxProps<ElementType>>,
): UseBoxStyleProps<Partial<SpiritBoxProps<ElementType>>> => {
const { backgroundColor, borderColor, borderRadius, borderStyle, borderWidth, ...restProps } = props || {};
const boxBackgroundColor = backgroundColor ? `bg-${backgroundColor}` : '';
let boxBorderColor = borderColor ? borderColor.replace('', 'border-') : '';
const boxBackgroundClassName = useClassNamePrefix(`bg-${backgroundColor}`);
const boxBorderClassName = useClassNamePrefix('border-');
const boxRadiiClassName = useClassNamePrefix('rounded-');

const boxBackgroundColor = backgroundColor ? boxBackgroundClassName : '';
let boxBorderColor = borderColor ? borderColor.replace('', boxBorderClassName) : '';
let boxBorderRadius = '';
let boxBorderStyle = '';
const boxBorderWidth = borderWidth ? borderWidth.replace('', 'border-') : '';
const boxBorderWidth = borderWidth ? borderWidth.replace('', boxBorderClassName) : '';

if (borderWidth && parseInt(borderWidth, 10) > 0) {
boxBorderStyle = `border-${borderStyle}`;
boxBorderRadius = borderRadius ? borderRadius.replace('', 'rounded-') : '';
boxBorderStyle = `${boxBorderClassName}${borderStyle}`;
boxBorderRadius = borderRadius ? borderRadius.replace('', boxRadiiClassName) : '';
if (!borderColor) {
boxBorderColor = `border-${BorderColors.BASIC}`;
boxBorderColor = `${boxBorderClassName}${BorderColors.BASIC}`;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import '@testing-library/jest-dom';
import { restPropsTest, stylePropsTest } from '@local/tests';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import { BackgroundColors } from '../../../constants';
import Footer from '../Footer';

describe('Footer', () => {
classNamePrefixProviderTest(Footer, 'bg-secondary');

stylePropsTest(Footer);

restPropsTest(Footer, 'footer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export interface UseFooterStyleProps {
classProps: string;
}

export function useFooterStyleProps(props: Partial<SpiritFooterProps>): UseFooterStyleProps {
export const useFooterStyleProps = (props: Partial<SpiritFooterProps>): UseFooterStyleProps => {
const { backgroundColor } = props;
const footerBackgroundColor = backgroundColor ? `bg-${backgroundColor}` : '';
const footerBackgroundClassName = `bg-${backgroundColor}`;

const footerBackgroundColor = backgroundColor ? footerBackgroundClassName : '';

const classProps = classNames({
[footerBackgroundColor]: backgroundColor,
Expand All @@ -16,4 +18,4 @@ export function useFooterStyleProps(props: Partial<SpiritFooterProps>): UseFoote
return {
classProps,
};
}
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import React from 'react';
import { restPropsTest, stylePropsTest } from '@local/tests';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import { SpiritItemProps } from '../../../types';
import Item from '../Item';

jest.mock('../../../hooks/useIcon');

describe('Item', () => {
classNamePrefixProviderTest(Item, 'Item');

stylePropsTest(Item);

restPropsTest((props: SpiritItemProps) => <Item {...props} />, 'button');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import React from 'react';
import { requiredPropsTest } from '@local/tests';
import { classNamePrefixProviderTest, requiredPropsTest } from '@local/tests';
import { TextFieldType } from '../../../types';
import TextFieldBase from '../TextFieldBase';

describe('TextFieldBase', () => {
classNamePrefixProviderTest(TextFieldBase, 'TextField');

requiredPropsTest(TextFieldBase, 'textbox', 'id', 'textfieldbase');

describe.each(['text', 'password', 'email'])('input type %s', (type) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { restPropsTest, stylePropsTest } from '@local/tests';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import { Button } from '../../Button';
import { Tooltip, TooltipPopover, TooltipTrigger } from '..';

Expand All @@ -10,6 +10,8 @@ describe('Tooltip', () => {
const triggerText = 'TooltipTrigger';
const popoverText = 'TooltipPopover';

classNamePrefixProviderTest(Tooltip, 'Tooltip');

stylePropsTest((props) => <Tooltip id={id} {...props} data-testid="tooltip-test" />, 'tooltip-test');

restPropsTest((props) => <Tooltip id={id} {...props} />, 'div');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { restPropsTest, stylePropsTest } from '@local/tests';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import { Button } from '../../Button';
import { TooltipTrigger } from '..';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { restPropsTest, stylePropsTest } from '@local/tests';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import UNSTABLE_Slider from '../UNSTABLE_Slider';

describe('UNSTABLE_Slider', () => {
Expand All @@ -13,6 +13,8 @@ describe('UNSTABLE_Slider', () => {
value: defaultValue,
};

classNamePrefixProviderTest(UNSTABLE_Slider, 'UNSTABLE_Slider');

stylePropsTest(
(props) => <UNSTABLE_Slider id={defaultProps.id} {...props} data-testid="slider-test" />,
'slider-test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import classNames from 'classnames';
import React, { ElementType } from 'react';
import { useClassNamePrefix, useStyleProps } from '../../hooks';
import { useStyleProps } from '../../hooks';
import { SpiritTruncateProps } from '../../types/truncate';
import { useTruncateStyleProps } from './useTruncateStyleProps';

Expand All @@ -25,11 +25,7 @@ const UNSTABLE_Truncate = <T extends ElementType = 'span'>(props: SpiritTruncate
};

return (
<ElementTag
{...otherProps}
{...truncateStyleProps}
className={classNames(useClassNamePrefix(classProps), styleProps.className)}
>
<ElementTag {...otherProps} {...truncateStyleProps} className={classNames(classProps, styleProps.className)}>
{children}
</ElementTag>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CSSProperties, ElementType } from 'react';
import { useClassNamePrefix } from '../../hooks';
import { SpiritTruncateProps } from '../../types/truncate';

interface TruncateCSSProperties extends CSSProperties {
Expand All @@ -14,7 +15,7 @@ export interface TruncateStyles<T extends ElementType> {
export function useTruncateStyleProps<T extends ElementType>(props: SpiritTruncateProps<T>): TruncateStyles<T> {
const { lines, ...restProps } = props;

const TruncateMultilinesClass = 'text-truncate-multiline';
const TruncateMultilinesClass = useClassNamePrefix('text-truncate-multiline');
const classProps = TruncateMultilinesClass;

const truncateStyle: TruncateCSSProperties = {};
Expand Down

0 comments on commit dd814b8

Please sign in to comment.