Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 731b94b

Browse files
Merge pull request #8 from Gongreg/master
Making work with react-native-storybook. Adding some features
2 parents 7db808d + 744b336 commit 731b94b

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

dist/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
99

1010
var _react = require('react');
1111

12-
var _storybook = require('@kadira/storybook');
12+
var _storybookAddonActions = require('@kadira/storybook-addon-actions');
1313

1414
var _storybookAddonKnobs = require('@kadira/storybook-addon-knobs');
1515

@@ -44,7 +44,7 @@ var propTypeKnobResolver = exports.propTypeKnobResolver = function propTypeKnobR
4444
/* eslint-disable no-multi-spaces */
4545
// Default simple PropType based knob map.
4646
var propTypeKnobsMap = [{ name: 'string', knob: _storybookAddonKnobs.text }, { name: 'number', knob: _storybookAddonKnobs.number }, { name: 'bool', knob: _storybookAddonKnobs.boolean }, { name: 'func', knob: function knob(name, value) {
47-
return value || (0, _storybook.action)(name);
47+
return value || (0, _storybookAddonActions.action)(name);
4848
} }, { name: 'object', knob: _storybookAddonKnobs.object }, { name: 'node', knob: _storybookAddonKnobs.text }, { name: 'element', knob: _storybookAddonKnobs.text }];
4949

5050
propTypeKnobsMap.forEach(function (_ref2, weight) {
@@ -90,19 +90,17 @@ var withSmartKnobs = exports.withSmartKnobs = function withSmartKnobs(story, con
9090

9191
var defaultProps = _extends({}, component.type.defaultProps || {}, component.props);
9292

93-
var finalProps = Object.keys(props).reduce(function (acc, n) {
93+
var finalProps = props ? Object.keys(props).reduce(function (acc, n) {
9494
var item = props[n];
9595
if (!item.type) {
9696
console.warn('There is a prop with defaultValue ' + item.defaultValue.value + ' but it wasnt specified on element.propTypes. Check story: "' + context.kind + '".');
9797
return acc;
9898
}
9999

100100
return _extends({}, acc, _defineProperty({}, n, item));
101-
}, {});
101+
}, {}) : {};
102102

103-
return (0, _storybookAddonKnobs.withKnobs)(function () {
104-
return (0, _react.cloneElement)(component, resolvePropValues(finalProps, defaultProps));
105-
}, context);
103+
return (0, _react.cloneElement)(component, resolvePropValues(finalProps, defaultProps));
106104
};
107105

108106
var resolvePropValues = function resolvePropValues(propTypes, defaultProps) {
@@ -118,6 +116,6 @@ var resolvePropValues = function resolvePropValues(propTypes, defaultProps) {
118116
return value !== undefined ? value : resolver(propName, propTypes[propName], defaultProps[propName], propTypes, defaultProps);
119117
}, undefined);
120118
}).reduce(function (props, value, i) {
121-
return _extends({}, props, _defineProperty({}, propNames[i], value));
119+
return _extends({}, props, _defineProperty({}, propNames[i], value !== undefined ? value : defaultProps[propNames[i]]));
122120
}, defaultProps);
123121
};

example/stories/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
import React from 'react'
22
import { storiesOf } from '@kadira/storybook'
33
import { withSmartKnobs } from '../../src'
4+
import { withKnobs, select } from '@kadira/storybook-addon-knobs';
45

56
import SmartKnobedComponent from './SmartKnobedComponent';
67
import SmartKnobedComponentMissingProps from './SmartKnobedComponentMissingProps';
78

9+
const stub = fn => fn();
10+
811
storiesOf('Example of smart Knobs', module)
912
.addDecorator(withSmartKnobs)
13+
.addDecorator(withKnobs)
1014
.add('full example', () => (
1115
<SmartKnobedComponent />
1216
))
1317

1418
storiesOf('Smart Knobs missing props', module)
1519
.addDecorator(withSmartKnobs)
20+
.addDecorator(withKnobs)
1621
.add('example', () => (
1722
<SmartKnobedComponentMissingProps foo="baz" />
1823
))
24+
25+
storiesOf('Smart Knobs with manual knobs', module)
26+
.addDecorator(stub)
27+
.addDecorator(withSmartKnobs)
28+
.addDecorator(withKnobs)
29+
.add('example', () => (
30+
<SmartKnobedComponent string={ select('string', ['1', '2', '3'], '2') }/>
31+
))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"author": "",
1414
"license": "MIT",
1515
"peerDependencies": {
16+
"@kadira/storybook-addon-actions": "^1.1.3",
1617
"@kadira/storybook-addon-knobs": "^1.3.0",
1718
"react": "^0.14.7 || ^15.0.0"
1819
},

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cloneElement } from 'react'
2-
import { action } from '@kadira/storybook'
2+
import { action } from '@kadira/storybook-addon-actions'
33
import { withKnobs, text, boolean, number, object, select } from '@kadira/storybook-addon-knobs'
44

55
const knobResolvers = {}
@@ -62,7 +62,7 @@ export const withSmartKnobs = (story, context) => {
6262
...component.props
6363
}
6464

65-
const finalProps = Object.keys(props).reduce((acc, n) => {
65+
const finalProps = props ? Object.keys(props).reduce((acc, n) => {
6666
const item = props[n];
6767
if (!item.type) {
6868
console.warn(`There is a prop with defaultValue ${item.defaultValue.value} but it wasnt specified on element.propTypes. Check story: "${context.kind}".`);
@@ -73,9 +73,9 @@ export const withSmartKnobs = (story, context) => {
7373
...acc,
7474
[n]: item,
7575
};
76-
}, {});
76+
}, {}) : {};
7777

78-
return withKnobs(() => cloneElement(component, resolvePropValues(finalProps, defaultProps)), context)
78+
return cloneElement(component, resolvePropValues(finalProps, defaultProps))
7979
}
8080

8181
const resolvePropValues = (propTypes, defaultProps) => {
@@ -92,6 +92,6 @@ const resolvePropValues = (propTypes, defaultProps) => {
9292
))
9393
.reduce((props, value, i) => ({
9494
...props,
95-
[propNames[i]]: value
95+
[propNames[i]]: value !== undefined ? value : defaultProps[propNames[i]]
9696
}), defaultProps)
9797
}

0 commit comments

Comments
 (0)