diff --git a/.eslintignore b/.eslintignore index 1e263e0ef99a..93e974542d01 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,6 +6,9 @@ node_modules **/demo/** docs/public +*.bundle.js +*.js.map + !.remarkrc.js !.eslintrc.js !.eslintrc-markdown.js diff --git a/addons/events/CHANGELOG.md b/addons/events/CHANGELOG.md new file mode 100644 index 000000000000..1d997b5493bc --- /dev/null +++ b/addons/events/CHANGELOG.md @@ -0,0 +1,15 @@ +# ChangeLog + +### v1.1.0 + +14-04-2017 + +- Added the ability to edit event payload +- Added live example +- Updated readme + +### v1.0.0 + +13-04-2017 + +- Initial version diff --git a/addons/events/LICENSE b/addons/events/LICENSE new file mode 100644 index 000000000000..85a6c94557e5 --- /dev/null +++ b/addons/events/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Kadira Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/addons/events/README.md b/addons/events/README.md new file mode 100644 index 000000000000..4b5caf589a87 --- /dev/null +++ b/addons/events/README.md @@ -0,0 +1,94 @@ +# Storybook Addon Events + +This [storybook](https://storybooks.js.org) ([source](https://github.com/storybooks/storybook)) addon allows you to add events for your stories. + +![Storybook Addon Events Example](docs/demo1.png) +[Storybook Addon Events Live Demo](https://z4o4z.github.io/storybook-addon-events/index.html) + +### Getting Started + +```sh +npm i --save-dev @storybook/addon-events +``` + +Then create a file called `addons.js` in your storybook config. + +Add following content to it: + +```js +import '@storybook/addon-actions'; +import '@storybook/addon-links'; +import '@storybook/addon-events/register'; +``` + +Then write your stories like this: + +```js +import { storiesOf } from '@storybook/react'; +import WithEvents from '@storybook/addon-events'; +import EventEmiter from 'event-emiter'; + +import Logger from './Logger'; +import * as EVENTS from './events'; + +const emiter = new EventEmiter(); +const emit = emiter.emit.bind(emiter); + + +storiesOf('WithEvents', module) + .addDecorator(getStory => ( + + {getStory()} + + )) + .add('Logger', () => ); +``` diff --git a/addons/events/docs/.DS_Store b/addons/events/docs/.DS_Store new file mode 100644 index 000000000000..817e5e65edf1 Binary files /dev/null and b/addons/events/docs/.DS_Store differ diff --git a/addons/events/docs/demo1.png b/addons/events/docs/demo1.png new file mode 100644 index 000000000000..e65cab4e9605 Binary files /dev/null and b/addons/events/docs/demo1.png differ diff --git a/addons/events/package.json b/addons/events/package.json new file mode 100644 index 000000000000..03be701ec8bb --- /dev/null +++ b/addons/events/package.json @@ -0,0 +1,37 @@ +{ + "name": "@storybook/addon-events", + "version": "3.0.1", + "description": "Add events to your Storybook stories.", + "keywords": [ + "addon", + "events", + "react", + "storybook" + ], + "license": "MIT", + "main": "dist/index.js", + "repository": { + "type": "git", + "url": "git@github.com:storybooks/storybook.git" + }, + "scripts": { + "build-storybook": "build-storybook", + "prepublish": "node ../../scripts/prepublish.js", + "storybook": "start-storybook -p 6006" + }, + "dependencies": { + "@storybook/addons": "^3.0.0", + "babel-runtime": "^6.5.0", + "format-json": "^1.0.3", + "prop-types": "^15.5.10", + "react-textarea-autosize": "^4.0.5", + "uuid": "^3.0.1" + }, + "devDependencies": { + "react": "^15.3.2", + "react-dom": "^15.3.2" + }, + "peerDependencies": { + "react": "*" + } +} diff --git a/addons/events/register.js b/addons/events/register.js new file mode 100644 index 000000000000..e69edbea3ed1 --- /dev/null +++ b/addons/events/register.js @@ -0,0 +1 @@ +require('./dist').register(); diff --git a/addons/events/src/components/Event.js b/addons/events/src/components/Event.js new file mode 100644 index 000000000000..35c93f3d607c --- /dev/null +++ b/addons/events/src/components/Event.js @@ -0,0 +1,137 @@ +import React, { Component } from 'react'; +import json from 'format-json'; +import Textarea from 'react-textarea-autosize'; +import PropTypes from 'prop-types'; + +const styles = { + item: { + padding: '10 0', + }, + buttonWrapper: { + textAlign: 'center', + }, + button: { + display: 'inline-block', + fontFamily: ` + -apple-system, ".SFNSText-Regular", "San Francisco", "Roboto", + "Segoe UI", "Helvetica Neue", "Lucida Grande", sans-serif + `, + fontSize: 14, + padding: 10, + margin: 10, + width: '40%', + }, + textArea: { + display: 'block', + boxSizing: 'border-box', + margin: 0, + width: '100%', + maxWidth: '100%', + verticalAlign: 'middle', + outline: 'none', + border: '1px solid #c7c7c7', + borderRadius: 2, + fontSize: 13, + padding: '5px', + color: 'rgb(51, 51, 51)', + fontFamily: 'Arial, sans-serif', + }, +}; + +export default class Item extends Component { + static propTypes = { + name: PropTypes.string.isRequired, + title: PropTypes.string.isRequired, + onEmit: PropTypes.func.isRequired, + payload: PropTypes.any, // eslint-disable-line react/forbid-prop-types + }; + + static defaultProps = { + payload: {}, + }; + + static getJSONFromString(str) { + try { + return JSON.parse(str); + } catch (e) { + return str; + } + } + + state = {}; + + componentWillMount() { + const payloadString = json.plain(this.props.payload); + + this.setState({ + failed: false, + payload: Item.getJSONFromString(payloadString), + payloadString, + isTextAreaShowed: false, + }); + } + + onChange = ({ target: { value } }) => { + const newState = { + payloadString: value, + }; + + try { + newState.payload = JSON.parse(value.trim()); + newState.failed = false; + } catch (err) { + newState.failed = true; + } + + this.setState(newState); + }; + + onEmitClick = () => { + this.props.onEmit({ + name: this.props.name, + payload: this.state.payload, + }); + }; + + onToggleEditClick = () => { + this.setState(({ isTextAreaShowed }) => ({ + isTextAreaShowed: !isTextAreaShowed, + })); + }; + + render() { + const { failed, isTextAreaShowed } = this.state; + const extraStyle = { + display: isTextAreaShowed ? 'block' : 'none', + }; + + if (failed) { + extraStyle.border = '1px solid #fadddd'; + extraStyle.backgroundColor = '#fff5f5'; + } + + return ( +
+

{this.props.title}

+
+ + +
+ +