Skip to content

Commit

Permalink
feat(storybook): more changes for controls
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina committed Jun 22, 2021
1 parent 4c2d4fe commit 9446ba1
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -79,6 +79,7 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "7.1.1",
"@schematics/angular": "^12.0.0",
"@storybook/addon-controls": "^6.2.9",
"@storybook/addon-knobs": "^6.2.7",
"@storybook/angular": "^6.2.7",
"@storybook/core": "^6.0.21",
Expand Down
@@ -1,7 +1,7 @@
import type { Tree } from '@nrwl/devkit';
import { generateFiles, joinPathFragments } from '@nrwl/devkit';
import { getComponentProps } from '../utils/storybook';
import { getKnobDefaultValue } from './lib/get-knob-default-value';
import { getControlDefaultValue } from './lib/get-control-default-value';
import type { ComponentStoryGeneratorOptions } from './schema';

export function componentStoryGenerator(
Expand All @@ -25,7 +25,7 @@ export function componentStoryGenerator(
const props = getComponentProps(
tree,
joinPathFragments(destinationDir, `${componentFileName}.ts`),
getKnobDefaultValue
getControlDefaultValue
);

generateFiles(tree, templatesDir, destinationDir, {
Expand Down
@@ -1,16 +1,23 @@
<% if(props.length > 0) { %>import { text, number, boolean } from '@storybook/addon-knobs';<% } %>
import { moduleMetadata, Story, Meta } from '@storybook/angular';
import { <%=componentName%> } from './<%=componentFileName%>';

export default {
title: '<%=componentName%>',
component: <%=componentName%>
}
component: <%=componentName%>,
decorators: [
moduleMetadata({
imports: [],
})
],
} as Meta;

export const primary = () => ({
moduleMetadata: {
imports: []
},
props: {<% for (let prop of props) { %>
<%=prop.name%>: <%=prop.type%>('<%=prop.name%>', <%-prop.defaultValue%>),<% } %>
}
})
const Template: Story<<%=componentName%>> = (args: <%=componentName%>) => ({
component: <%=componentName%>,
props: args,
});


export const primary = Template.bind({});
primary.args = {<% for (let prop of props) { %>
<%= prop.name %>: <%- prop.defaultValue %>,<% } %>
}
@@ -1,6 +1,6 @@
import type { PropertyDeclaration } from 'typescript';

export function getKnobDefaultValue(property: PropertyDeclaration): string {
export function getControlDefaultValue(property: PropertyDeclaration): string {
const typeNameToDefault = {
string: "''",
number: '0',
Expand Down
6 changes: 4 additions & 2 deletions packages/angular/src/generators/utils/storybook.ts
Expand Up @@ -36,7 +36,9 @@ export function getInputPropertyDeclarations(
export function getComponentProps(
tree: Tree,
componentPath: string,
getKnobDefaultValueFn: (property: PropertyDeclaration) => string | undefined
getControlDefaultValueFn: (
property: PropertyDeclaration
) => string | undefined
): InputDescriptor[] {
const props = getInputPropertyDeclarations(tree, componentPath).map(
(node) => {
Expand All @@ -49,7 +51,7 @@ export function getComponentProps(
: node.name.getText();

const type = getKnobType(node);
const defaultValue = getKnobDefaultValueFn(node);
const defaultValue = getControlDefaultValueFn(node);

return {
name,
Expand Down
@@ -1,12 +1,12 @@
import React from 'react';
import { Story, Meta } from '@storybook/react';
import { <%= componentName %><% if ( propsTypeName && !isPlainJs ) { %>, <%= propsTypeName %> <% } %> } from './<%= componentFileName %>';

export default {
component: <%= componentName %>,
title: '<%= componentName %>'
};
} as Meta;

const Template<%= isPlainJs ? '': ': Story<' + propsTypeName %> + '>' = (args) => <<%= componentName %> {...args} />;
const Template: Story<<%= propsTypeName %>> = (args) => <<%= componentName %> {...args} />;

export const primary = Template.bind({})
primary.args = {<% for (let prop of props) { %>
Expand Down
@@ -1,4 +0,0 @@
import { addDecorator } from '<%= uiFramework %>';
import { withKnobs } from '@storybook/addon-knobs';

addDecorator(withKnobs);
@@ -1,4 +1,4 @@
module.exports = {
stories: [],
addons: ['@storybook/addon-knobs/register'],
addons: ['@storybook/addon-controls'],
};
6 changes: 3 additions & 3 deletions packages/storybook/src/generators/init/init.ts
Expand Up @@ -35,10 +35,10 @@ function checkDependenciesInstalled(host: Tree, schema: Schema) {
*/

if (
!packageJson.dependencies['@storybook/addon-knobs'] &&
!packageJson.devDependencies['@storybook/addon-knobs']
!packageJson.dependencies['@storybook/addon-controls'] &&
!packageJson.devDependencies['@storybook/addon-controls']
) {
devDependencies['@storybook/addon-knobs'] = storybookVersion;
devDependencies['@storybook/addon-controls'] = storybookVersion;
}

if (isFramework('angular', schema)) {
Expand Down
1 change: 1 addition & 0 deletions scripts/depcheck/missing.ts
Expand Up @@ -63,6 +63,7 @@ const IGNORE_MATCHES = {
'@angular-devkit/core',
'@angular-devkit/schematics',
'@storybook/addon-knobs',
'@storybook/addon-controls',
'@storybook/core',
'rxjs',
],
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Expand Up @@ -3431,6 +3431,20 @@
dependencies:
"@sinonjs/commons" "^1.7.0"

"@storybook/addon-controls@^6.2.9":
version "6.2.9"
resolved "https://registry.yarnpkg.com/@storybook/addon-controls/-/addon-controls-6.2.9.tgz#eeec14b2946f1fb5326115f2205ed72c7f44ccea"
integrity sha512-NvXAJ7I5U4CLxv4wL3/Ne9rehJlgnSmQlLIG/z6dg5zm7JIb48LT4IY6GzjlUP5LkjmO9KJ8gJC249uRt2iPBQ==
dependencies:
"@storybook/addons" "6.2.9"
"@storybook/api" "6.2.9"
"@storybook/client-api" "6.2.9"
"@storybook/components" "6.2.9"
"@storybook/node-logger" "6.2.9"
"@storybook/theming" "6.2.9"
core-js "^3.8.2"
ts-dedent "^2.0.0"

"@storybook/addon-knobs@^6.2.7":
version "6.2.9"
resolved "https://registry.yarnpkg.com/@storybook/addon-knobs/-/addon-knobs-6.2.9.tgz#879b2e3861c29b30b53f1d624315a2f3a463785a"
Expand Down

0 comments on commit 9446ba1

Please sign in to comment.