-
-
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
Refactor transitional decorator from addon-notes #3559
Changes from 1 commit
98a99b5
e7f7f32
15c2ed8
b8f8acb
199098c
de1c65c
5d86249
a2e57cf
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,34 @@ | ||
import deprecate from 'util-deprecate'; | ||
|
||
// Create a decorator that can be used both in the (deprecated) old "hoc" style: | ||
// .add('story', decorator(options)(() => <Story />)); | ||
// | ||
// And in the new, "parameterized" style: | ||
// .addDecorator(decorator) | ||
// .add('story', () => <Story />, { name: { parameters } }); | ||
|
||
export const makeTransitionalDecorator = ({ name, parameterName, wrapper }) => { | ||
const decorator = options => (getStory, context) => { | ||
const parameters = context.parameters && context.parameters[parameterName]; | ||
|
||
return wrapper(getStory, context, { | ||
options, | ||
parameters, | ||
}); | ||
}; | ||
|
||
const hoc = deprecate( | ||
options => story => context => decorator(options)(story, context), | ||
`Passing stories directly into ${name}() is deprecated, instead use addDecorator(${name}) and pass options with the '${parameterName}' parameter` | ||
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. Are we sure that we want to deprecate the old way? Honestly, it looks a lot more explicit to me 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. Oh, I thought we did? I think the strongest argument in favour is if you want to pass parameters to more than one decorator for the same story: .add('story', withNotes('foo')(backgrounds(['red', 'white'])(() => <Story />)))
vs
.add('story', () => <Story />, { notes: 'foo', backgrounds: ['red', 'white'] }) I was planning on changing all the documentation to not mention the old way at least, so deprecating made sense to me, but I'm open to whatever. 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 could afford supporting both @ndelangen WDYT 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. What is a benefit of having the old API supported? 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. You may want to supply the same parameters (e.g. backgrounds) to multiple stories And even extract it into variable or module, like 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.
That's perfect I think. 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. @tmeasday I just realised that those are two different usecases
Actually addons that support both do some checks to figure out which usage is it. Can we maybe just deprecate the latter but keep the former, because it's quite useful in cases like backgrounds? 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. Oh, now I see that the latter is the thing we're actually deprecating here, good 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. But the former would probably need to be supported when migrating other addons 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, I see what you are saying here. Let me take another pass at this. |
||
); | ||
|
||
return (...args) => { | ||
// Used without options as .addDecorator(decorator) | ||
if (typeof args[0] === 'function') { | ||
return decorator()(...args); | ||
} | ||
|
||
// Input are options, ala .add('name', decorator('note')(() => <Story/>)) | ||
return hoc(args[0]); | ||
}; | ||
}; |
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.
Should we rename it to something like
makePolymorphicDecorator
?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.
Sure! Or just
makeDecorator()
?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.
just
makeDecorator
sounds goodThere 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.
I like the makeDecorator name too