Skip to content

Commit

Permalink
Merge pull request #14200 from storybookjs/14056-fix-vue-string-defaults
Browse files Browse the repository at this point in the history
Addon-docs/Vue: Fix string docgen
  • Loading branch information
shilman authored Mar 11, 2021
2 parents 276aa9b + c5b260a commit f2125d8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
9 changes: 7 additions & 2 deletions addons/docs/src/lib/docgen/createPropDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ function createDefaultValue(
type: DocgenType
): PropDefaultValue {
if (defaultValue != null) {
const { value, computed } = defaultValue;
const { value, computed, func } = defaultValue;


if (!isDefaultValueBlacklisted(value)) {
// Work around a bug in `react-docgen-typescript-loader`, which returns 'string' for a string
// default, instead of "'string'" -- which is incorrect (PR to RDT to follow)
if (typeof computed === 'undefined' && type.name === 'string') {
if (
typeof computed === 'undefined' &&
typeof func === 'undefined' &&
type.name === 'string'
) {
return createSummaryValue(JSON.stringify(value));
}

Expand Down
19 changes: 19 additions & 0 deletions addons/docs/src/lib/docgen/extractDocgenProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ TypeSystems.forEach((x) => {
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe('"Default"');
});

it('should map defaults docgen info properly, vue', () => {
const component = createComponent({
...createStringType(x),
description: 'Hey! Hey!',
defaultValue: {
value: "'Default'",
func: false,
},
});

const { propDef } = extractComponentProps(component, DOCGEN_SECTION)[0];

expect(propDef.name).toBe(PROP_NAME);
expect(propDef.type.summary).toBe('string');
expect(propDef.description).toBe('Hey! Hey!');
expect(propDef.required).toBe(false);
expect(propDef.defaultValue.summary).toBe("'Default'");
});
}

it('should remove JSDoc tags from the description', () => {
Expand Down
1 change: 1 addition & 0 deletions addons/docs/src/lib/docgen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface DocgenTypeScriptType extends DocgenType {}
export interface DocgenPropDefaultValue {
value: string;
computed?: boolean;
func?: boolean;
}

export interface DocgenInfo {
Expand Down
8 changes: 7 additions & 1 deletion examples/vue-3-cli/src/stories/Button.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<template>
<button type="button" :class="classes" @click="onClick" :style="style">{{ label }}</button>
<button type="button" :class="classes" @click="onClick" :style="style">
{{ label }} - {{ sublabel }}
</button>
</template>

<script lang="typescript">
Expand All @@ -14,6 +16,10 @@ export default {
type: String,
required: true,
},
sublabel: {
type: String,
default: 'sublabel',
},
primary: {
type: Boolean,
default: false,
Expand Down

0 comments on commit f2125d8

Please sign in to comment.