Skip to content

Commit

Permalink
Merge branch 'feature/DES-331-Badge' of https://github.com/Amsterdam/…
Browse files Browse the repository at this point in the history
…design-system into feature/DES-544-Tip
  • Loading branch information
dlnr committed Jan 23, 2024
2 parents 0f02f9c + 3820afc commit 88c762d
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 19 deletions.
11 changes: 11 additions & 0 deletions packages/css/src/components/badge/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# Badge

A prominently colored box containing 1 or 2 words to guide the user in taking a specific action.

## Design

The badge can contain a string of text or a number.

## Guidelines

The default background colour is green.
Suggestions when to use the other colours will follow soon.
12 changes: 11 additions & 1 deletion packages/css/src/components/badge/badge.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license EUPL-1.2+
* Copyright (c) 2023 Gemeente Amsterdam
* Copyright (c) 2024 Gemeente Amsterdam
*/

.amsterdam-badge {
Expand Down Expand Up @@ -56,3 +56,13 @@
background-color: var(--amsterdam-badge-yellow-background-color);
color: var(--amsterdam-badge-yellow-color);
}

.amsterdam-badge--black {
background-color: var(--amsterdam-badge-black-background-color);
color: var(--amsterdam-badge-black-color);
}

.amsterdam-badge--white {
background-color: var(--amsterdam-badge-white-background-color);
color: var(--amsterdam-badge-white-color);
}
96 changes: 87 additions & 9 deletions packages/react/src/Badge/Badge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@testing-library/jest-dom'

describe('Badge', () => {
it('renders', () => {
const { container } = render(<Badge />)
const { container } = render(<Badge label="test" />)

const component = container.querySelector(':only-child')

Expand All @@ -14,15 +14,15 @@ describe('Badge', () => {
})

it('renders a design system BEM class name', () => {
const { container } = render(<Badge />)
const { container } = render(<Badge label="test" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge')
})

it('renders an additional class name', () => {
const { container } = render(<Badge className="extra" />)
const { container } = render(<Badge label="test" className="extra" />)

const component = container.querySelector(':only-child')

Expand All @@ -32,28 +32,106 @@ describe('Badge', () => {
it('supports ForwardRef in React', () => {
const ref = createRef<HTMLElement>()

const { container } = render(<Badge ref={ref} />)
const { container } = render(<Badge label="test" ref={ref} />)

const component = container.querySelector(':only-child')

expect(ref.current).toBe(component)
})

it('renders with a number label', () => {
const { container } = render(<Badge label={1} />)

const component = container.querySelector(':only-child')

expect(component).toHaveTextContent('1')
})

it('renders with default color', () => {
const { container } = render(<Badge />)
const { container } = render(<Badge label="test" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge')
expect(component).toHaveClass('amsterdam-badge--dark-green')
})

it('renders with specified color', () => {
const { container } = render(<Badge color="blue" />)
it('renders blue color', () => {
const { container } = render(<Badge label="test" color="blue" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge')
expect(component).toHaveClass('amsterdam-badge--blue')
})

it('renders dark-blue color', () => {
const { container } = render(<Badge label="test" color="dark-blue" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--dark-blue')
})

it('renders green color', () => {
const { container } = render(<Badge label="test" color="green" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--green')
})

it('renders dark-green color', () => {
const { container } = render(<Badge label="test" color="dark-green" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--dark-green')
})

it('renders orange color', () => {
const { container } = render(<Badge label="test" color="orange" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--orange')
})

it('renders magenta color', () => {
const { container } = render(<Badge label="test" color="magenta" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--magenta')
})

it('renders purple color', () => {
const { container } = render(<Badge label="test" color="purple" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--purple')
})

it('renders yellow color', () => {
const { container } = render(<Badge label="test" color="yellow" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--yellow')
})

it('renders black color', () => {
const { container } = render(<Badge label="test" color="black" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--black')
})

it('renders white color', () => {
const { container } = render(<Badge label="test" color="white" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge--white')
})
})
13 changes: 7 additions & 6 deletions packages/react/src/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/**
* @license EUPL-1.2+
* Copyright (c) 2023 Gemeente Amsterdam
* Copyright (c) 2024 Gemeente Amsterdam
*/

import clsx from 'clsx'
import { forwardRef } from 'react'
import type { ForwardedRef, HTMLAttributes, PropsWithChildren } from 'react'
import type { ForwardedRef, HTMLAttributes } from 'react'

export interface BadgeProps extends PropsWithChildren<HTMLAttributes<HTMLElement>> {
color?: 'blue' | 'dark-blue' | 'green' | 'dark-green' | 'orange' | 'magenta' | 'purple' | 'yellow'
export interface BadgeProps extends HTMLAttributes<HTMLElement> {
label: string | number
color?: 'blue' | 'dark-blue' | 'green' | 'dark-green' | 'orange' | 'magenta' | 'purple' | 'yellow' | 'black' | 'white'
}

export const Badge = forwardRef(
({ children, className, color = 'dark-green', ...restProps }: BadgeProps, ref: ForwardedRef<HTMLElement>) => (
({ label, className, color = 'dark-green', ...restProps }: BadgeProps, ref: ForwardedRef<HTMLElement>) => (
<span {...restProps} ref={ref} className={clsx('amsterdam-badge', `amsterdam-badge--${color}`, className)}>
{children}
{label}
</span>
),
)
Expand Down
8 changes: 8 additions & 0 deletions proprietary/tokens/src/components/amsterdam/badge.tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
"yellow": {
"background-color": { "value": "{amsterdam.color.yellow}" },
"color": { "value": "{amsterdam.color.primary-black}" }
},
"black": {
"background-color": { "value": "{amsterdam.color.primary-black}" },
"color": { "value": "{amsterdam.color.primary-white}" }
},
"white": {
"background-color": { "value": "{amsterdam.color.primary-white}" },
"color": { "value": "{amsterdam.color.primary-black}" }
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions storybook/storybook-react/src/Badge/Badge.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import README from "../../../../packages/css/src/components/badge/README.md?raw"

<Markdown>{README}</Markdown>

## Stories

### Default

<Primary />

<Controls />
6 changes: 3 additions & 3 deletions storybook/storybook-react/src/Badge/Badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license EUPL-1.2+
* Copyright (c) 2023 Gemeente Amsterdam
* Copyright (c) 2024 Gemeente Amsterdam
*/

import { Badge } from '@amsterdam/design-system-react'
Expand All @@ -10,14 +10,14 @@ const meta = {
title: 'Feedback/Badge',
component: Badge,
args: {
children: 'Tip',
label: 'Tip',
},
argTypes: {
color: {
control: {
type: 'inline-radio',
},
options: ['blue', 'dark-blue', 'green', 'dark-green', 'orange', 'magenta', 'purple', 'yellow'],
options: ['blue', 'dark-blue', 'green', 'dark-green', 'orange', 'magenta', 'purple', 'yellow', 'black', 'white'],
selected: 'dark-green',
},
},
Expand Down

0 comments on commit 88c762d

Please sign in to comment.