Skip to content

Commit

Permalink
Merge pull request #7 from kadirahq/use-add-decorator
Browse files Browse the repository at this point in the history
Use add decorator
  • Loading branch information
roonyh committed Aug 30, 2016
2 parents b38dd41 + 36868f5 commit 7583cfd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/components/Wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react';
export default class Wrap extends React.Component {
constructor(props) {
super(props);
this.store = this.props.store;
this._initialPropsReceived = this.initialPropsReceived.bind(this);
this._knobChanged = this.knobChanged.bind(this);
this._resetKnobs = this.resetKnobs.bind(this);
Expand All @@ -12,7 +11,7 @@ export default class Wrap extends React.Component {
this.props.channel.emit('addon:knobs:helloFromStory');
};
this.setPanelFields = () => {
this.props.channel.emit('addon:knobs:setFields', this.store);
this.props.channel.emit('addon:knobs:setFields', this.props.store);
};
}

Expand Down Expand Up @@ -42,7 +41,7 @@ export default class Wrap extends React.Component {

knobChanged(change) {
const { name, value } = change;
const { type } = this.store[name];
const { type } = this.props.store[name];

let formatedValue = value;
if (type === 'object') {
Expand All @@ -53,13 +52,13 @@ export default class Wrap extends React.Component {
}
}

this.store[name].value = formatedValue;
this.props.store[name].value = formatedValue;
this.forceUpdate();
}

resetKnobs() {
Object.keys(this.store).forEach(field => {
delete(this.store[field]);
Object.keys(this.props.store).forEach(field => {
delete(this.props.store[field]);
});
this.forceUpdate();
this.setPanelFields();
Expand Down
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function register() {
}

let knobStore = {};
const stories = {};

function createKnob(name, value, type) {
if (knobStore[name]) {
Expand All @@ -30,12 +31,20 @@ function createKnob(name, value, type) {

function wrap(storyFn) {
const channel = addons.getChannel();
const localKnobStore = {};

return context => {
if (!stories[context.kind]) {
stories[context.kind] = {};
}

if (!stories[context.kind][context.story]) {
stories[context.kind][context.story] = {};
}

// Change the global knobStore to the one local to this story
knobStore = localKnobStore;
return <Wrap {...{ context, storyFn, channel, store: localKnobStore }} />;
knobStore = stories[context.kind][context.story];

return <Wrap {...{ context, storyFn, channel, store: knobStore }} />;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/stories/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const buttonStyles = {

const Button = ({ onClick, style, color, width, children, disabled }) => (
<button
style={{ ...buttonStyles, ...style, ...{ backgroundColor: color, width: `${width}px` } }}
style={{ ...buttonStyles, ...{ backgroundColor: color, width: `${width}px` }, ...style }}
onClick={onClick}
disabled={disabled}
>
Expand Down
9 changes: 5 additions & 4 deletions src/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { createKnob, wrap } from '../index';
import Button from './Button';

storiesOf('Button', module)
.add('default view', wrap(() => (
.addDecorator((story, context) => (wrap(story)(context)))
.add('default view', () => (
<Button
onClick={ action('button clicked') }
style={createKnob('style', { width: '70px' }, 'object')}
>
{createKnob('children', 'Hello')}
</Button>
)))
.add('default view with different knobs', wrap(() => (
))
.add('default view with different knobs', () => (
<Button
onClick={ action('button clicked') }
color={createKnob('color', '#abc')}
Expand All @@ -22,7 +23,7 @@ storiesOf('Button', module)
>
{createKnob('text', 'Hello')}
</Button>
)))
))
.add('Story without any knobs', () => (
<Button>Hello</Button>
));

0 comments on commit 7583cfd

Please sign in to comment.