Skip to content

Commit

Permalink
feat(ui): add chip, traffic light and centered-column
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael2Gray committed Jun 4, 2022
1 parent 09835ee commit 2e6351e
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ui/centered-column/centered-column.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

type CenteredColumnProps = { children: React.ReactNode };

export const CenteredColumn = ({ children }: CenteredColumnProps) => (
<div className="min-h-screen flex items-center justify-center">
<div className="max-w-md w-full space-y-8">{children}</div>
</div>
);
18 changes: 18 additions & 0 deletions src/ui/centered-column/centered-column.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { ComponentMeta, ComponentStory } from '@storybook/react';

import { CenteredColumn } from './centered-column.component';

export default {
title: 'Atoms/Centered Column',
component: CenteredColumn,
} as ComponentMeta<typeof CenteredColumn>;

const Template: ComponentStory<typeof CenteredColumn> = (args) => (
<CenteredColumn {...args} />
);

export const Default = Template.bind({});
Default.args = {
children: <div className="text-center p-4">This is a Centered Column</div>,
};
1 change: 1 addition & 0 deletions src/ui/centered-column/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './centered-column.component';
27 changes: 27 additions & 0 deletions src/ui/chip/chip.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import clsx from 'clsx';

import { ValhallaVariant } from '../../models';
import { CHIP_VARIANTS } from './chip.constant';

export type ChipProps = {
children: React.ReactNode;
variant?: ValhallaVariant;
className?: string;
};

export const Chip = ({
variant = 'default',
children,
className,
}: ChipProps) => (
<div
className={clsx(
'px-2 py-1 rounded text-xs font-bold',
CHIP_VARIANTS[variant],
className
)}
>
{children}
</div>
);
9 changes: 9 additions & 0 deletions src/ui/chip/chip.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ValhallaVariant } from '../../models';

export const CHIP_VARIANTS: Record<ValhallaVariant, string> = {
default: 'bg-neutral-400 text-white',
info: 'bg-blue-400 text-white',
success: 'bg-brand-400 text-white',
error: 'bg-red-600 text-white',
warning: 'bg-yellow-400 text-white',
};
20 changes: 20 additions & 0 deletions src/ui/chip/chip.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { ComponentMeta, ComponentStory } from '@storybook/react';

import { Chip } from './chip.component';

export default {
title: 'Atoms/Chip',
component: Chip,
} as ComponentMeta<typeof Chip>;

const Template: ComponentStory<typeof Chip> = (args) => (
<div className="flex">
<Chip {...args} />
</div>
);

export const Default = Template.bind({});
Default.args = {
children: 'Chip',
};
1 change: 1 addition & 0 deletions src/ui/chip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './chip.component';
3 changes: 3 additions & 0 deletions src/ui/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './alert';
export * from './button';
export * from './card';
export * from './centered-column';
export * from './chip';
export * from './coords';
export * from './date-format';
export * from './distance';
Expand All @@ -17,5 +19,6 @@ export * from './stack';
export * from './status';
export * from './temperature';
export * from './text-field';
export * from './traffic-light-number';
export * from './typography';
export * from './weather-icon';
1 change: 1 addition & 0 deletions src/ui/traffic-light-number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './traffic-light-number.component';
28 changes: 28 additions & 0 deletions src/ui/traffic-light-number/traffic-light-number.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import clsx from 'clsx';

type TrafficLightNumberProps = {
value: number;
total?: number;
};

export const TrafficLightNumber = ({
value,
total = 20,
}: TrafficLightNumberProps) => {
const quarter = total / 4;
const half = total / 2;

return (
<span
data-testid="traffic-light-number"
className={clsx({
'text-yellow-400': value >= quarter && value <= half,
'text-red-600': value <= quarter,
'text-green-600': value >= half,
})}
>
{value}
</span>
);
};
21 changes: 21 additions & 0 deletions src/ui/traffic-light-number/traffic-light-number.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { render, screen } from '@testing-library/react';

import { TrafficLightNumber } from './traffic-light-number.component';

describe('TrafficLightNumber', () => {
test.each([
[20, 'text-green-600'],
[10, 'text-yellow-400'],
[5, 'text-red-600'],
])('renders the value', (value, className) => {
const { container } = render(
<TrafficLightNumber value={value} total={20} />
);

const el = screen.getByTestId('traffic-light-number');

expect(el).toHaveTextContent(String(value));
expect(container.getElementsByClassName(className)).toBeTruthy();
});
});
16 changes: 16 additions & 0 deletions src/ui/traffic-light-number/traffic-light-number.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { ComponentMeta, ComponentStory } from '@storybook/react';

import { TrafficLightNumber } from './traffic-light-number.component';

export default {
title: 'Atoms/Traffic Light Number',
component: TrafficLightNumber,
} as ComponentMeta<typeof TrafficLightNumber>;

const Template: ComponentStory<typeof TrafficLightNumber> = (args) => (
<TrafficLightNumber {...args} />
);

export const Default = Template.bind({});
Default.args = { value: 10 };

0 comments on commit 2e6351e

Please sign in to comment.