Open atomicrobokid opened 3 months ago
Disclaimer This information might be inaccurate, due to it being generated automatically To address the issue of Storybook Angular not setting required Inputs correctly, follow these steps:
Update Meta Type: Modify the Meta
type in @storybook/angular
to accept type
with a required
property.
// Path: /code/frameworks/angular/src/types.ts
export interface ArgType {
type?: { name?: string; required?: boolean };
// other properties...
}
Infer Required Fields: Enhance the logic to infer required fields from compodoc
.
// Path: /code/frameworks/angular/src/client/preview/angular/compodoc.ts
function inferArgTypesFromCompodoc(compodocJson) {
return compodocJson.reduce((acc, doc) => {
acc[doc.name] = {
type: { name: doc.type, required: doc.required },
// other properties...
};
return acc;
}, {});
}
Example Usage: Update your story to use the new Meta
type.
// Path: /code/frameworks/angular/template/stories/basics/component-with-provider/di.component.stories.ts
const meta: Meta<DiComponent> = {
component: DiComponent,
argTypes: {
title: { type: { name: 'string', required: true }, control: 'text' },
},
};
export default meta;
/docs/_snippets/api-doc-block-argtypes-parameter.md /code/frameworks/angular/template/stories/basics/component-with-provider/di.component.stories.ts /code/lib/blocks/src/examples/ArgTypesWithSubcomponentsParameters.stories.tsx /docs/_snippets/button-story-with-parameters.md /docs/_snippets/component-story-auto-title.md /code/core/src/csf-tools/CsfFile.test.ts /docs/_snippets/component-story-sort-controls.md /docs/_snippets/button-story-controls-radio-group.md /docs/_snippets/arg-types-in-meta.md /docs/_snippets/component-story-conditional-controls-toggle.md /docs/_snippets/arg-types-options.md /code/renderers/server/template/cli/button.stories.json /docs/_snippets/button-story-hypothetical-example.md /.github/DISCUSSION_TEMPLATE/ideas.yml /docs/_snippets/button-story-baseline.md /code/frameworks/angular/template/stories/core/README.mdx /code/renderers/vue3/src/public-types.test.ts /code/frameworks/angular/template/stories/basics/component-with-enums/enums.component.stories.ts /.github/DISCUSSION_TEMPLATE/rfc.yml /docs/_snippets/checkbox-story-grouped.md /docs/_snippets/storybook-preview-with-angular-polyfills.md /code/frameworks/angular/template/stories/basics/README.mdx /docs/_snippets/api-doc-block-story-parameter.md /docs/_snippets/arg-types-control.md /code/frameworks/angular/template/stories/basics/angular-forms/customControlValueAccessor/custom-cva-component.stories.ts
Describe the bug
I have a very simple component with a story. The component has an input:
@Input({ required: true }) public title: string;
Compodoc successfully generates the correct object:
However it appears the Storybook does not pick up the
required
property automatically (implied by the docs that Storybook will infer required fields) and set theargType
in the table to required.If I manually add the required flag to the argTypes in the story meta, it works (with some caveats)
Story meta (shortened for brevity)
So here we see the second issue. I have to add
@ts-ignore
as it throws a Typescript errorType { required: true; } is not assignable to type
- however it works, so feels like something is missing from the@storybook/angular
Meta
typeI've noticed that if you set
argType
with aname
, typescript doesn't complain:type: { name: 'string', required: true },
Feels strange having to set the name to one of the accepted types, as it doesn't appear to actually do anything. Could maybe make name optional?
Reproduction link
https://stackblitz.com/edit/storybook-angular-required-fields?file=src%2Fstories%2Fbutton.stories.ts
Reproduction steps
type: { required: true}
@ts-ignore
to see first issue - AngularMeta
definition doesn't seetype
as validargType.primary
config entirely so it reverts to Storybooks inferred setup for the controlrequired
even thoughcompodoc
marks it asrequired: true
System
Additional context
Feels like there's two issue here but closely related:
Meta
type from@storybook/angular
needs to be able to seetype: {}
as valid if justrequired
is setcompodoc
that if a field is marked as required in the component and the property"required": true
exists in thedocumentation.json
then automatically mark that control as required without having to manually add that info to the control. Much nicer dev experience