Skip to content
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

Fix Vue prop defaults for objects/arrays. #12634 #12795

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion addons/docs/src/frameworks/vue/extractArgTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ export const extractArgTypes: ArgTypesExtractor = (component) => {
props.forEach(({ propDef, docgenInfo, jsDocTags }) => {
const { name, type, description, defaultValue: defaultSummary, required } = propDef;
const sbType = section === 'props' ? convert(docgenInfo) : { name: 'void' };
let defaultValue = defaultSummary && (defaultSummary.detail || defaultSummary.summary);
let defaultValue: any = defaultSummary && (defaultSummary.detail || defaultSummary.summary);
try {
// eslint-disable-next-line no-eval
defaultValue = eval(defaultValue);
// B/c Vue requires that Object/Array defaults be supplied as factory functions.
if (
typeof defaultValue === 'function' &&
['object', 'array', 'other'].includes(sbType.name)
) {
defaultValue = defaultValue();
}
// eslint-disable-next-line no-empty
} catch {}

Expand Down
30 changes: 29 additions & 1 deletion examples/vue-kitchen-sink/src/stories/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<button
class="button"
:style="{ color, borderColor: color }"
:class="{ rounded }"
:class="computedClasses"
@click="onClick"
@dblclick="onDoubleClick"
>
Expand All @@ -20,12 +20,28 @@
type: Boolean,
default: false,
},
corners: {
type: [Boolean, Array],
default: () => [false, false, false, false],
},
color: {
type: String,
default: '#42b983'
}
},

computed: {
computedClasses() {
return {
rounded: this.rounded,
'rounded-top-left': this.corners[0],
'rounded-top-right': this.corners[1],
'rounded-bottom-right': this.corners[2],
'rounded-bottom-left': this.corners[3],
}
}
},

methods: {
onClick($event) {
/**
Expand All @@ -51,6 +67,18 @@
.rounded {
border-radius: 5px;
}
.rounded-top-left {
border-top-left-radius: 5px;
}
.rounded-top-right {
border-top-right-radius: 5px;
}
.rounded-bottom-right {
border-bottom-right-radius: 5px;
}
.rounded-bottom-left {
border-bottom-left-radius: 5px;
}

.button {
border: 3px solid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
const Template = (args, { argTypes }) => ({
props: Object.keys(argTypes),
components: { MyButton },
template: '<my-button :color="color" :rounded="rounded">{{label}}</my-button>',
template: '<my-button :color="color" :rounded="rounded" :corners="corners">{{label}}</my-button>',
});

export const Rounded = Template.bind({});
Expand All @@ -27,3 +27,11 @@ Square.args = {
color: '#00f',
label: 'A Button with square edges',
};

export const Varied = Template.bind({});
Varied.args = {
rounded: false,
corners: [true, false, true, false],
color: '#00f',
label: 'A Button with varied edges',
};