From 46defba4ad40c4a9aa3bfa0e554a6721a8e78236 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 1 Feb 2024 21:45:28 +0100 Subject: [PATCH 1/2] Improve InputType --- src/story.ts | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 3 deletions(-) diff --git a/src/story.ts b/src/story.ts index 5841ddb..1ac4f3a 100644 --- a/src/story.ts +++ b/src/story.ts @@ -34,15 +34,117 @@ export interface StrictParameters { [name: string]: unknown; } +type ControlType = + | 'object' + | 'boolean' + | 'check' + | 'inline-check' + | 'radio' + | 'inline-radio' + | 'select' + | 'multi-select' + | 'number' + | 'range' + | 'file' + | 'color' + | 'date' + | 'text'; + type ConditionalTest = { truthy?: boolean } | { exists: boolean } | { eq: any } | { neq: any }; type ConditionalValue = { arg: string } | { global: string }; export type Conditional = ConditionalValue & ConditionalTest; export interface InputType { - name?: string; + /** + * @see https://storybook.js.org/docs/api/arg-types#control + */ + control?: + | ControlType + | { + /** + * @see https://storybook.js.org/docs/api/arg-types#controltype + */ + type: ControlType; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlaccept + */ + accept?: string; + /** + * @see https://storybook.js.org/docs/api/arg-types#controllabels + */ + labels?: { [options: string]: string }; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlmax + */ + max?: number; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlmin + */ + min?: number; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlpresetcolors + */ + presetColors?: string[]; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlstep + */ + step?: number; + } + | false; + /** + * @see https://storybook.js.org/docs/api/arg-types#description + */ description?: string; - defaultValue?: any; - type?: SBType | SBScalarType['name']; + /** + * @see https://storybook.js.org/docs/api/arg-types#if + */ if?: Conditional; + /** + * @see https://storybook.js.org/docs/api/arg-types#mapping + */ + mapping?: { [key: string]: { [option: string]: any } }; + /** + * @see https://storybook.js.org/docs/api/arg-types#name + */ + name?: string; + /** + * @see https://storybook.js.org/docs/api/arg-types#options + */ + options?: string[]; + /** + * @see https://storybook.js.org/docs/api/arg-types#table + */ + table?: { + /** + * @see https://storybook.js.org/docs/api/arg-types#tablecategory + */ + category?: string; + /** + * @see https://storybook.js.org/docs/api/arg-types#tabledefaultvalue + */ + defaultValue?: { summary: string; detail?: string }; + /** + * @see https://storybook.js.org/docs/api/arg-types#tabledisable + */ + disable?: boolean; + /** + * @see https://storybook.js.org/docs/api/arg-types#tablesubcategory + */ + subcategory?: string; + /** + * @see https://storybook.js.org/docs/api/arg-types#tabletype + */ + type?: { summary?: string; detail?: string }; + }; + /** + * @see https://storybook.js.org/docs/api/arg-types#type + */ + type?: SBType | SBScalarType['name']; + /** + * @see https://storybook.js.org/docs/api/arg-types#defaultvalue + * + * @deprecated Use `table.defaultValue.summary` instead. + */ + defaultValue?: any; [key: string]: any; } @@ -59,6 +161,9 @@ export interface StrictArgs { [name: string]: unknown; } +/** + * @see https://storybook.js.org/docs/api/arg-types#argtypes + */ export type ArgTypes = { [name in keyof TArgs]: InputType }; export type StrictArgTypes = { [name in keyof TArgs]: StrictInputType }; From 17be521a1cf6a67055510776b28cc69bfbea71ea Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 14 Mar 2024 09:02:09 +0800 Subject: [PATCH 2/2] Improve control types with discriminated union --- src/story.ts | 70 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/story.ts b/src/story.ts index 1ac4f3a..d29c5a2 100644 --- a/src/story.ts +++ b/src/story.ts @@ -60,35 +60,47 @@ export interface InputType { control?: | ControlType | { - /** - * @see https://storybook.js.org/docs/api/arg-types#controltype - */ - type: ControlType; - /** - * @see https://storybook.js.org/docs/api/arg-types#controlaccept - */ - accept?: string; - /** - * @see https://storybook.js.org/docs/api/arg-types#controllabels - */ - labels?: { [options: string]: string }; - /** - * @see https://storybook.js.org/docs/api/arg-types#controlmax - */ - max?: number; - /** - * @see https://storybook.js.org/docs/api/arg-types#controlmin - */ - min?: number; - /** - * @see https://storybook.js.org/docs/api/arg-types#controlpresetcolors - */ - presetColors?: string[]; - /** - * @see https://storybook.js.org/docs/api/arg-types#controlstep - */ - step?: number; - } + /** + * @see https://storybook.js.org/docs/api/arg-types#controltype + */ + type: ControlType; + } + | { + type: 'color'; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlpresetcolors + */ + presetColors?: string[]; + } + | { + type: 'file'; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlaccept + */ + accept?: string; + } + | { + type: 'inline-check' | 'radio' | 'inline-radio' | 'select' | 'multi-select'; + /** + * @see https://storybook.js.org/docs/api/arg-types#controllabels + */ + labels?: { [options: string]: string }; + } + | { + type: 'number' | 'range'; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlmax + */ + max?: number; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlmin + */ + min?: number; + /** + * @see https://storybook.js.org/docs/api/arg-types#controlstep + */ + step?: number; + } | false; /** * @see https://storybook.js.org/docs/api/arg-types#description