From e8b72ade5b7c212e52dae2950fd43db3ca8c45ad Mon Sep 17 00:00:00 2001 From: pahosler Date: Wed, 31 Oct 2018 09:34:34 -0500 Subject: [PATCH 1/2] add-toast --- PULL_REQUEST_TEMPLATE.md | 2 +- docs/package.json | 1 + docs/src/index.js | 5 +- package.json | 5 +- src/Toast/Toast.js | 35 ++++++++++ src/Toast/index.js | 1 + src/__test__/Toast/Toast.test.js | 69 +++++++++++++++++++ .../Toast/__snapshots__/Toast.test.js.snap | 7 ++ src/index.js | 1 + stories/toast.stories.js | 51 ++++++++++++++ 10 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 src/Toast/Toast.js create mode 100644 src/Toast/index.js create mode 100644 src/__test__/Toast/Toast.test.js create mode 100644 src/__test__/Toast/__snapshots__/Toast.test.js.snap create mode 100644 stories/toast.stories.js diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index f50f9e1..b7217a4 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,4 +1,4 @@ -# Pull Request Template +# title-component ## Types of changes diff --git a/docs/package.json b/docs/package.json index 845742d..7c4755a 100644 --- a/docs/package.json +++ b/docs/package.json @@ -14,6 +14,7 @@ "react-spectre": "../src/" }, "devDependencies": { + "babel-core": "^6.26.3", "babel-preset-env": "^1.7.0", "babel-preset-react": "^6.24.1", "babel-preset-stage-0": "^6.24.1", diff --git a/docs/src/index.js b/docs/src/index.js index 0a53c4c..d3c058d 100644 --- a/docs/src/index.js +++ b/docs/src/index.js @@ -6,5 +6,6 @@ import App from './components/App' render(
-
- , document.getElementById('root')) + , + document.getElementById('root') +) diff --git a/package.json b/package.json index 24ed69f..e78462f 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "@storybook/node-logger": "^3.4.10", "@storybook/react": "^3.4.10", "@storybook/storybook-deployer": "^2.3.0", + "@types/classnames": "^2.2.6", "babel-cli": "^6.26.0", "babel-core": "^6.26.3", "babel-eslint": "^10.0.1", @@ -81,7 +82,7 @@ "standard": "^12.0.1" }, "dependencies": { - "prop-types": "^15.6.2", - "classnames": "^2.2.6" + "classnames": "^2.2.6", + "prop-types": "^15.6.2" } } diff --git a/src/Toast/Toast.js b/src/Toast/Toast.js new file mode 100644 index 0000000..2957168 --- /dev/null +++ b/src/Toast/Toast.js @@ -0,0 +1,35 @@ +import React from 'react' +import PropTypes from 'prop-types' +import classnames from 'classnames' + +const colors = ['primary', 'secondary', 'success', 'warning', 'error'] + +const propTypes = { + children: PropTypes.node, + className: PropTypes.string, + color: PropTypes.oneOf(colors), + renderAs: PropTypes.oneOfType([PropTypes.func, PropTypes.string]) +} + +const defaultProps = { + renderAs: 'div' +} + +const Toast = ({ children, ...props }) => { + const { color, className, renderAs: Element, ...attributes } = props + + const classNames = classnames( + { + [`toast-${color}`]: color, + toast: 'toast' + }, + className + ) + + return {children} +} + +Toast.propTypes = propTypes +Toast.defaultProps = defaultProps + +export default Toast diff --git a/src/Toast/index.js b/src/Toast/index.js new file mode 100644 index 0000000..3363f44 --- /dev/null +++ b/src/Toast/index.js @@ -0,0 +1 @@ +export default from './Toast' diff --git a/src/__test__/Toast/Toast.test.js b/src/__test__/Toast/Toast.test.js new file mode 100644 index 0000000..1c2c3cb --- /dev/null +++ b/src/__test__/Toast/Toast.test.js @@ -0,0 +1,69 @@ +import React from 'react' +import { shallow } from 'enzyme' + +import { Toast } from '../..' + +describe('Toasts', () => { + it('Should render a span tag by default', () => { + const wrapper = shallow() + + expect(wrapper.type()).toBe('div') + }) + + it('Should render children', () => { + const wrapper = shallow(My toast) + + expect(wrapper.text()).toBe('My toast') + }) + + it('Should pass additional classNames', () => { + const wrapper = shallow(My Toasts) + + expect(wrapper.hasClass('extra')).toBe(true) + }) + + it('Should render with "Toasts" class by default', () => { + const wrapper = shallow(Toasts) + + expect(wrapper.hasClass('toast')).toBe(true) + }) + + it('Should render with "toast-{color}" class when a color is provided', () => { + const wrapper = shallow(Primary) + + expect(wrapper.hasClass('toast-primary')).toBe(true) + }) + + it('Should render custom tag', () => { + const wrapper = shallow(custom) + + expect(wrapper.type()).toBe('main') + }) + + it('Should match the snapshot', () => { + const wrapper = shallow() + + expect(wrapper).toMatchSnapshot() + }) + + // it('Should have Toasts classname', () => { + // const wrapper = renderer.create(Test) + + // expect(wrapper.toJSON()).toMatchSnapshot() + // }) + + // it('Should concat classnames in props with Spectre classnames', () => { + // const wrapper = renderer.create( + // classes + // ) + + // expect(wrapper.toJSON()).toMatchSnapshot() + // }) + + // it('Should use inline styles', () => { + // const wrapper = renderer.create( + // Inline styles) + + // expect(wrapper.toJSON()).toMatchSnapshot() + // }) +}) diff --git a/src/__test__/Toast/__snapshots__/Toast.test.js.snap b/src/__test__/Toast/__snapshots__/Toast.test.js.snap new file mode 100644 index 0000000..fc0eb95 --- /dev/null +++ b/src/__test__/Toast/__snapshots__/Toast.test.js.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Toasts Should match the snapshot 1`] = ` +
+`; diff --git a/src/index.js b/src/index.js index 128681d..3a4329e 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ export Icon from './Icon' export Label from './Label' export Code from './Code' export Chip from './Chip' +export Toast from './Toast' // ------------------- // GROUPED COMPONENTS diff --git a/stories/toast.stories.js b/stories/toast.stories.js new file mode 100644 index 0000000..8e0047d --- /dev/null +++ b/stories/toast.stories.js @@ -0,0 +1,51 @@ +import React from 'react' +import { storiesOf } from '@storybook/react' +import { Container, Col, Row, Toast } from '../src' + +storiesOf('Toasts', module) + .add('Default', () => ( + + + + +