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

Commit 4d8556c

Browse files
committed
Added dependency storybook-addon-actions, so this addon would also work with react-native-storybook.
Added checking if props exist, because if they are undefined plugin crashes. Returns clone of element instead of already wrapping element into withKnobs, since we still need to do withKnobs manualy and setting withKnobs before screws up usage with other addons (for instance storybook-addon-usage) If there is no knob on prop, it will still use the prop, instead of returning undefined.
1 parent 7db808d commit 4d8556c

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

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
};

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)