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
Addon-actions: Add Storybook Args support #10029
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2be2db6
Added basic global args mechanism to story_store
tmeasday 1d04267
Re-render when global args change
tmeasday 7673dbc
Add global args module
tmeasday 8e49d0f
Add `useGlobalArgs` hook for addons
tmeasday 3a10267
Whoops, correct path sep
tmeasday 585d026
Added tests for `globalArgs` module
tmeasday c1d8a8e
Added test for preview-side useGlobalArgs
tmeasday 04792c5
Args: Fix circular package dependency
shilman 6828f7c
Fix useGlobalArgs typing
shilman 192eea5
Export combineParameters and types from client-api
shilman 4cd2414
Addon-actions: Add args and argTypesRegex parameter enhancers
shilman c9882e1
Merge branch 'feature/global-args' into feature/action-args
shilman 5192264
Clarify load order
shilman 2b3f137
Merge branch 'next' into feature/action-args
shilman b9841ca
Minor cleanup
shilman 0858d97
Merge branch 'next' into feature/action-args
shilman a431c91
Merge branch 'next' into feature/action-args
shilman e06b2e7
Addon-actions: Use argType.action instead of action.args
shilman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/preset'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { StoryContext } from '@storybook/addons'; | ||
import { inferActionsFromArgTypes, addActionsFromArgs } from './addArgs'; | ||
|
||
describe('actions parameter enhancers', () => { | ||
describe('argTypes option', () => { | ||
const baseParameters = { | ||
argTypes: { onClick: {}, onFocus: {}, somethingElse: {} }, | ||
actions: { argTypesRegex: '^on.*' }, | ||
}; | ||
|
||
it('should add actions that match a pattern', () => { | ||
const parameters = baseParameters; | ||
const { args } = inferActionsFromArgTypes({ parameters } as StoryContext); | ||
expect(Object.keys(args)).toEqual(['onClick', 'onFocus']); | ||
}); | ||
|
||
it('should prioritize pre-existing args', () => { | ||
const parameters = { | ||
...baseParameters, | ||
args: { onClick: 'pre-existing arg' }, | ||
}; | ||
const { args } = inferActionsFromArgTypes({ parameters } as StoryContext); | ||
expect(Object.keys(args)).toEqual(['onClick', 'onFocus']); | ||
expect(args.onClick).toEqual('pre-existing arg'); | ||
}); | ||
|
||
it('should do nothing if actions are disabled', () => { | ||
const parameters = { | ||
...baseParameters, | ||
actions: { ...baseParameters.actions, disable: true }, | ||
}; | ||
const result = inferActionsFromArgTypes({ parameters } as StoryContext); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('args option', () => { | ||
const baseParameters = { | ||
actions: { args: ['onClick', 'onBlur'] }, | ||
}; | ||
|
||
it('should add actions based on action.args', () => { | ||
const parameters = baseParameters; | ||
const { args } = addActionsFromArgs({ parameters } as StoryContext); | ||
expect(Object.keys(args)).toEqual(['onClick', 'onBlur']); | ||
}); | ||
|
||
it('should prioritize pre-existing args', () => { | ||
const parameters = { | ||
...baseParameters, | ||
args: { onClick: 'pre-existing arg' }, | ||
}; | ||
const { args } = addActionsFromArgs({ parameters } as StoryContext); | ||
expect(Object.keys(args)).toEqual(['onClick', 'onBlur']); | ||
expect(args.onClick).toEqual('pre-existing arg'); | ||
}); | ||
|
||
it('should do nothing if actions are disabled', () => { | ||
const parameters = { actions: { ...baseParameters.actions, disable: true } }; | ||
const result = addActionsFromArgs({ parameters } as StoryContext); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ParameterEnhancer, combineParameters } from '@storybook/client-api'; | ||
import { Args } from '@storybook/addons'; | ||
|
||
import { action } from '../index'; | ||
|
||
// interface ActionsParameter { | ||
// disable?: boolean; | ||
// args?: string[]; | ||
// argTypesRegex?: RegExp; | ||
// } | ||
|
||
/** | ||
* Automatically add action args for argTypes whose name | ||
* matches a regex, such as `^on.*` for react-style `onClick` etc. | ||
*/ | ||
export const inferActionsFromArgTypes: ParameterEnhancer = context => { | ||
const { args, actions, argTypes } = context.parameters; | ||
if (!actions || actions.disable || !actions.argTypesRegex || !argTypes) { | ||
shilman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
|
||
const argTypesRegex = new RegExp(actions.argTypesRegex); | ||
const actionArgs = Object.keys(argTypes).reduce((acc, name) => { | ||
if (argTypesRegex.test(name)) { | ||
acc[name] = action(name); | ||
} | ||
return acc; | ||
}, {} as Args); | ||
|
||
return { | ||
args: combineParameters(actionArgs, args), | ||
tmeasday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
}; | ||
|
||
/** | ||
* Add action args for list of strings. | ||
*/ | ||
export const addActionsFromArgs: ParameterEnhancer = context => { | ||
const { args, actions } = context.parameters; | ||
if (!actions || actions.disable || !actions.args) { | ||
return null; | ||
} | ||
|
||
const actionArgs = (actions.args as string[]).reduce((acc, name) => { | ||
acc[name] = action(name); | ||
return acc; | ||
}, {} as Args); | ||
|
||
return { | ||
args: combineParameters(actionArgs, args), | ||
}; | ||
}; | ||
|
||
export const parameterEnhancers = [addActionsFromArgs, inferActionsFromArgTypes]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function managerEntries(entry: any[] = [], options: any) { | ||
return [...entry, require.resolve('../../register')]; | ||
} | ||
|
||
export function config(entry: any[] = []) { | ||
return [...entry, require.resolve('./addArgs')]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe add an example of how you'd provide an argType to the story? Or is that sort of pointless?
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.
Waiting on this until the argTypes design is finalized