-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Knobs addon: new knob type button
#2004
Changes from 10 commits
f2e8d14
2576d3a
0c12449
e54076b
d75fdbd
2eeece0
97a6ee1
c34c4a0
0297552
377e812
ad97eb0
8ae2b76
58e0860
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
const styles = { | ||
height: '26px', | ||
}; | ||
|
||
class ButtonType extends React.Component { | ||
render() { | ||
const { knob, onClick } = this.props; | ||
return ( | ||
<button | ||
type="button" | ||
id={knob.name} | ||
ref={c => { | ||
this.input = c; | ||
}} | ||
style={styles} | ||
onClick={() => onClick(knob)} | ||
> | ||
{knob.name} | ||
</button> | ||
); | ||
} | ||
} | ||
|
||
ButtonType.defaultProps = { | ||
knob: {}, | ||
}; | ||
|
||
ButtonType.propTypes = { | ||
knob: PropTypes.shape({ | ||
name: PropTypes.string, | ||
}), | ||
onClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
ButtonType.serialize = value => value; | ||
ButtonType.deserialize = value => value; | ||
|
||
export default ButtonType; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import EventEmiter from 'eventemitter3'; | ||
|
||
import { storiesOf } from '@storybook/react'; | ||
|
@@ -16,6 +17,7 @@ import { | |
select, | ||
array, | ||
date, | ||
button, | ||
object, | ||
} from '@storybook/addon-knobs'; | ||
import centered from '@storybook/addon-centered'; | ||
|
@@ -59,6 +61,23 @@ const InfoButton = () => ( | |
</span> | ||
); | ||
|
||
class AsyncItemLoader extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { items: [] }; | ||
} | ||
|
||
loadItems() { | ||
setTimeout(() => this.setState({ items: ['pencil', 'pen', 'eraser'] }), 1500); | ||
} | ||
|
||
render() { | ||
button('Load the items', () => this.loadItems()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be in the constructor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very clear example by the way, Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HMR kills it when it is in the constructor. The button stays present in the knob panel but the instance of the example component is new, so the actual component tied to the knob was unmounted. I think there'd need to be modifications to the knobManager to override existing knobs of this type, instead of just returning the existing instance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, if this is the best solution, that's fine. Can you take a look at why the CI is disapproving of this PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I restarted the build tasks |
||
return this.props.children(this.state.items); | ||
} | ||
} | ||
AsyncItemLoader.propTypes = { children: PropTypes.func.isRequired }; | ||
|
||
storiesOf('Button', module) | ||
.addDecorator(withKnobs) | ||
.add('with text', () => ( | ||
|
@@ -118,6 +137,14 @@ storiesOf('Button', module) | |
<p>In my backpack, I have:</p> | ||
<ul>{items.map(item => <li key={item}>{item}</li>)}</ul> | ||
<p>{salutation}</p> | ||
<hr /> | ||
<p>PS. My shirt pocket contains: </p> | ||
<AsyncItemLoader> | ||
{loadedItems => { | ||
if (!loadedItems.length) return <li>No items!</li>; | ||
return <ul>{loadedItems.map(i => <li key={i}>{i}</li>)}</ul>; | ||
}} | ||
</AsyncItemLoader> | ||
</div> | ||
); | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way this is used, is quite different from how the other knobs are used. This confuses me, and I'd assume other as well. Having a fully 'working' real-world example would help a lot understanding this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the documentation as of today, I don't get nothin' 😆. How is this supposed to be used ? 🤔