From c05c16f0f8aa117cda5a43bd0c1d5944772af1e5 Mon Sep 17 00:00:00 2001 From: Mateusz Kleszcz Date: Mon, 11 Sep 2023 10:30:23 +0200 Subject: [PATCH 1/4] refactor: tet-217 label (#49) * refactor: TET-217 label * fix: TET-217 add label attributes, action prop --- .../CheckboxGroup/CheckboxGroup.props.ts | 2 +- src/components/Label/Label.props.ts | 13 +++--- src/components/Label/Label.stories.ts | 11 +++++ src/components/Label/Label.styles.ts | 42 +++++++++---------- src/components/Label/Label.test.tsx | 32 ++++++++++++++ src/components/Label/Label.tsx | 38 ++++++++--------- src/components/Label/index.ts | 1 + src/components/Label/stylesBuilder/index.ts | 1 + .../Label/stylesBuilder/stylesBuilder.ts | 29 +++++++++++++ .../RadioButtonGroup.props.ts | 2 +- 10 files changed, 121 insertions(+), 50 deletions(-) create mode 100644 src/components/Label/stylesBuilder/index.ts create mode 100644 src/components/Label/stylesBuilder/stylesBuilder.ts diff --git a/src/components/CheckboxGroup/CheckboxGroup.props.ts b/src/components/CheckboxGroup/CheckboxGroup.props.ts index a358d53d..d03b2eb0 100644 --- a/src/components/CheckboxGroup/CheckboxGroup.props.ts +++ b/src/components/CheckboxGroup/CheckboxGroup.props.ts @@ -1,7 +1,7 @@ import { CheckboxGroupConfig } from './CheckboxGroup.style'; import { CheckboxProps } from '../Checkbox/Checkbox.props'; import { HelperTextProps } from '../HelperText/HelperText.props'; -import { LabelProps } from '../Label/Label.props'; +import type { LabelProps } from '../Label'; import { DeepPartial } from '@/utility-types/DeepPartial'; diff --git a/src/components/Label/Label.props.ts b/src/components/Label/Label.props.ts index 9759235b..2044987c 100644 --- a/src/components/Label/Label.props.ts +++ b/src/components/Label/Label.props.ts @@ -1,12 +1,13 @@ -import { LabelConfig } from './Label.styles'; -// import { ButtonProps } from '../Button'; +import type { LabelHTMLAttributes } from 'react'; -import { DeepPartial } from '@/utility-types/DeepPartial'; +import type { LabelConfig } from './Label.styles'; + +import type { Action } from '@/types'; export type LabelProps = { label: string; - // action?: ButtonProps<'bare'>; + action?: Action; tooltip?: boolean; optional?: string; - custom?: DeepPartial; -}; + custom?: LabelConfig; +} & Omit, 'color'>; diff --git a/src/components/Label/Label.stories.ts b/src/components/Label/Label.stories.ts index 4a745069..d8f1ee69 100644 --- a/src/components/Label/Label.stories.ts +++ b/src/components/Label/Label.stories.ts @@ -28,9 +28,20 @@ export const Tooltip: Story = { }, }; +export const Action: Story = { + args: { + action: { + label: 'Action', + }, + }, +}; + export const All: Story = { args: { optional: '(optional)', tooltip: true, + action: { + label: 'Action', + }, }, }; diff --git a/src/components/Label/Label.styles.ts b/src/components/Label/Label.styles.ts index 0f67236b..082633e8 100644 --- a/src/components/Label/Label.styles.ts +++ b/src/components/Label/Label.styles.ts @@ -1,17 +1,12 @@ -import { SystemProps } from '@xstyled/styled-components'; +import type { BaseProps } from '@/types/BaseProps'; -import { Theme } from '@/theme'; -import { BaseProps } from '@/types/BaseProps'; - -export type LabelConfig = BaseProps & TooltipConfig & OptionalConfig; - -type TooltipConfig = { - tooltip?: SystemProps; -}; - -type OptionalConfig = { - optional?: SystemProps; -}; +export type LabelConfig = { + innerElements?: { + tooltip?: BaseProps; + optional?: BaseProps; + action?: BaseProps; + }; +} & BaseProps; export const defaultConfig = { text: 'medium-175', @@ -19,13 +14,18 @@ export const defaultConfig = { display: 'flex', alignItems: 'flex-start', gap: 'component-gap-xSmall', - tooltip: { - color: 'content-secondary', - display: 'flex', - alignItems: 'center', - minHeight: 'xSmall', - }, - optional: { - color: 'content-tertiary', + innerElements: { + tooltip: { + color: 'content-secondary', + display: 'flex', + alignItems: 'center', + minHeight: 'xSmall', + }, + optional: { + color: 'content-tertiary', + }, + action: { + ml: 'auto', + }, }, } satisfies LabelConfig; diff --git a/src/components/Label/Label.test.tsx b/src/components/Label/Label.test.tsx index 8d7eae10..c804c9b8 100644 --- a/src/components/Label/Label.test.tsx +++ b/src/components/Label/Label.test.tsx @@ -1,12 +1,15 @@ import { Label } from './Label'; import { render } from '../../tests/render'; +import { customPropTester } from '@/tests/customPropTester'; + const getLabel = (jsx: JSX.Element) => { const { queryByTestId } = render(jsx); return { label: queryByTestId('label'), optional: queryByTestId('label-optional'), + action: queryByTestId('label-action'), tooltip: queryByTestId('label-tooltip'), }; }; @@ -44,6 +47,18 @@ describe('Label', () => { expect(tooltip).toBeNull(); }); + it('should render action if a prop passed', () => { + const { action } = getLabel( +