Open afontcu opened 3 years ago
I ran into the same problem. Any news?
This is still a problem in the v7 beta, fyi. Would love to see some movement here...
If anyone wants a workaround, export this from a 'utils' folder or something inside your stories folder:
export default function (component, property) {
const validator = component?.props?.[property]?.validator?.toString();
if (!component || !property || !validator) {
console.error(`Unable to obtain validator function for ${property} property.`);
return [];
}
else if(/includes/i.test(validator) === false) {
console.error(`Validator for ${property} doesn't contain an "includes" statement`);
return [];
}
const arrayMatch = validator.match(/\[([^\]]*)\]/);
if (arrayMatch) {
const optionsString = arrayMatch[1];
return optionsString.split(',').map(option => option.trim().replace(/^["']/, '').replace(/["']$/, ''));
} else {
console.error(`No options found for ${property} property`);
}
return [];
}
Then inside your story file, you can use it like so:
import Button from 'components/button/Button.vue';
import getOptions from "../_utils/extractOptionsFromValidator";
export default {
title: 'Button',
component: Button,
argTypes: {
size: {
control: {
type: 'select',
},
options: getOptions(Button, 'size'),
},
},
};
Note that this will only work for component props with a validator method that uses the includes
method, e.g.
const props = defineProps({
size: {
type: String,
default: 'medium',
validator: (v) => ['small', 'medium', 'large', 'x-large'].includes(v),
}
});
Describe the bug Storybook does not extract values for an array prop (Vue).
To Reproduce
(for the record, I also tried the
@values small, normal, large
JSDoc comment with no luck).I haven't been able to make it work, and storybook keeps rendering a text input control with no information about the available types.
I checked, and looks like vue-docgen-api is properly extracting values from a definition such as the ones above.
Expected behavior I expected control addons to display a Select with several options, one for each value of the prop.
Screenshots This is what I get:
Instead, I expect something similar to what documentation shows:
Code snippets If applicable, add code samples to help explain your problem.
System
Thank you, folks!