-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Refactoring of withX wrappers withNotes and withInfo #1538
Changes from 2 commits
a50a8e8
1810a98
ba2793e
ee6204e
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 |
---|---|---|
@@ -1,14 +1,8 @@ | ||
import React from 'react'; | ||
import deprecate from 'util-deprecate'; | ||
import _Story from './components/Story'; | ||
import Story from './components/Story'; | ||
import { H1, H2, H3, H4, H5, H6, Code, P, UL, A, LI } from './components/markdown'; | ||
|
||
function addonCompose(addonFn) { | ||
return storyFn => context => addonFn(storyFn, context); | ||
} | ||
|
||
export const Story = _Story; | ||
|
||
const defaultOptions = { | ||
inline: false, | ||
header: true, | ||
|
@@ -34,20 +28,10 @@ const defaultMarksyConf = { | |
ul: UL, | ||
}; | ||
|
||
export function addInfo(storyFn, context, info, _options) { | ||
if (typeof storyFn !== 'function') { | ||
if (typeof info === 'function') { | ||
_options = storyFn; // eslint-disable-line | ||
storyFn = info; // eslint-disable-line | ||
info = ''; // eslint-disable-line | ||
} else { | ||
throw new Error('No story defining function has been specified'); | ||
} | ||
} | ||
|
||
function addInfo(storyFn, context, infoOptions) { | ||
const options = { | ||
...defaultOptions, | ||
..._options, | ||
...infoOptions, | ||
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. Also merge in: if (!options.propTables) {
options.propTables = null;
} 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. how would you merge this in? see the comment above in the code. |
||
}; | ||
|
||
// props.propTables can only be either an array of components or null | ||
|
@@ -62,7 +46,7 @@ export function addInfo(storyFn, context, info, _options) { | |
Object.assign(marksyConf, options.marksyConf); | ||
} | ||
const props = { | ||
info, | ||
info: options.text, | ||
context, | ||
showInline: Boolean(options.inline), | ||
showHeader: Boolean(options.header), | ||
|
@@ -83,12 +67,25 @@ export function addInfo(storyFn, context, info, _options) { | |
); | ||
} | ||
|
||
export const withInfo = (info, _options) => | ||
addonCompose((storyFn, context) => addInfo(storyFn, context, info, _options)); | ||
export const withInfo = textOrOptions => { | ||
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. IMO, it would be nice to provide here such an option as well: withInfo('text info', { options }) There are two reasons for that: 1.1. We might want to use different .add('Button', withInfo('Default Button', widgetInfoOptions)(() => <Button />)) 1.2. We might want to use multiline template string for
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. @usulpro the reason @tmeasday wanted to keep it to a single argument is for this API: https://gist.github.com/shilman/792dc25550daa9c2bf37238f4ef7a398#options-types The API described in the gist is obviously just a proposal, but we think having a single argument makes stuff like this simple to implement. If we have two arguments it gets complicated fast. |
||
const options = typeof textOrOptions === 'string' ? { text: textOrOptions } : textOrOptions; | ||
return storyFn => context => addInfo(storyFn, context, options); | ||
}; | ||
|
||
export { Story }; | ||
|
||
export default { | ||
addWithInfo: deprecate(function addWithInfo(storyName, info, storyFn, _options) { | ||
return this.add(storyName, withInfo(info, _options)(storyFn)); | ||
addWithInfo: deprecate(function addWithInfo(storyName, text, storyFn, options) { | ||
if (typeof storyFn !== 'function') { | ||
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. Could use a bit of refactoring, I'd prefer using abstract names or simply an argument array to do detection, and only assign to a const when we are sure what it is. |
||
if (typeof text === 'function') { | ||
options = storyFn; // eslint-disable-line | ||
storyFn = text; // eslint-disable-line | ||
text = ''; // eslint-disable-line | ||
} else { | ||
throw new Error('No story defining function has been specified'); | ||
} | ||
} | ||
return this.add(storyName, withInfo({ text, ...options })(storyFn)); | ||
}, '@storybook/addon-info .addWithInfo() addon is deprecated, use withInfo() from the same package instead. \nSee https://github.com/storybooks/storybook/tree/master/addons/info'), | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ import deprecate from 'util-deprecate'; | |
import addons from '@storybook/addons'; | ||
import { WithNotes as ReactWithNotes } from './react'; | ||
|
||
export const withNotes = ({ notes }) => { | ||
export const withNotes = textOrOptions => { | ||
const channel = addons.getChannel(); | ||
const options = typeof textOrOptions === 'string' ? { text: textOrOptions } : textOrOptions; | ||
|
||
return getStory => context => { | ||
// send the notes to the channel before the story is rendered | ||
channel.emit('storybook/notes/add_notes', notes); | ||
channel.emit('storybook/notes/add_notes', options.text); | ||
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. If we mean that this addon could have other options in the future (or even if we want to keep it as an addon example) why don't pass all 'options' object here? 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 think we can emit those options once we handle them on the other end. There may be "preview-side" options, so I don't think it's necessarily the case that we'd want to emit them all. |
||
return getStory(context); | ||
}; | ||
}; | ||
|
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.
Is there any reason to not export this function to users? Maybe our documentation shouldn't encourage people to use it, but there could be some "advanced" use cases when this form is more convenient! Especially it makes sense after #1501
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.
IMHO, we should never export a function to users if we don't need to, because then we are stuck supporting it unless we want to break the API.