Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10029 from storybookjs/feature/action-args
Addon-actions: Add Storybook Args support
- Loading branch information
Showing
9 changed files
with
228 additions
and
39 deletions.
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,67 @@ | ||
import { StoryContext } from '@storybook/addons'; | ||
import { inferActionsFromArgTypesRegex, addActionsFromArgTypes } from './addArgs'; | ||
|
||
describe('actions parameter enhancers', () => { | ||
describe('actions.argTypesRegex parameter', () => { | ||
const baseParameters = { | ||
argTypes: { onClick: {}, onFocus: {}, somethingElse: {} }, | ||
actions: { argTypesRegex: '^on.*' }, | ||
}; | ||
|
||
it('should add actions that match a pattern', () => { | ||
const parameters = baseParameters; | ||
const { args } = inferActionsFromArgTypesRegex({ 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 } = inferActionsFromArgTypesRegex({ 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 = inferActionsFromArgTypesRegex({ parameters } as StoryContext); | ||
expect(result).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('argTypes.action parameter', () => { | ||
const baseParameters = { | ||
argTypes: { | ||
onClick: { action: 'clicked!' }, | ||
onBlur: { action: 'blurred!' }, | ||
}, | ||
}; | ||
|
||
it('should add actions based on action.args', () => { | ||
const parameters = baseParameters; | ||
const { args } = addActionsFromArgTypes({ 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 } = addActionsFromArgTypes({ 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 = { ...baseParameters, actions: { disable: true } }; | ||
const result = addActionsFromArgTypes({ 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,57 @@ | ||
import { ParameterEnhancer, combineParameters } from '@storybook/client-api'; | ||
import { Args, ArgType } from '@storybook/addons'; | ||
|
||
import { action } from '../index'; | ||
|
||
// interface ActionsParameter { | ||
// disable?: boolean; | ||
// argTypesRegex?: RegExp; | ||
// } | ||
|
||
/** | ||
* Automatically add action args for argTypes whose name | ||
* matches a regex, such as `^on.*` for react-style `onClick` etc. | ||
*/ | ||
export const inferActionsFromArgTypesRegex: ParameterEnhancer = context => { | ||
const { args, actions, argTypes } = context.parameters; | ||
if (!actions || actions.disable || !actions.argTypesRegex || !argTypes) { | ||
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), | ||
}; | ||
}; | ||
|
||
/** | ||
* Add action args for list of strings. | ||
*/ | ||
export const addActionsFromArgTypes: ParameterEnhancer = context => { | ||
const { args, argTypes, actions } = context.parameters; | ||
if (actions?.disable || !argTypes) { | ||
return null; | ||
} | ||
|
||
const actionArgs = Object.keys(argTypes).reduce((acc, argName) => { | ||
const argType: ArgType = argTypes[argName]; | ||
if (argType.action) { | ||
const message = typeof argType.action === 'string' ? argType.action : argName; | ||
acc[argName] = action(message); | ||
} | ||
return acc; | ||
}, {} as Args); | ||
|
||
return { | ||
args: combineParameters(actionArgs, args), | ||
}; | ||
}; | ||
|
||
export const parameterEnhancers = [addActionsFromArgTypes, inferActionsFromArgTypesRegex]; |
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