fix(typings): discriminated types for widgets (#4640)

This commit is contained in:
s 2020-11-30 09:04:25 -05:00 committed by GitHub
parent 97fc85b19c
commit 63ea63defe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,23 +56,29 @@ declare module 'netlify-cms-core' {
default_locale?: string; default_locale?: string;
} }
export interface CmsField { export interface CmsFieldBase {
name: string; name: string;
label?: string; label?: string;
widget?: string;
required?: boolean; required?: boolean;
hint?: string; hint?: string;
pattern?: [string, string]; pattern?: [string, string];
default?: any;
i18n?: boolean | 'translate' | 'duplicate'; i18n?: boolean | 'translate' | 'duplicate';
}
export interface CmsFieldCode {
widget: 'code';
default?: any;
/** If widget === "code" */
default_language?: string; default_language?: string;
allow_language_selection?: boolean; allow_language_selection?: boolean;
keys?: { code: string; lang: string }; keys?: { code: string; lang: string };
output_code_only?: boolean; output_code_only?: boolean;
}
export interface CmsFieldDateTime {
widget: 'datetime';
default?: string;
/** If widget === "datetime" */
format?: string; format?: string;
date_format?: boolean | string; date_format?: boolean | string;
time_format?: boolean | string; time_format?: boolean | string;
@ -90,60 +96,102 @@ declare module 'netlify-cms-core' {
* @deprecated Use picker_utc instead * @deprecated Use picker_utc instead
*/ */
pickerUtc?: boolean; pickerUtc?: boolean;
}
export interface CmsFieldFileOrImage {
widget: 'file' | 'image';
default?: string;
/** If widget === "file" || widget === "image" */
media_library?: CmsMediaLibrary; media_library?: CmsMediaLibrary;
allow_multiple?: boolean; allow_multiple?: boolean;
config?: any; config?: any;
}
export interface CmsFieldObject {
widget: 'object';
default?: any;
/** If widget === "object" || widget === "list" */
fields?: CmsField[];
collapsed?: boolean; collapsed?: boolean;
summary?: string;
fields: CmsField[];
}
export interface CmsFieldList {
widget: 'list';
default?: any;
/** If widget === "list" */
field?: CmsField;
allow_add?: boolean; allow_add?: boolean;
collapsed?: boolean;
summary?: string; summary?: string;
minimize_collapsed?: boolean; minimize_collapsed?: boolean;
label_singular?: string; label_singular?: string;
types?: CmsField[]; field?: CmsField;
fields?: CmsField[];
max?: number;
min?: number;
add_to_top?: boolean;
types?: (CmsFieldBase & CmsFieldObject)[];
}
export interface CmsFieldMap {
widget: 'map';
default?: string;
/** If widget === "map" */
decimals?: number; decimals?: number;
type?: CmsMapWidgetType; type?: CmsMapWidgetType;
}
export interface CmsFieldMarkdown {
widget: 'markdown';
default?: string;
/** If widget === "markdown" */
minimal?: boolean; minimal?: boolean;
buttons?: CmsMarkdownWidgetButton[]; buttons?: CmsMarkdownWidgetButton[];
editor_components?: string[]; editor_components?: string[];
modes?: ('raw' | 'rich_text')[];
/** /**
* @deprecated Use editor_components instead * @deprecated Use editor_components instead
*/ */
editorComponents?: string[]; editorComponents?: string[];
}
export interface CmsFieldNumber {
widget: 'number';
default?: string | number;
/** If widget === "number" */
value_type?: 'int' | 'float' | string; value_type?: 'int' | 'float' | string;
min?: number;
max?: number;
step?: number; step?: number;
/** /**
* @deprecated Use valueType instead * @deprecated Use valueType instead
*/ */
valueType?: 'int' | 'float' | string; valueType?: 'int' | 'float' | string;
}
/** If widget === "number" || widget === "select" */ export interface CmsFieldSelect {
widget: 'select';
default?: string | string[];
options: string[] | CmsSelectWidgetOptionObject[];
multiple?: boolean;
min?: number; min?: number;
max?: number; max?: number;
}
/** If widget === "relation" || widget === "select" */ export interface CmsFieldRelation {
multiple?: boolean; widget: 'relation';
default?: string | string[];
/** If widget === "relation" */ collection: string;
collection?: string; value_field: string;
value_field?: string; search_fields: string[];
search_fields?: string[];
file?: string; file?: string;
display_fields?: string[]; display_fields?: string[];
multiple?: boolean;
options_length?: number; options_length?: number;
/** /**
@ -162,11 +210,35 @@ declare module 'netlify-cms-core' {
* @deprecated Use options_length instead * @deprecated Use options_length instead
*/ */
optionsLength?: number; optionsLength?: number;
/** If widget === "select" */
options?: string[] | CmsSelectWidgetOptionObject[];
} }
export interface CmsFieldHidden {
widget: 'hidden';
default?: any;
}
export interface CmsFieldStringOrText {
// This is the default widget, so declaring its type is optional.
widget?: 'string' | 'text';
default?: string;
}
export type CmsField = CmsFieldBase &
(
| CmsFieldCode
| CmsFieldDateTime
| CmsFieldFileOrImage
| CmsFieldList
| CmsFieldMap
| CmsFieldMarkdown
| CmsFieldNumber
| CmsFieldObject
| CmsFieldRelation
| CmsFieldSelect
| CmsFieldHidden
| CmsFieldStringOrText
);
export interface CmsCollectionFile { export interface CmsCollectionFile {
name: string; name: string;
label: string; label: string;