Fix types
This commit is contained in:
parent
9706d094e4
commit
cabf381256
@ -31,6 +31,7 @@ import type {
|
|||||||
GetAssetFunction,
|
GetAssetFunction,
|
||||||
I18nSettings,
|
I18nSettings,
|
||||||
TranslatedProps,
|
TranslatedProps,
|
||||||
|
UnknownField,
|
||||||
ValueOrNestedValue,
|
ValueOrNestedValue,
|
||||||
Widget,
|
Widget,
|
||||||
} from '../../../interface';
|
} from '../../../interface';
|
||||||
@ -206,7 +207,7 @@ const EditorControl = ({
|
|||||||
collection,
|
collection,
|
||||||
config,
|
config,
|
||||||
entry,
|
entry,
|
||||||
field,
|
field: field as UnknownField,
|
||||||
fieldsErrors,
|
fieldsErrors,
|
||||||
submitted,
|
submitted,
|
||||||
getAsset: handleGetAsset,
|
getAsset: handleGetAsset,
|
||||||
|
@ -33,6 +33,7 @@ import type {
|
|||||||
TemplatePreviewProps,
|
TemplatePreviewProps,
|
||||||
TranslatedProps,
|
TranslatedProps,
|
||||||
ValueOrNestedValue,
|
ValueOrNestedValue,
|
||||||
|
WidgetPreviewComponent,
|
||||||
} from '../../../interface';
|
} from '../../../interface';
|
||||||
import type { RootState } from '../../../store';
|
import type { RootState } from '../../../store';
|
||||||
|
|
||||||
@ -104,12 +105,12 @@ function getWidgetFor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const value = values?.[field.name];
|
const value = values?.[field.name];
|
||||||
let fieldWithWidgets: RenderedField = Object.entries(field).reduce((acc, [key, fieldValue]) => {
|
let fieldWithWidgets = Object.entries(field).reduce((acc, [key, fieldValue]) => {
|
||||||
if (!['fields', 'fields'].includes(key)) {
|
if (!['fields', 'fields'].includes(key)) {
|
||||||
acc[key] = fieldValue;
|
acc[key] = fieldValue;
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}, {} as Record<string, unknown>) as Omit<Field, 'fields' | 'field'>;
|
}, {} as Record<string, unknown>) as RenderedField;
|
||||||
|
|
||||||
if ('fields' in field && field.fields) {
|
if ('fields' in field && field.fields) {
|
||||||
fieldWithWidgets = {
|
fieldWithWidgets = {
|
||||||
@ -205,9 +206,9 @@ function getWidget(
|
|||||||
*/
|
*/
|
||||||
return !widget.preview ? null : (
|
return !widget.preview ? null : (
|
||||||
<PreviewHOC
|
<PreviewHOC
|
||||||
previewComponent={widget.preview}
|
previewComponent={widget.preview as WidgetPreviewComponent}
|
||||||
key={key}
|
key={key}
|
||||||
field={field}
|
field={field as RenderedField}
|
||||||
getAsset={getAsset}
|
getAsset={getAsset}
|
||||||
config={config}
|
config={config}
|
||||||
collection={collection}
|
collection={collection}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import type {
|
import type {
|
||||||
EditorPlugin as MarkdownPlugin,
|
EditorPlugin as MarkdownPlugin,
|
||||||
EditorType as MarkdownEditorType,
|
EditorType as MarkdownEditorType
|
||||||
} from '@toast-ui/editor/types/editor';
|
} from '@toast-ui/editor/types/editor';
|
||||||
import type { ToolbarItemOptions as MarkdownToolbarItemOptions } from '@toast-ui/editor/types/ui';
|
import type { ToolbarItemOptions as MarkdownToolbarItemOptions } from '@toast-ui/editor/types/ui';
|
||||||
import type { PropertiesSchema } from 'ajv/dist/types/json-schema';
|
import type { PropertiesSchema } from 'ajv/dist/types/json-schema';
|
||||||
import type { FunctionComponent, ComponentType, ReactNode } from 'react';
|
import type { ComponentType, FunctionComponent, ReactNode } from 'react';
|
||||||
import type { t, TranslateProps as ReactPolyglotTranslateProps } from 'react-polyglot';
|
import type { t, TranslateProps as ReactPolyglotTranslateProps } from 'react-polyglot';
|
||||||
import type { MediaFile as BackendMediaFile } from './backend';
|
import type { MediaFile as BackendMediaFile } from './backend';
|
||||||
import type { EditorControlProps } from './components/Editor/EditorControlPane/EditorControl';
|
import type { EditorControlProps } from './components/Editor/EditorControlPane/EditorControl';
|
||||||
@ -106,13 +106,13 @@ export interface FieldsErrors {
|
|||||||
[field: string]: FieldError[];
|
[field: string]: FieldError[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FieldValidationMethodProps<T = unknown, F extends Field = Field> {
|
export interface FieldValidationMethodProps<T = unknown, F extends BaseField = UnknownField> {
|
||||||
field: F;
|
field: F;
|
||||||
value: T | undefined | null;
|
value: T | undefined | null;
|
||||||
t: t;
|
t: t;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FieldValidationMethod<T = unknown, F extends Field = Field> = (
|
export type FieldValidationMethod<T = unknown, F extends BaseField = UnknownField> = (
|
||||||
props: FieldValidationMethodProps<T, F>,
|
props: FieldValidationMethodProps<T, F>,
|
||||||
) => false | FieldError | Promise<false | FieldError>;
|
) => false | FieldError | Promise<false | FieldError>;
|
||||||
|
|
||||||
@ -222,9 +222,9 @@ export type TranslatedProps<T> = T & ReactPolyglotTranslateProps;
|
|||||||
|
|
||||||
export type GetAssetFunction = (path: string, field?: Field) => Promise<AssetProxy>;
|
export type GetAssetFunction = (path: string, field?: Field) => Promise<AssetProxy>;
|
||||||
|
|
||||||
export interface WidgetControlProps<T, F extends Field = Field, EF extends BaseField = Field> {
|
export interface WidgetControlProps<T, F extends BaseField = UnknownField> {
|
||||||
collection: Collection<EF>;
|
collection: Collection<F>;
|
||||||
config: Config<EF>;
|
config: Config<F>;
|
||||||
entry: Entry;
|
entry: Entry;
|
||||||
field: F;
|
field: F;
|
||||||
fieldsErrors: FieldsErrors;
|
fieldsErrors: FieldsErrors;
|
||||||
@ -252,11 +252,10 @@ export interface WidgetControlProps<T, F extends Field = Field, EF extends BaseF
|
|||||||
|
|
||||||
export interface WidgetPreviewProps<
|
export interface WidgetPreviewProps<
|
||||||
T = unknown,
|
T = unknown,
|
||||||
F extends Field = Field,
|
F extends BaseField = UnknownField
|
||||||
EF extends BaseField = Field,
|
|
||||||
> {
|
> {
|
||||||
config: Config<EF>;
|
config: Config<F>;
|
||||||
collection: Collection<EF>;
|
collection: Collection<F>;
|
||||||
entry: Entry;
|
entry: Entry;
|
||||||
field: RenderedField<F>;
|
field: RenderedField<F>;
|
||||||
getAsset: GetAssetFunction;
|
getAsset: GetAssetFunction;
|
||||||
@ -265,12 +264,11 @@ export interface WidgetPreviewProps<
|
|||||||
|
|
||||||
export type WidgetPreviewComponent<
|
export type WidgetPreviewComponent<
|
||||||
T = unknown,
|
T = unknown,
|
||||||
F extends Field = Field,
|
F extends BaseField = UnknownField
|
||||||
EF extends BaseField = Field,
|
|
||||||
> =
|
> =
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
| React.ReactElement<unknown, string | React.JSXElementConstructor<any>>
|
| React.ReactElement<unknown, string | React.JSXElementConstructor<any>>
|
||||||
| ComponentType<WidgetPreviewProps<T, F, EF>>;
|
| ComponentType<WidgetPreviewProps<T, F>>;
|
||||||
|
|
||||||
export type WidgetsFor<P = EntryData> = <K extends keyof P>(
|
export type WidgetsFor<P = EntryData> = <K extends keyof P>(
|
||||||
name: K,
|
name: K,
|
||||||
@ -284,7 +282,7 @@ export type WidgetsFor<P = EntryData> = <K extends keyof P>(
|
|||||||
widgets: Record<keyof P[K], React.ReactNode>;
|
widgets: Record<keyof P[K], React.ReactNode>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface TemplatePreviewProps<T = EntryData, EF extends BaseField = Field> {
|
export interface TemplatePreviewProps<T = EntryData, EF extends BaseField = UnknownField> {
|
||||||
collection: Collection<EF>;
|
collection: Collection<EF>;
|
||||||
fields: Field<EF>[];
|
fields: Field<EF>[];
|
||||||
entry: Entry<T>;
|
entry: Entry<T>;
|
||||||
@ -295,28 +293,28 @@ export interface TemplatePreviewProps<T = EntryData, EF extends BaseField = Fiel
|
|||||||
widgetsFor: WidgetsFor<T>;
|
widgetsFor: WidgetsFor<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TemplatePreviewComponent<T = EntryData, EF extends BaseField = Field> = ComponentType<
|
export type TemplatePreviewComponent<T = EntryData, EF extends BaseField = UnknownField> = ComponentType<
|
||||||
TemplatePreviewProps<T, EF>
|
TemplatePreviewProps<T, EF>
|
||||||
>;
|
>;
|
||||||
|
|
||||||
export interface WidgetOptions<T = unknown, F extends Field = Field> {
|
export interface WidgetOptions<T = unknown, F extends BaseField = UnknownField> {
|
||||||
validator?: Widget<T, F>['validator'];
|
validator?: Widget<T, F>['validator'];
|
||||||
getValidValue?: Widget<T, F>['getValidValue'];
|
getValidValue?: Widget<T, F>['getValidValue'];
|
||||||
schema?: Widget<T, F>['schema'];
|
schema?: Widget<T, F>['schema'];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Widget<T = unknown, F extends Field = Field, EF extends BaseField = Field> {
|
export interface Widget<T = unknown, F extends BaseField = UnknownField> {
|
||||||
control: ComponentType<WidgetControlProps<T, F, EF>>;
|
control: ComponentType<WidgetControlProps<T, F>>;
|
||||||
preview?: WidgetPreviewComponent<T, F, EF>;
|
preview?: WidgetPreviewComponent<T, F>;
|
||||||
validator: FieldValidationMethod<T, F>;
|
validator: FieldValidationMethod<T, F>;
|
||||||
getValidValue: (value: T | undefined | null) => T | undefined | null;
|
getValidValue: (value: T | undefined | null) => T | undefined | null;
|
||||||
schema?: PropertiesSchema<unknown>;
|
schema?: PropertiesSchema<unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WidgetParam<T = unknown, F extends Field = Field, EF extends BaseField = Field> {
|
export interface WidgetParam<T = unknown, F extends BaseField = UnknownField> {
|
||||||
name: string;
|
name: string;
|
||||||
controlComponent: Widget<T, F, EF>['control'];
|
controlComponent: Widget<T, F>['control'];
|
||||||
previewComponent?: Widget<T, F, EF>['preview'];
|
previewComponent?: Widget<T, F>['preview'];
|
||||||
options?: WidgetOptions<T, F>;
|
options?: WidgetOptions<T, F>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,7 +478,7 @@ export type AuthScope = 'repo' | 'public_repo';
|
|||||||
|
|
||||||
export type SlugEncoding = 'unicode' | 'ascii';
|
export type SlugEncoding = 'unicode' | 'ascii';
|
||||||
|
|
||||||
export type RenderedField<T extends Field = Field> = Omit<T, 'fields'> & {
|
export type RenderedField<T extends BaseField = UnknownField> = Omit<T, 'fields'> & {
|
||||||
fields?: ReactNode[];
|
fields?: ReactNode[];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -850,7 +848,7 @@ export interface PreviewStyle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface MarkdownPluginFactoryProps {
|
export interface MarkdownPluginFactoryProps {
|
||||||
config: Config;
|
config: Config<MarkdownField>;
|
||||||
field: MarkdownField;
|
field: MarkdownField;
|
||||||
media: MediaHolder;
|
media: MediaHolder;
|
||||||
mode: 'editor' | 'preview';
|
mode: 'editor' | 'preview';
|
||||||
|
@ -121,7 +121,8 @@ export function getPreviewTemplate(name: string): TemplatePreviewComponent<Entry
|
|||||||
/**
|
/**
|
||||||
* Editor Widgets
|
* Editor Widgets
|
||||||
*/
|
*/
|
||||||
export function registerWidget(widgets: WidgetParam[]): void;
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
export function registerWidget(widgets: WidgetParam<any, any>[]): void;
|
||||||
export function registerWidget(widget: WidgetParam): void;
|
export function registerWidget(widget: WidgetParam): void;
|
||||||
export function registerWidget<T = unknown>(
|
export function registerWidget<T = unknown>(
|
||||||
name: string,
|
name: string,
|
||||||
|
@ -7,6 +7,7 @@ import type {
|
|||||||
FieldError,
|
FieldError,
|
||||||
FieldValidationMethod,
|
FieldValidationMethod,
|
||||||
FieldValidationMethodProps,
|
FieldValidationMethodProps,
|
||||||
|
UnknownField,
|
||||||
ValueOrNestedValue,
|
ValueOrNestedValue,
|
||||||
Widget,
|
Widget,
|
||||||
} from '../../interface';
|
} from '../../interface';
|
||||||
@ -76,7 +77,6 @@ export function validatePattern({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Fix typings
|
|
||||||
export async function validate(
|
export async function validate(
|
||||||
path: string,
|
path: string,
|
||||||
field: Field,
|
field: Field,
|
||||||
@ -94,7 +94,7 @@ export async function validate(
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const validation of validations) {
|
for (const validation of validations) {
|
||||||
const response = await validation({ field, value: validValue, t });
|
const response = await validation({ field: field as UnknownField, value: validValue, t });
|
||||||
if (response) {
|
if (response) {
|
||||||
errors.push(response);
|
errors.push(response);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user