Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store-ui): Create Badge component #894

Merged
merged 10 commits into from
Aug 24, 2021
14 changes: 14 additions & 0 deletions packages/store-ui/src/atoms/Badge/Badge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import { render } from '@testing-library/react'

import Badge from './Badge'

describe('Badge', () => {
it('should have `data-store-badge` attribute', () => {
const { getByText } = render(<Badge>-25%</Badge>)

const renderedBadge = getByText('-25%')

expect(renderedBadge).toHaveAttribute('data-store-badge')
})
})
23 changes: 23 additions & 0 deletions packages/store-ui/src/atoms/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ReactNode, ForwardedRef } from 'react'
import React, { forwardRef } from 'react'

export interface BadgeProps {
/**
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
*/
testId?: string
children?: ReactNode
victorhmp marked this conversation as resolved.
Show resolved Hide resolved
}

const Badge = forwardRef<HTMLDivElement, BadgeProps>(function Badge(
{ testId = 'store-badge', children }: BadgeProps,
ref: ForwardedRef<HTMLDivElement>
victorhmp marked this conversation as resolved.
Show resolved Hide resolved
) {
return (
<div ref={ref} data-testid={testId} data-store-badge>
{children}
</div>
)
})

export default Badge
2 changes: 2 additions & 0 deletions packages/store-ui/src/atoms/Badge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './Badge'
export type { BadgeProps } from './Badge'
12 changes: 12 additions & 0 deletions packages/store-ui/src/atoms/Badge/stories/Badge.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'
import Badge from '../Badge'

# Preview

<Canvas>
<Story id="atoms-badge--badge" />
</Canvas>

# Props

<ArgsTable of={Badge} />
37 changes: 37 additions & 0 deletions packages/store-ui/src/atoms/Badge/stories/Badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Story } from '@storybook/react'
import React from 'react'

import type { ComponentArgTypes } from '../../../typings/utils'
import Component from '../Badge'
import mdx from './Badge.mdx'

interface StoryControls {
badgeText: string
}

const BadgeTemplate: Story<StoryControls> = ({ badgeText }) => (
<Component>{badgeText}</Component>
)

export const Badge = BadgeTemplate.bind({})

const argTypes: ComponentArgTypes<StoryControls> = {
badgeText: {
name: 'Badge text',
control: {
type: 'text',
},
defaultValue: '-25%',
},
}

export default {
title: 'Atoms/Badge',
component: Badge,
argTypes,
parameters: {
docs: {
page: mdx,
},
},
}
4 changes: 2 additions & 2 deletions packages/store-ui/src/deprecated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export {
Progress,
Donut,
Avatar,
Badge,
Badge as UIBadge,
Close,
Alert,
Divider,
Expand Down Expand Up @@ -61,7 +61,7 @@ export type {
ProgressProps,
DonutProps,
AvatarProps,
BadgeProps,
BadgeProps as UIBadgeProps,
AlertProps,
DividerProps,
EmbedProps,
Expand Down
3 changes: 3 additions & 0 deletions packages/store-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type { SelectProps } from './atoms/Select'
export { default as Radio } from './atoms/Radio'
export type { RadioProps } from './atoms/Radio'

export { default as Badge } from './atoms/Badge'
export type { BadgeProps } from './atoms/Badge'

// Molecules
export { default as Bullets } from './molecules/Bullets'
export type { BulletsProps } from './molecules/Bullets'
Expand Down
8 changes: 8 additions & 0 deletions themes/theme-b2c-tailwind/src/atoms/badge.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[data-store-badge] {
display: inline-block;
color: #ffffff;
background-color: #f71963;
padding: 0.4rem;
border-radius: 0.2rem;
font-size: 0.9rem;
}
1 change: 1 addition & 0 deletions themes/theme-b2c-tailwind/src/atoms/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
@import "./price.css";
@import "./textarea.css";
@import "./overlay.css";
@import "./badge.css";