-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b367072
commit 7638177
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import Badge from '../src/Badge' | ||
|
||
describe('Badge', () => { | ||
it('should render without crashing', () => { | ||
mount(<Badge />) | ||
}) | ||
|
||
it('should render with base styles', () => { | ||
const expected = 'px-2 py-1 text-xs font-medium leading-none rounded-full' | ||
const wrapper = mount(<Badge />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
|
||
it('should render with success styles', () => { | ||
const expected = 'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100' | ||
const wrapper = mount(<Badge type="success" />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
|
||
it('should render with danger styles', () => { | ||
const expected = 'text-red-700 bg-red-100 dark:text-red-100 dark:bg-red-700' | ||
const wrapper = mount(<Badge type="danger" />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
|
||
it('should render with warning styles', () => { | ||
const expected = 'text-orange-700 bg-orange-100 dark:text-white dark:bg-orange-600' | ||
const wrapper = mount(<Badge type="warning" />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
|
||
it('should render with neutral styles', () => { | ||
const expected = 'text-gray-700 bg-gray-100 dark:text-gray-100 dark:bg-gray-700' | ||
const wrapper = mount(<Badge type="neutral" />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
|
||
it('should render with primary styles', () => { | ||
const expected = 'text-purple-700 bg-purple-100 dark:text-white dark:bg-purple-600' | ||
const wrapper = mount(<Badge type="primary" />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
|
||
it('should render with primary styles when no type is used', () => { | ||
const expected = 'text-purple-700 bg-purple-100 dark:text-white dark:bg-purple-600' | ||
const wrapper = mount(<Badge />) | ||
|
||
expect(wrapper.find('span').getDOMNode().getAttribute('class')).toContain(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, { useContext } from 'react' | ||
import classNames from 'classnames' | ||
import PropTypes from 'prop-types' | ||
import { ThemeContext } from './context/ThemeContext' | ||
import defaultTheme from './themes/default' | ||
|
||
function Badge(props) { | ||
const { className, children, type, ...other } = props | ||
|
||
const { badge } = useContext(ThemeContext) || defaultTheme | ||
|
||
const baseStyle = badge.base | ||
const typeStyle = { | ||
success: badge.success, | ||
danger: badge.danger, | ||
warning: badge.warning, | ||
neutral: badge.neutral, | ||
primary: badge.primary, | ||
} | ||
|
||
const cls = classNames(baseStyle, typeStyle[type], className) | ||
|
||
return ( | ||
<span className={cls} {...other}> | ||
{children} | ||
</span> | ||
) | ||
} | ||
|
||
Badge.propTypes = { | ||
children: PropTypes.node, | ||
className: PropTypes.string, | ||
type: PropTypes.oneOf(['success', 'danger', 'warning', 'neutral', 'primary']), | ||
} | ||
|
||
Badge.defaultProps = { | ||
type: 'primary', | ||
} | ||
|
||
export default Badge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters