Skip to content

Commit

Permalink
feat(storybook): from addon knobs to controls
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina committed Jun 22, 2021
1 parent 122f7ec commit 4c2d4fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
15 changes: 2 additions & 13 deletions packages/react/src/generators/component-story/component-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ export interface CreateComponentStoriesFileSchema {
componentPath: string;
}

export type KnobType = 'text' | 'boolean' | 'number' | 'select';

// TODO: candidate to refactor with the angular component story
export function getKnobDefaultValue(property: ts.SyntaxKind): string {
export function getControlDefaultValue(property: ts.SyntaxKind): string {
const typeNameToDefault: Record<number, any> = {
[ts.SyntaxKind.StringKeyword]: "''",
[ts.SyntaxKind.NumberKeyword]: 0,
Expand Down Expand Up @@ -97,24 +95,16 @@ export function createComponentStoriesFile(
let propsTypeName: string = null;
let props: {
name: string;
type: KnobType;
defaultValue: any;
}[] = [];

if (propsInterface) {
propsTypeName = propsInterface.name.text;

props = propsInterface.members.map((member: ts.PropertySignature) => {
const initializerKindToKnobType: Record<number, KnobType> = {
[ts.SyntaxKind.StringKeyword]: 'text',
[ts.SyntaxKind.NumberKeyword]: 'number',
[ts.SyntaxKind.BooleanKeyword]: 'boolean',
};

return {
name: (member.name as ts.Identifier).text,
type: initializerKindToKnobType[member.type.kind],
defaultValue: getKnobDefaultValue(member.type.kind),
defaultValue: getControlDefaultValue(member.type.kind),
};
});
}
Expand All @@ -127,7 +117,6 @@ export function createComponentStoriesFile(
componentFileName: name,
propsTypeName,
props,
usedKnobs: props.map((x) => x.type).join(', '),
componentName: (cmpDeclaration as any).name.text,
isPlainJs,
fileExt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<% if(usedKnobs && usedKnobs.length > 0) { %>import { <%= usedKnobs %> } from '@storybook/addon-knobs';<% } %>
import React from 'react';
import { <%= componentName %><% if ( propsTypeName && !isPlainJs ) { %>, <%= propsTypeName %> <% } %> } from './<%= componentFileName %>';

Expand All @@ -7,13 +6,9 @@ export default {
title: '<%= componentName %>'
};

export const primary = () => {
<% if (propsTypeName || isPlainJs ) { %>
<% if (props.length === 0) { %>/* <%= usesEsLint ? 'eslint' : 'tslint'%>-disable-next-line */<% } %>
const props<%= isPlainJs ? '': ':' + propsTypeName %> = {<% for (let prop of props) { %>
<%= prop.name %>: <%= prop.type %>('<%= prop.name %>', <%- prop.defaultValue %>),<% } %>
};
<% } %>
const Template<%= isPlainJs ? '': ': Story<' + propsTypeName %> + '>' = (args) => <<%= componentName %> {...args} />;

return <<%= componentName %> <% for (let prop of props) { %><%= prop.name %> = {props.<%= prop.name %>} <% } %> />;
};
export const primary = Template.bind({})
primary.args = {<% for (let prop of props) { %>
<%= prop.name %>: <%- prop.defaultValue %>,<% } %>
}

0 comments on commit 4c2d4fe

Please sign in to comment.