add types for all field-types, and other type tweaks

This commit is contained in:
Elliot DeNolf
2020-11-22 00:46:10 -05:00
parent cb33417924
commit fdbdf93250
3 changed files with 103 additions and 27 deletions

View File

@@ -15,6 +15,7 @@ export type Collection = {
singular: string;
plural: string;
};
fields: Field[];
admin?: {
useAsTitle?: string;
defaultColumns?: string[];
@@ -62,10 +63,10 @@ export type Collection = {
generateEmailSubject?: (args?: {req?: PayloadRequest}) => string,
}
};
fields: Field[];
upload: {
imageSizes: ImageSize[];
staticURL: string;
staticDir: string;
adminThumbnail?: string;
};
};

View File

@@ -1,4 +1,6 @@
/* eslint-disable no-use-before-define */
import { CSSProperties } from 'react';
import { Access } from '../../config/types';
import { PayloadRequest } from '../../express/types/payloadRequest';
// TODO: add generic type and use mongoose types for originalDoc & data
@@ -9,29 +11,17 @@ export type FieldHook = (args: {
operation?: 'create' | 'update',
req?: PayloadRequest}) => Promise<any> | any;
export type Field = {
type FieldBase = {
name: string;
label?: string;
type:
| 'number'
| 'text'
| 'email'
| 'textarea'
| 'richText'
| 'code'
| 'radio'
| 'checkbox'
| 'date'
| 'upload'
| 'relationship'
| 'row'
| 'array'
| 'group'
| 'select'
| 'blocks';
localized?: boolean;
label: string;
required?: boolean;
unique?: boolean;
defaultValue?: any;
hidden?: boolean;
fields?: Field[];
localized?: boolean;
maxLength?: number;
height?: number;
// eslint-disable-next-line no-use-before-define
hooks?: {
beforeValidate?: FieldHook[];
beforeChange?: FieldHook[];
@@ -39,10 +29,99 @@ export type Field = {
afterRead?: FieldHook[];
}
admin?: {
position?: string;
position?: 'sidebar';
width?: string;
style?: CSSProperties;
readOnly?: boolean;
disabled?: boolean;
components?: { [key: string]: JSX.Element | (() => JSX.Element) };
};
access?: {
create?: Access;
read?: Access;
update?: Access;
delete?: Access;
admin?: Access;
unlock?: Access;
};
}
export type StandardField = FieldBase & {
fields?: Field[];
}
export type NumberField = StandardField & { type: 'number'; };
export type TextField = StandardField & { type: 'text'; };
export type EmailField = StandardField & { type: 'email'; };
export type TextareaField = StandardField & { type: 'textarea'; };
export type CodeField = StandardField & { type: 'code'; };
export type CheckboxField = StandardField & { type: 'checkbox'; };
export type DateboxField = StandardField & { type: 'date'; };
export type GroupField = StandardField & { type: 'group'; };
export type RowField = StandardField & { type: 'row'; };
export type UploadField = FieldBase & {
type: 'upload';
relationTo: string;
}
export type SelectField = FieldBase & {
type: 'select';
options: {
value: string;
label: string;
}[];
hasMany?: boolean;
}
export type SelectManyField = SelectField & {
hasMany: true;
}
export type RelationshipField = FieldBase & {
type: 'relationship';
relationTo: string | string[];
}
type RichTextElements = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'blockquote' | 'ul' | 'ol' | 'link';
type RichTextLeaves = 'bold' | 'italic' | 'underline' | 'strikethrough';
export type RichTextField = FieldBase & {
type: 'richText';
admin?: {
elements?: RichTextElements[];
leaves?: RichTextLeaves[];
}
}
export type ArrayField = FieldBase & {
type: 'array';
minRows?: number;
maxRows?: number;
fields?: Field[];
}
export type RadioField = FieldBase & {
type: 'radio';
options: {
value: string;
label: string;
}[];
}
export type Block = {
slug: string,
labels: {
singular: string;
plural: string;
};
fields: Field[],
}
export type BlockField = FieldBase & {
type: 'blocks';
minRows?: number;
maxRows?: number;
blocks?: Block[];
};
export type Field = NumberField | TextField | EmailField | TextareaField | CodeField | CheckboxField | DateboxField | BlockField | RadioField | RelationshipField | ArrayField | RichTextField | GroupField | RowField | SelectField | SelectManyField | UploadField;