Skip to content

Commit

Permalink
feat: add Label (#13)
Browse files Browse the repository at this point in the history
* feat: add Label

* fix: review changes

* fix: remove unused props

* fix: removed action property from stories

* fix: remove action from tests

* fix: change optional to string in stories

* fix: refactor test

* fix: yarn lock

* fix height of an icon

* fix: change gap size

* fix: fix import

---------

Co-authored-by: mwleklinskiVL <[email protected]>
  • Loading branch information
mateusz-kleszcz and wleklinskimateusz authored Aug 8, 2023
1 parent d9acc62 commit 91cf31f
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/components/Label/Label.props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SystemProps } from '@xstyled/styled-components';

import { config } from './Label.styles';
// import { ButtonProps } from '../Button';

import { Theme } from '@/theme';
import { DeepPartial } from '@/utility-types/DeepPartial';

export type LabelProps = {
label: string;
// action?: ButtonProps<'bare'>;
tooltip?: boolean;
optional?: string;
custom?: DeepPartial<SystemProps<Theme> & typeof config>;
};
36 changes: 36 additions & 0 deletions src/components/Label/Label.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Meta, StoryObj } from '@storybook/react';

import { Label } from './Label';

const meta = {
title: 'Components/Label',
component: Label,
tags: ['autodocs'],
args: {
label: 'Label',
},
} satisfies Meta<typeof Label>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {};

export const Optional: Story = {
args: {
optional: '(optional)',
},
};

export const Tooltip: Story = {
args: {
tooltip: true,
},
};

export const All: Story = {
args: {
optional: '(optional)',
tooltip: true,
},
};
30 changes: 30 additions & 0 deletions src/components/Label/Label.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SystemProps } from '@xstyled/styled-components';

import { Theme } from '@/theme';

type Config = SystemProps<Theme> & TooltipConfig & OptionalConfig;

type TooltipConfig = {
tooltip: SystemProps<Theme>;
};

type OptionalConfig = {
optional: SystemProps<Theme>;
};

export const config = {
text: 'medium-175',
color: 'content-primary',
display: 'flex',
alignItems: 'flex-start',
gap: 'component-gap-xSmall',
tooltip: {
color: 'content-secondary',
display: 'flex',
alignItems: 'center',
minHeight: 'xSmall',
},
optional: {
color: 'content-tertiary',
},
} as const satisfies Config;
55 changes: 55 additions & 0 deletions src/components/Label/Label.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Label } from './Label';
import { render } from '../../tests/render';

const getLabel = (jsx: JSX.Element) => {
const { queryByTestId } = render(jsx);

return {
label: queryByTestId('label'),
optional: queryByTestId('label-optional'),
tooltip: queryByTestId('label-tooltip'),
};
};

describe('Label', () => {
it('should render the label', () => {
const { label } = getLabel(<Label label="Hello there" />);
expect(label).toBeInTheDocument();
});

it('should render correct text', () => {
const { label } = getLabel(<Label label="Hello there" />);
expect(label).toHaveTextContent('Hello there');
});

it('should render optional if a prop passed', () => {
const { optional } = getLabel(
<Label label="Hello there" optional="optional" />
);
expect(optional).toBeInTheDocument();
});

it('should not render optional if a prop not passed', () => {
const { optional } = getLabel(<Label label="Hello there" />);
expect(optional).toBeNull();
});

it('should render tooltip if a prop passed', () => {
const { tooltip } = getLabel(<Label label="Hello there" tooltip />);
expect(tooltip).toBeInTheDocument();
});

it('should not render tooltip if a prop not passed', () => {
const { tooltip } = getLabel(<Label label="Hello there" />);
expect(tooltip).toBeNull();
});

it('should render correct colors', () => {
const { label, optional, tooltip } = getLabel(
<Label label="Hello there" optional="optional" tooltip />
);
expect(label).toHaveStyle('color: rgb(39, 39, 39)');
expect(optional).toHaveStyle('color: rgb(126, 126, 126);');
expect(tooltip).toHaveStyle('color: rgb(85, 85, 85)');
});
});
48 changes: 48 additions & 0 deletions src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Icon } from '@virtuslab/tetrisly-icons';
import { merge } from 'lodash';

import { LabelProps } from './Label.props';
import { config as defaultConfig } from './Label.styles';
// import { Button } from '../Button';

import { tet } from '@/tetrisly';
import { MarginProps } from '@/types/MarginProps';

export const Label = ({
label,
tooltip,
optional,
custom = {},
...rest
}: LabelProps & MarginProps) => {
const {
optional: optionalStyles,
tooltip: tooltipStyles,
...labelStyles
} = merge(defaultConfig, custom);
return (
<tet.div {...labelStyles} {...rest} data-testid="label">
{label}
{!!optional && (
<tet.span {...optionalStyles} data-testid="label-optional">
{optional}
</tet.span>
)}
{/* TODO: add tooltip instead of bare icon, when we get one */}
{tooltip && (
<tet.span {...tooltipStyles} data-testid="label-tooltip">
<Icon name="16-info" />
</tet.span>
)}
{/* TODO: add action when we discuss how they should behave */}
{/* {!!action && (
<Button
variant="bare"
{...action}
marginLeft="auto"
data-testid="label-action"
/>
)} */}
</tet.div>
);
};
1 change: 1 addition & 0 deletions src/components/Label/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Label } from './Label';
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Button } from './components/Button';
export { HelperText } from './components/HelperText';
export { Label } from './components/Label';
export { StatusDot } from './components/StatusDot';

0 comments on commit 91cf31f

Please sign in to comment.