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

Commit 5f1777a

Browse files
committed
feat/ignore-props
1 parent 68c799f commit 5f1777a

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ Button.propTypes = {
2626
}
2727

2828
storiesOf('Button')
29-
.addDecorator(withSmartKnobs)
29+
.addDecorator(withSmartKnobs(options))
3030
.addDecorator(withKnobs)
3131
.add('simple', () => <Button>Smart knobed button</Button>)
3232

3333
```
3434

35+
## Options
36+
37+
- **ignoreProps**
38+
39+
Type: `Array`
40+
41+
If you want to ignore automatically props, you can list them. Example:
42+
```js
43+
.withSmartKnobs({ ignoreProps: ['number'] })
44+
.add('example' () => <div number={date('number', date)} />)
45+
```
46+
3547
## Configuration:
3648

3749
This plugin has a peer dependency on [babel-plugin-react-docgen

example/stories/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { storiesOf } from '@storybook/react'
33
import { withSmartKnobs } from '../../src'
4-
import { withKnobs, select } from '@storybook/addon-knobs'
4+
import { withKnobs, select, date } from '@storybook/addon-knobs'
55
import { withInfo } from '@storybook/addon-info'
66

77
import SmartKnobedComponent from './SmartKnobedComponent'
@@ -11,7 +11,7 @@ import SmartKnobedComponentWithFlow from './SmartKnobedComponentWithFlow'
1111
const stub = fn => fn()
1212

1313
storiesOf('Basic', module)
14-
.addDecorator(withSmartKnobs)
14+
.addDecorator(withSmartKnobs())
1515
.addDecorator(withKnobs)
1616
.add('proptypes', () => <SmartKnobedComponent />)
1717
.add('flow', () => <SmartKnobedComponentWithFlow />)
@@ -23,22 +23,27 @@ storiesOf('Basic', module)
2323
))
2424

2525
storiesOf('withInfo', module)
26-
.addDecorator(withSmartKnobs)
26+
.addDecorator(withSmartKnobs())
2727
.addDecorator(withKnobs)
2828
.addDecorator(withInfo)
2929
.add('proptypes', () => <SmartKnobedComponent />)
3030

3131
storiesOf('Missing props', module)
32-
.addDecorator(withSmartKnobs)
32+
.addDecorator(withSmartKnobs())
3333
.addDecorator(withKnobs)
3434
.add('example', () => (
3535
<SmartKnobedComponentMissingProps foo='baz' />
3636
))
3737

3838
storiesOf('Manual knobs', module)
3939
.addDecorator(stub)
40-
.addDecorator(withSmartKnobs)
40+
.addDecorator(withSmartKnobs())
4141
.addDecorator(withKnobs)
4242
.add('example', () => (
4343
<SmartKnobedComponent string={ select('string', ['1', '2', '3'], '2') } />
4444
))
45+
46+
storiesOf('Ignore Props', module)
47+
.addDecorator(withSmartKnobs({ ignoreProps: ['number'] }))
48+
.addDecorator(withKnobs)
49+
.add('proptypes', () => <SmartKnobedComponent number={ date('number', new Date()) } />)

src/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ addKnobResolver({
7070

7171
const ensureType = (item) => item.flowType ? ({ ...item, type: item.flowType }) : item
7272

73-
const getNewProps = (target, context) => {
73+
const getNewProps = (target, context, opts) => {
7474
const { __docgenInfo: { props } = { props: {} } } = target.type
75+
const { ignoreProps = [] } = opts
7576
const defaultProps = {
7677
...(target.type.defaultProps || {}),
7778
...target.props
@@ -80,6 +81,10 @@ const getNewProps = (target, context) => {
8081
const finalProps = props ? Object.keys(props).reduce((acc, n) => {
8182
const item = ensureType(props[n])
8283

84+
if (ignoreProps.includes(n)) {
85+
return acc
86+
}
87+
8388
if (!item.type) {
8489
const defaultValue = item.defaultValue ? item.defaultValue.value : 'Unknown'
8590
logger.warn(`There is a prop with defaultValue ${defaultValue} but it wasn't specified on element.propTypes. Check story: "${context.kind}".`)
@@ -95,30 +100,30 @@ const getNewProps = (target, context) => {
95100
return resolvePropValues(finalProps, defaultProps)
96101
}
97102

98-
const mutateChildren = (component) => {
103+
const mutateChildren = (component, context, opts) => {
99104
return cloneElement(component, { children: Children.map(component.props.children, (child) => {
100105
if (child.type.__docgenInfo) {
101-
const newProps = getNewProps(child)
106+
const newProps = getNewProps(child, context, opts)
102107

103108
return cloneElement(child, { ...child.props, ...newProps })
104109
}
105110

106111
if (child.props.children) {
107-
return mutateChildren(child)
112+
return mutateChildren(child, context, opts)
108113
}
109114

110115
return child
111116
}) })
112117
}
113118

114-
export const withSmartKnobs = (story, context) => {
119+
export const withSmartKnobs = (opts = {}) => (story, context) => {
115120
const component = story(context)
116121

117122
if (!component.type.__docgenInfo && component.props.children) {
118-
return mutateChildren(component)
123+
return mutateChildren(component, context, opts)
119124
}
120125

121-
const newProps = getNewProps(component, context)
126+
const newProps = getNewProps(component, context, opts)
122127

123128
return cloneElement(component, newProps)
124129
}

0 commit comments

Comments
 (0)