Skip to content

Commit

Permalink
feat(card): add card and cardbody
Browse files Browse the repository at this point in the history
add new components, styles and tests
  • Loading branch information
estevanmaito committed Jun 25, 2020
1 parent 5f63285 commit 3c64e85
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
30 changes: 30 additions & 0 deletions __tests__/Card.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import { mount } from 'enzyme'
import Card from '../src/Card'

describe('Card', () => {
it('should render without crashing', () => {
mount(<Card />)
})

it('should render with default styles', () => {
const expected = 'bg-white dark:bg-gray-800'
const wrapper = mount(<Card />)

expect(wrapper.find(Card).getDOMNode().getAttribute('class')).toContain(expected)
})

it('should render without default styles', () => {
const expected = 'bg-white dark:bg-gray-800'
const wrapper = mount(<Card colored />)

expect(wrapper.find(Card).getDOMNode().getAttribute('class')).not.toContain(expected)
})

it('should add classes to base styles', () => {
const expected = 'min-w-0 rounded-lg shadow-xs bg-red-400'
const wrapper = mount(<Card className="bg-red-400" colored />)

expect(wrapper.find(Card).getDOMNode().getAttribute('class')).toBe(expected)
})
})
16 changes: 16 additions & 0 deletions __tests__/CardBody.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'
import { mount } from 'enzyme'
import CardBody from '../src/CardBody'

describe('CardBody', () => {
it('should render without crashing', () => {
mount(<CardBody />)
})

it('should render with default styles', () => {
const expected = 'p-4'
const wrapper = mount(<CardBody />)

expect(wrapper.find(CardBody).getDOMNode().getAttribute('class')).toContain(expected)
})
})
28 changes: 28 additions & 0 deletions src/Card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 Card({ className, children, colored }) {
const { card } = useContext(ThemeContext) || defaultTheme

const baseStyle = card.base
const uncoloredStyle = card.default

const cls = classNames(baseStyle, !colored && uncoloredStyle, className)

return <div className={cls}>{children}</div>
}

Card.propTypes = {
children: PropTypes.element,
colored: PropTypes.bool,
className: PropTypes.string,
}

Card.defaultProps = {
colored: false,
}

export default Card
22 changes: 22 additions & 0 deletions src/CardBody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 CardBody({ className, children }) {
const { cardBody } = useContext(ThemeContext) || defaultTheme

const baseStyle = cardBody.base

const cls = classNames(baseStyle, className)

return <div className={cls}>{children}</div>
}

CardBody.propTypes = {
children: PropTypes.element,
className: PropTypes.string,
}

export default CardBody
8 changes: 8 additions & 0 deletions src/themes/default.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export default {
// Card
card: {
base: 'min-w-0 rounded-lg shadow-xs',
default: 'bg-white dark:bg-gray-800',
},
cardBody: {
base: 'p-4',
},
// Button
button: {
base:
Expand Down

0 comments on commit 3c64e85

Please sign in to comment.