-
-
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 3 commits
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,40 @@ | ||
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, | ||
}); | ||
}; | ||
|
||
return (...args) => { | ||
// Used without options as .addDecorator(decorator) | ||
if (typeof args[0] === 'function') { | ||
return decorator()(...args); | ||
} | ||
|
||
return (...innerArgs) => { | ||
// Used as [.]addDecorator(decorator(options)) | ||
if (typeof innerArgs.length > 1) { | ||
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. typeof? 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. wow :0 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 need to add a test for this modality, it just doesn't really make sense for |
||
return decorator(...args)(...innerArgs); | ||
} | ||
|
||
// Used to wrap a story directly .add('story', decorator(options)(() => <Story />)) | ||
// This is now deprecated: | ||
return deprecate( | ||
context => decorator(...args)(innerArgs[0], 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 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