Comparing version 4.0.0-alpha.0 to 4.0.0-alpha.1
/// <reference types="react" /> | ||
/// <reference types="node" /> | ||
import { BaseForm } from './BaseForm'; | ||
import { ValidatedQuickForm, ValidatedQuickFormProps, ValidatedQuickFormState } from './ValidatedQuickForm'; | ||
import { DeepPartial, ModelTransformMode } from './types'; | ||
export declare type AutoFormProps<Model> = ValidatedQuickFormProps<Model> & { | ||
onChangeModel?: (model: DeepPartial<Model>) => void; | ||
import { ModelTransformMode, UnknownObject } from './types'; | ||
export declare type AutoFormProps<Model extends UnknownObject> = ValidatedQuickFormProps<Model> & { | ||
onChangeModel?: (model: Model) => void; | ||
}; | ||
export declare type AutoFormState<Model> = ValidatedQuickFormState<Model> & { | ||
model: DeepPartial<Model>; | ||
export declare type AutoFormState<Model extends UnknownObject> = ValidatedQuickFormState<Model> & { | ||
model: Model; | ||
}; | ||
export declare function Auto<Base extends typeof ValidatedQuickForm>(Base: Base): { | ||
new <Model, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
getModel(mode: ModelTransformMode): State["model"]; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -47,4 +48,4 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
onReset(): void; | ||
@@ -76,3 +77,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -99,29 +100,29 @@ autosave: boolean; | ||
export declare const AutoForm: { | ||
new <Model, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
getModel(mode: ModelTransformMode): State["model"]; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -135,4 +136,4 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
onReset(): void; | ||
@@ -164,3 +165,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -186,28 +187,28 @@ autosave: boolean; | ||
} & { | ||
new <Model_1, Props_1 extends import("./ValidatedForm").ValidatedFormProps<Model_1> = import("./ValidatedForm").ValidatedFormProps<Model_1>, State_1 extends import("./ValidatedForm").ValidatedFormState<Model_1> = import("./ValidatedForm").ValidatedFormState<Model_1>>(props: Props_1): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model_1 extends UnknownObject, Props_1 extends import("./ValidatedForm").ValidatedFormProps<Model_1> = import("./ValidatedForm").ValidatedFormProps<Model_1>, State_1 extends import("./ValidatedForm").ValidatedFormState<Model_1> = import("./ValidatedForm").ValidatedFormState<Model_1>>(props: Props_1): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model_1>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props_1, prevState: State_1, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State_1): Partial<State_1>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props_1["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props_1["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model_1>; | ||
getContextModel(): Model_1; | ||
getContextState(): { | ||
@@ -221,5 +222,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: ModelTransformMode | undefined, model?: DeepPartial<Model_1>): DeepPartial<Model_1>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: ModelTransformMode | undefined, model?: Model_1): Model_1; | ||
onReset(): void; | ||
@@ -250,3 +251,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -263,6 +264,6 @@ autosave: boolean; | ||
} & { | ||
new <Model_2, Props_2 extends import("./QuickForm").QuickFormProps<Model_2> = import("./QuickForm").QuickFormProps<Model_2>, State_2 extends import("./QuickForm").QuickFormState<Model_2> = import("./QuickForm").QuickFormState<Model_2>>(props: Props_2): { | ||
new <Model_2 extends UnknownObject, Props_2 extends import("./QuickForm").QuickFormProps<Model_2> = import("./QuickForm").QuickFormProps<Model_2>, State_2 extends import("./QuickForm").QuickFormState<Model_2> = import("./QuickForm").QuickFormState<Model_2>>(props: Props_2): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -278,12 +279,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model_2>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): DeepPartial<Model_2>; | ||
getContextError(): unknown; | ||
getContextModel(): Model_2; | ||
getContextState(): { | ||
@@ -297,9 +298,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: ModelTransformMode | undefined, model?: DeepPartial<Model_2>): DeepPartial<Model_2>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: ModelTransformMode | undefined, model?: Model_2): Model_2; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State_2): Partial<State_2>; | ||
onReset(): void; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -306,0 +307,0 @@ context: any; |
import { Component, SyntheticEvent } from 'react'; | ||
import { Bridge } from './Bridge'; | ||
import { ChangedMap, Context, DeepPartial, ModelTransformMode } from './types'; | ||
export declare type BaseFormProps<Model> = { | ||
import { ChangedMap, Context, ModelTransformMode, UnknownObject } from './types'; | ||
export declare type BaseFormProps<Model extends UnknownObject> = { | ||
autosave: boolean; | ||
autosaveDelay: number; | ||
disabled?: boolean; | ||
error: any; | ||
error: unknown; | ||
id?: string; | ||
label: boolean; | ||
model: DeepPartial<Model>; | ||
modelTransform?: (mode: ModelTransformMode, model: DeepPartial<Model>) => DeepPartial<Model>; | ||
model: Model; | ||
modelTransform?: (mode: ModelTransformMode, model: Model) => Model; | ||
noValidate: boolean; | ||
onChange?: (key: string, value: any) => void; | ||
onSubmit: (model: DeepPartial<Model>) => void | Promise<any>; | ||
onChange?: (key: string, value: unknown) => void; | ||
onSubmit: (model: Model) => void | Promise<unknown>; | ||
placeholder?: boolean; | ||
@@ -21,3 +21,3 @@ readOnly?: boolean; | ||
}; | ||
export declare type BaseFormState<Model> = { | ||
export declare type BaseFormState<Model extends UnknownObject> = { | ||
changed: boolean; | ||
@@ -29,3 +29,3 @@ changedMap: ChangedMap<Model>; | ||
}; | ||
export declare class BaseForm<Model, Props extends BaseFormProps<Model> = BaseFormProps<Model>, State extends BaseFormState<Model> = BaseFormState<Model>> extends Component<Props, State> { | ||
export declare class BaseForm<Model extends UnknownObject, Props extends BaseFormProps<Model> = BaseFormProps<Model>, State extends BaseFormState<Model> = BaseFormState<Model>> extends Component<Props, State> { | ||
static displayName: string; | ||
@@ -45,7 +45,7 @@ static defaultProps: { | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: ReturnType<typeof setTimeout> | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: SyntheticEvent) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: SyntheticEvent) => Promise<unknown>; | ||
randomId: () => string; | ||
@@ -60,3 +60,3 @@ getContext(): Context<Model>; | ||
getContextOnSubmit(): Context<Model>['onSubmit']; | ||
getModel(mode?: ModelTransformMode, model?: DeepPartial<Model>): Context<Model>['model']; | ||
getModel(mode?: ModelTransformMode, model?: Model): Context<Model>['model']; | ||
getNativeFormProps(): { | ||
@@ -67,7 +67,7 @@ [key: string]: unknown; | ||
}; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onReset(): void; | ||
onSubmit(event?: SyntheticEvent): Promise<any>; | ||
onSubmit(event?: SyntheticEvent): Promise<unknown>; | ||
render(): JSX.Element; | ||
} |
@@ -147,3 +147,3 @@ "use strict"; | ||
if (this.delayId) { | ||
this.delayId = clearTimeout(this.delayId); | ||
clearTimeout(this.delayId); | ||
} | ||
@@ -150,0 +150,0 @@ // Delay autosave by `autosaveDelay` milliseconds... |
@@ -1,16 +0,59 @@ | ||
declare type Error = any; | ||
declare type Field = any; | ||
declare type FieldType = any; | ||
import { UnknownObject } from './types'; | ||
export declare abstract class Bridge { | ||
constructor(...args: any[]); | ||
getError(name: string, error: Error): Error; | ||
getErrorMessage(name: string, error: Error): string; | ||
getErrorMessages(error: Error): string[]; | ||
getField(name: string): Field; | ||
getInitialValue(name: string): any; | ||
getProps(name: string, props: Record<string, any>): Record<string, any>; | ||
/** | ||
* Get an error for field `name` out of `error`. There is no standarized | ||
* format, but fields treat truthy values as a sign of being invalid. Fields | ||
* receive this as a `error` guaranteed prop. | ||
*/ | ||
getError(name: string, error: unknown): unknown; | ||
/** | ||
* Get an error message for field `name` out of `error`. If there is no error, | ||
* return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
* prop. | ||
*/ | ||
getErrorMessage(name: string, error: unknown): string; | ||
/** | ||
* Get all error messages from `error`. Only `ErrorsField` make use of that | ||
* (in builtin themes). | ||
*/ | ||
getErrorMessages(error: unknown): string[]; | ||
/** | ||
* Get internal field definition for field `name`. Fields receive this as a | ||
* `field` guaranteed prop. There is no standarized field format. Most bridges | ||
* use it as a common object, used in calculation of initial values, props, | ||
* and types. | ||
*/ | ||
getField(name: string): unknown; | ||
/** | ||
* Get initial value of field `name`. It is used as a default when no value is | ||
* set (e.g. the form is rendered with an empty `model`). Additionally, | ||
* `props` are this field instance props. If a field is rendered multiple | ||
* times, this function will be called multiple times, possibly with different | ||
* `props`. | ||
*/ | ||
getInitialValue(name: string): unknown; | ||
/** | ||
* Get props defined in schema for a field `name`. There are no required nor | ||
* banned fields, however properties like `required` are often available. | ||
*/ | ||
getProps(name: string): UnknownObject; | ||
/** | ||
* Get a list of subfields of field `name` or top-level fields, if no `name` | ||
* is passed. | ||
*/ | ||
getSubfields(name?: string): string[]; | ||
getType(name: string): FieldType; | ||
getValidator(options?: any): (model: Record<string, any>) => null | Error | Promise<null | Error>; | ||
/** | ||
* There is no standarized field type format. However, `AutoField` component | ||
* will work correctly only with standard JavaScript constructors, like | ||
* `String` or `Number`. | ||
*/ | ||
getType(name: string): unknown; | ||
/** | ||
* Get a validator function. The `options` here are from the `validator` prop | ||
* of the form. A validator function receives a model and returns an error or | ||
* a promise that will resolve (not reject!) with an error. If there is no | ||
* error, return (or resolve with) a `null` value instead. | ||
*/ | ||
getValidator(options?: unknown): (model: UnknownObject) => unknown; | ||
} | ||
export {}; |
@@ -11,54 +11,72 @@ "use strict"; | ||
} | ||
// Get an error for field `name` out of `error`. There is no standarized | ||
// format, but fields treat truthy values as a sign of being invalid. Fields | ||
// receive this as a `error` guaranteed prop. | ||
/** | ||
* Get an error for field `name` out of `error`. There is no standarized | ||
* format, but fields treat truthy values as a sign of being invalid. Fields | ||
* receive this as a `error` guaranteed prop. | ||
*/ | ||
getError(name, error) { | ||
return invariant_1.default(false, '%s have not implemented `getError` method (args=%o).', this.constructor.name, { name, error }); | ||
} | ||
// Get an error message for field `name` out of `error`. If there is no error, | ||
// return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
// prop. | ||
/** | ||
* Get an error message for field `name` out of `error`. If there is no error, | ||
* return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
* prop. | ||
*/ | ||
getErrorMessage(name, error) { | ||
return invariant_1.default(false, '%s have not implemented `getErrorMessage` method (args=%o).', this.constructor.name, { name, error }); | ||
} | ||
// Get all error messages from `error`. Only `ErrorsField` make use of that | ||
// (in builtin themes). | ||
/** | ||
* Get all error messages from `error`. Only `ErrorsField` make use of that | ||
* (in builtin themes). | ||
*/ | ||
getErrorMessages(error) { | ||
return invariant_1.default(false, '%s have not implemented `getErrorMessages` method (args=%o).', this.constructor.name, { error }); | ||
} | ||
// Get internal field definition for field `name`. Fields receive this as a | ||
// `field` guaranteed prop. | ||
/** | ||
* Get internal field definition for field `name`. Fields receive this as a | ||
* `field` guaranteed prop. There is no standarized field format. Most bridges | ||
* use it as a common object, used in calculation of initial values, props, | ||
* and types. | ||
*/ | ||
getField(name) { | ||
return invariant_1.default(false, '%s have not implemented `getField` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get initial value of field `name`. It is used as a default when no value is | ||
// set (e.g. the form is rendered with an empty `model`). Additionally, | ||
// `props` are this field instance props. If a field is rendered multiple | ||
// times, this function will be called multiple times, possibly with different | ||
// `props`. | ||
/** | ||
* Get initial value of field `name`. It is used as a default when no value is | ||
* set (e.g. the form is rendered with an empty `model`). Additionally, | ||
* `props` are this field instance props. If a field is rendered multiple | ||
* times, this function will be called multiple times, possibly with different | ||
* `props`. | ||
*/ | ||
getInitialValue(name) { | ||
return invariant_1.default(false, '%s have not implemented `getInitialValue` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get props defined in schema for a field `name`. There are no required nor | ||
// banned fields, however properties like `required` are often available. | ||
// Additionally, `props` are this field instance props. If a field is rendered | ||
// multiple times, this function will be called multiple times, possibly with | ||
// different `props`. | ||
getProps(name, props) { | ||
return invariant_1.default(false, '%s have not implemented `getProps` method (args=%o).', this.constructor.name, { name, props }); | ||
/** | ||
* Get props defined in schema for a field `name`. There are no required nor | ||
* banned fields, however properties like `required` are often available. | ||
*/ | ||
getProps(name) { | ||
return invariant_1.default(false, '%s have not implemented `getProps` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get a list of subfields of field `name` or top-level fields, if no `name` | ||
// is passed. | ||
/** | ||
* Get a list of subfields of field `name` or top-level fields, if no `name` | ||
* is passed. | ||
*/ | ||
getSubfields(name) { | ||
return invariant_1.default(false, '%s have not implemented `getSubfields` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get a type of field `name`. See `FieldTypeType` for details. | ||
/** | ||
* There is no standarized field type format. However, `AutoField` component | ||
* will work correctly only with standard JavaScript constructors, like | ||
* `String` or `Number`. | ||
*/ | ||
getType(name) { | ||
return invariant_1.default(false, '%s have not implemented `getType` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get a validator function. The `options` here are from the `validator` prop | ||
// of the form. A validator function receives a model and returns an error or | ||
// a promise that will resolve (not reject!) with an error. If there is no | ||
// error, return (or resolve with) a `null` value instead. | ||
// eslint-disable-next-line prettier/prettier | ||
/** | ||
* Get a validator function. The `options` here are from the `validator` prop | ||
* of the form. A validator function receives a model and returns an error or | ||
* a promise that will resolve (not reject!) with an error. If there is no | ||
* error, return (or resolve with) a `null` value instead. | ||
*/ | ||
getValidator(options) { | ||
@@ -65,0 +83,0 @@ return invariant_1.default(false, '%s have not implemented `getValidator` method (args=%o).', this.constructor.name, { options }); |
import { ComponentType, FunctionComponent } from 'react'; | ||
import { GuaranteedProps, Override } from './types'; | ||
import { GuaranteedProps, Override, UnknownObject } from './types'; | ||
/** @internal */ | ||
@@ -9,3 +9,3 @@ export declare type ConnectFieldOptions = { | ||
/** @internal */ | ||
export declare type ConnectedFieldProps<Props extends Record<string, unknown>, Value = Props['value']> = Override<Props, Override<Partial<GuaranteedProps<Value>>, { | ||
export declare type ConnectedFieldProps<Props extends UnknownObject, Value = Props['value']> = Override<Props, Override<Partial<GuaranteedProps<Value>>, { | ||
label?: Props['label'] | boolean | null | string; | ||
@@ -16,6 +16,6 @@ name: string; | ||
/** @internal */ | ||
export declare type ConnectedField<Props extends Record<string, unknown>, Value = Props['value']> = FunctionComponent<ConnectedFieldProps<Props, Value>> & { | ||
export declare type ConnectedField<Props extends UnknownObject, Value = Props['value']> = FunctionComponent<ConnectedFieldProps<Props, Value>> & { | ||
Component: ComponentType<Props>; | ||
options?: ConnectFieldOptions; | ||
}; | ||
export declare function connectField<Props extends Record<string, unknown>, Value = Props['value']>(Component: ComponentType<Props>, options?: ConnectFieldOptions): ConnectedField<Props, Value>; | ||
export declare function connectField<Props extends UnknownObject, Value = Props['value']>(Component: ComponentType<Props>, options?: ConnectFieldOptions): ConnectedField<Props, Value>; |
import { ComponentType, ReactElement } from 'react'; | ||
import { connectField } from './connectField'; | ||
import { Context } from './types'; | ||
import { Context, UnknownObject } from './types'; | ||
import { useField } from './useField'; | ||
export declare type AutoFieldProps = { | ||
export declare type AutoFieldProps = UnknownObject & { | ||
component?: Component; | ||
experimental_absoluteName?: boolean; | ||
name: string; | ||
[prop: string]: unknown; | ||
}; | ||
@@ -13,6 +13,6 @@ /** @internal */ | ||
/** @internal */ | ||
export declare type ComponentDetector = (props: ReturnType<typeof useField>[0], uniforms: Context<Record<string, unknown>>) => Component; | ||
export declare function createAutoField(defaultComponentDetector: ComponentDetector): ((rawProps: AutoFieldProps) => ReactElement) & { | ||
export declare type ComponentDetector = (props: ReturnType<typeof useField>[0], uniforms: Context<UnknownObject>) => Component; | ||
export declare function createAutoField(defaultComponentDetector: ComponentDetector): (({ experimental_absoluteName: absoluteName, ...rawProps }: AutoFieldProps) => ReactElement) & { | ||
componentDetectorContext: import("react").Context<ComponentDetector>; | ||
defaultComponentDetector: ComponentDetector; | ||
}; |
@@ -10,9 +10,11 @@ "use strict"; | ||
const context = react_1.createContext(defaultComponentDetector); | ||
function AutoField(rawProps) { | ||
var _a; | ||
const [props, uniforms] = useField_1.useField(rawProps.name, rawProps); | ||
function AutoField(_a) { | ||
var _b; | ||
var { experimental_absoluteName: absoluteName } = _a, rawProps = tslib_1.__rest(_a, ["experimental_absoluteName"]); | ||
const options = { absoluteName }; | ||
const [props, uniforms] = useField_1.useField(rawProps.name, rawProps, options); | ||
const componentDetector = react_1.useContext(context); | ||
const component = componentDetector(props, uniforms); | ||
invariant_1.default(component, 'AutoField received no component for: %s', props.name); | ||
return 'options' in component && ((_a = component.options) === null || _a === void 0 ? void 0 : _a.kind) === 'leaf' | ||
return 'options' in component && ((_b = component.options) === null || _b === void 0 ? void 0 : _b.kind) === 'leaf' | ||
? react_1.createElement(component.Component, props) | ||
@@ -19,0 +21,0 @@ : react_1.createElement(component, rawProps); |
@@ -0,13 +1,15 @@ | ||
/// <reference types="node" /> | ||
import React, { ComponentType } from 'react'; | ||
import { BaseForm, BaseFormProps, BaseFormState } from './BaseForm'; | ||
export declare type QuickFormProps<Model> = BaseFormProps<Model> & { | ||
import { UnknownObject } from './types'; | ||
export declare type QuickFormProps<Model extends UnknownObject> = BaseFormProps<Model> & { | ||
errorsField?: ComponentType; | ||
submitField?: ComponentType; | ||
}; | ||
export declare type QuickFormState<Model> = BaseFormState<Model>; | ||
export declare type QuickFormState<Model extends UnknownObject> = BaseFormState<Model>; | ||
export declare function Quick<Base extends typeof BaseForm>(Base: Base): { | ||
new <Model, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -23,12 +25,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): import("./types").DeepPartial<Model>; | ||
getContextError(): unknown; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -42,9 +44,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model>): import("./types").DeepPartial<Model>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onReset(): void; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -85,6 +87,6 @@ context: any; | ||
export declare const QuickForm: { | ||
new <Model, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -100,12 +102,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): import("./types").DeepPartial<Model>; | ||
getContextError(): unknown; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -119,9 +121,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model>): import("./types").DeepPartial<Model>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onReset(): void; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -128,0 +130,0 @@ context: any; |
@@ -7,11 +7,11 @@ import { HTMLProps, ReactNode, SyntheticEvent } from 'react'; | ||
} : Record<string, void>; | ||
export declare type Context<Model> = { | ||
export declare type Context<Model extends UnknownObject> = { | ||
changed: boolean; | ||
changedMap: ChangedMap<Model>; | ||
error: any; | ||
error: unknown; | ||
formRef: BaseForm<Model>; | ||
model: DeepPartial<Model>; | ||
model: Model; | ||
name: string[]; | ||
onChange: (key: string, value: any) => void; | ||
onSubmit: (event?: SyntheticEvent) => any | Promise<any>; | ||
onChange: (key: string, value: unknown) => void; | ||
onSubmit: (event?: SyntheticEvent) => unknown | Promise<unknown>; | ||
randomId: () => string; | ||
@@ -30,6 +30,2 @@ schema: Bridge; | ||
}; | ||
/** @internal */ | ||
export declare type DeepPartial<T> = { | ||
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]; | ||
}; | ||
export declare type FieldProps<Value, Base, Extension = object> = Override<Base, GuaranteedProps<Value> & Extension>; | ||
@@ -41,6 +37,6 @@ export interface FilterDOMProps { | ||
disabled: boolean; | ||
error?: any; | ||
error?: unknown; | ||
errorMessage?: string; | ||
field: any; | ||
fieldType: any; | ||
field: unknown; | ||
fieldType: unknown; | ||
fields: string[]; | ||
@@ -58,3 +54,3 @@ id: string; | ||
(value: Value): void; | ||
(value: any, name: string): void; | ||
(value: unknown, name: string): void; | ||
}; | ||
@@ -65,2 +61,4 @@ export declare type HTMLFieldProps<Value, Element, Extension = object> = FieldProps<Value, HTMLProps<Element>, Extension>; | ||
export declare type Override<T, U> = T extends any ? U & Omit<T, keyof U> : never; | ||
/** @internal */ | ||
export declare type UnknownObject = Record<string, unknown>; | ||
export declare type ValidateMode = 'onChange' | 'onChangeAfterSubmit' | 'onSubmit'; | ||
@@ -67,0 +65,0 @@ declare module '.' { |
@@ -1,5 +0,5 @@ | ||
import { GuaranteedProps } from './types'; | ||
export declare function useField<Props extends Record<string, any>, Value = Props['value'], Model = Record<string, any>>(fieldName: string, props: Props, options?: { | ||
import { GuaranteedProps, UnknownObject } from './types'; | ||
export declare function useField<Props extends Record<string, any>, Value = Props['value'], Model extends UnknownObject = UnknownObject>(fieldName: string, props: Props, options?: { | ||
absoluteName?: boolean; | ||
initialValue?: boolean; | ||
}): [GuaranteedProps<Value> & Props, import("./types").Context<Model>]; |
@@ -41,5 +41,9 @@ "use strict"; | ||
const fields = context.schema.getSubfields(name); | ||
const schemaProps = context.schema.getProps(name, Object.assign(Object.assign({}, state), props)); | ||
const [label, labelFallback] = propagate(props.label, schemaProps.label, state.label, ''); | ||
const [placeholder] = propagate(props.placeholder, schemaProps.placeholder, state.placeholder, label || labelFallback); | ||
const schemaProps = context.schema.getProps(name); | ||
const [label, labelFallback] = propagate(props.label, | ||
// @ts-expect-error The `schema.getProps` should be typed more precisely. | ||
schemaProps.label, state.label, ''); | ||
const [placeholder] = propagate(props.placeholder, | ||
// @ts-expect-error The `schema.getProps` should be typed more precisely. | ||
schemaProps.placeholder, state.placeholder, label || labelFallback); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -53,2 +57,3 @@ const id = react_1.useMemo(() => context.randomId(), []); | ||
[context.onChange, name]); | ||
// @ts-expect-error The `props` should be typed more precisely. | ||
const valueFromModel = get_1.default(context.model, name); | ||
@@ -55,0 +60,0 @@ let initialValue; |
@@ -1,2 +0,2 @@ | ||
import { Context } from './types'; | ||
export declare function useForm<Model>(): Context<Model>; | ||
import { Context, UnknownObject } from './types'; | ||
export declare function useForm<Model extends UnknownObject>(): Context<Model>; |
@@ -0,42 +1,43 @@ | ||
/// <reference types="node" /> | ||
import { SyntheticEvent } from 'react'; | ||
import { BaseForm, BaseFormProps, BaseFormState } from './BaseForm'; | ||
import { Context, DeepPartial, ValidateMode } from './types'; | ||
export declare type ValidatedFormProps<Model> = BaseFormProps<Model> & { | ||
onValidate: (model: DeepPartial<Model>, error: any) => any; | ||
import { Context, UnknownObject, ValidateMode } from './types'; | ||
export declare type ValidatedFormProps<Model extends UnknownObject> = BaseFormProps<Model> & { | ||
onValidate: (model: Model, error: unknown) => unknown; | ||
validate: ValidateMode; | ||
validator?: any; | ||
validator?: unknown; | ||
}; | ||
export declare type ValidatedFormState<Model> = BaseFormState<Model> & { | ||
error: any; | ||
export declare type ValidatedFormState<Model extends UnknownObject> = BaseFormState<Model> & { | ||
error: unknown; | ||
validate: boolean; | ||
validating: boolean; | ||
validator: (model: DeepPartial<Model>) => any; | ||
validator: (model: Model) => unknown; | ||
}; | ||
export declare function Validated<Base extends typeof BaseForm>(Base: Base): { | ||
new <Model, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model extends UnknownObject, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): Context<Model>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -50,5 +51,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: DeepPartial<Model>): DeepPartial<Model>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onReset(): void; | ||
@@ -79,3 +80,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -93,28 +94,28 @@ autosave: boolean; | ||
export declare const ValidatedForm: { | ||
new <Model, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model extends UnknownObject, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): Context<Model>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -128,5 +129,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: DeepPartial<Model>): DeepPartial<Model>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onReset(): void; | ||
@@ -157,3 +158,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -160,0 +161,0 @@ autosave: boolean; |
/// <reference types="react" /> | ||
/// <reference types="node" /> | ||
import { BaseForm } from './BaseForm'; | ||
import { Quick, QuickFormProps, QuickFormState } from './QuickForm'; | ||
import { Validated, ValidatedFormProps, ValidatedFormState } from './ValidatedForm'; | ||
export declare type ValidatedQuickFormProps<Model> = QuickFormProps<Model> & ValidatedFormProps<Model>; | ||
export declare type ValidatedQuickFormState<Model> = QuickFormState<Model> & ValidatedFormState<Model>; | ||
import { UnknownObject } from './types'; | ||
export declare type ValidatedQuickFormProps<Model extends UnknownObject> = QuickFormProps<Model> & ValidatedFormProps<Model>; | ||
export declare type ValidatedQuickFormState<Model extends UnknownObject> = QuickFormState<Model> & ValidatedFormState<Model>; | ||
export declare const ValidatedQuickForm: { | ||
new <Model, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model extends UnknownObject, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): import("./types").DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -42,5 +44,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model>): import("./types").DeepPartial<Model>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onReset(): void; | ||
@@ -71,3 +73,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -84,6 +86,6 @@ autosave: boolean; | ||
} & { | ||
new <Model_1, Props_1 extends QuickFormProps<Model_1> = QuickFormProps<Model_1>, State_1 extends QuickFormState<Model_1> = QuickFormState<Model_1>>(props: Props_1): { | ||
new <Model_1 extends UnknownObject, Props_1 extends QuickFormProps<Model_1> = QuickFormProps<Model_1>, State_1 extends QuickFormState<Model_1> = QuickFormState<Model_1>>(props: Props_1): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -99,12 +101,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model_1>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): import("./types").DeepPartial<Model_1>; | ||
getContextError(): unknown; | ||
getContextModel(): Model_1; | ||
getContextState(): { | ||
@@ -118,9 +120,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model_1>): import("./types").DeepPartial<Model_1>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model_1): Model_1; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State_1): Partial<State_1>; | ||
onReset(): void; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -127,0 +129,0 @@ context: any; |
/// <reference types="react" /> | ||
/// <reference types="node" /> | ||
import { BaseForm } from './BaseForm'; | ||
import { ValidatedQuickForm, ValidatedQuickFormProps, ValidatedQuickFormState } from './ValidatedQuickForm'; | ||
import { DeepPartial, ModelTransformMode } from './types'; | ||
export declare type AutoFormProps<Model> = ValidatedQuickFormProps<Model> & { | ||
onChangeModel?: (model: DeepPartial<Model>) => void; | ||
import { ModelTransformMode, UnknownObject } from './types'; | ||
export declare type AutoFormProps<Model extends UnknownObject> = ValidatedQuickFormProps<Model> & { | ||
onChangeModel?: (model: Model) => void; | ||
}; | ||
export declare type AutoFormState<Model> = ValidatedQuickFormState<Model> & { | ||
model: DeepPartial<Model>; | ||
export declare type AutoFormState<Model extends UnknownObject> = ValidatedQuickFormState<Model> & { | ||
model: Model; | ||
}; | ||
export declare function Auto<Base extends typeof ValidatedQuickForm>(Base: Base): { | ||
new <Model, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
getModel(mode: ModelTransformMode): State["model"]; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -47,4 +48,4 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
onReset(): void; | ||
@@ -76,3 +77,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -99,29 +100,29 @@ autosave: boolean; | ||
export declare const AutoForm: { | ||
new <Model, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends AutoFormProps<Model> = AutoFormProps<Model>, State extends AutoFormState<Model> = AutoFormState<Model>>(props: Props): { | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
getModel(mode: ModelTransformMode): State["model"]; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -135,4 +136,4 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
onReset(): void; | ||
@@ -164,3 +165,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -186,28 +187,28 @@ autosave: boolean; | ||
} & { | ||
new <Model_1, Props_1 extends import("./ValidatedForm").ValidatedFormProps<Model_1> = import("./ValidatedForm").ValidatedFormProps<Model_1>, State_1 extends import("./ValidatedForm").ValidatedFormState<Model_1> = import("./ValidatedForm").ValidatedFormState<Model_1>>(props: Props_1): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model_1 extends UnknownObject, Props_1 extends import("./ValidatedForm").ValidatedFormProps<Model_1> = import("./ValidatedForm").ValidatedFormProps<Model_1>, State_1 extends import("./ValidatedForm").ValidatedFormState<Model_1> = import("./ValidatedForm").ValidatedFormState<Model_1>>(props: Props_1): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model_1>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props_1, prevState: State_1, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State_1): Partial<State_1>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props_1["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props_1["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model_1>; | ||
getContextModel(): Model_1; | ||
getContextState(): { | ||
@@ -221,5 +222,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: ModelTransformMode | undefined, model?: DeepPartial<Model_1>): DeepPartial<Model_1>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: ModelTransformMode | undefined, model?: Model_1): Model_1; | ||
onReset(): void; | ||
@@ -250,3 +251,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -263,6 +264,6 @@ autosave: boolean; | ||
} & { | ||
new <Model_2, Props_2 extends import("./QuickForm").QuickFormProps<Model_2> = import("./QuickForm").QuickFormProps<Model_2>, State_2 extends import("./QuickForm").QuickFormState<Model_2> = import("./QuickForm").QuickFormState<Model_2>>(props: Props_2): { | ||
new <Model_2 extends UnknownObject, Props_2 extends import("./QuickForm").QuickFormProps<Model_2> = import("./QuickForm").QuickFormProps<Model_2>, State_2 extends import("./QuickForm").QuickFormState<Model_2> = import("./QuickForm").QuickFormState<Model_2>>(props: Props_2): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -278,12 +279,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model_2>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): DeepPartial<Model_2>; | ||
getContextError(): unknown; | ||
getContextModel(): Model_2; | ||
getContextState(): { | ||
@@ -297,9 +298,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: ModelTransformMode | undefined, model?: DeepPartial<Model_2>): DeepPartial<Model_2>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: ModelTransformMode | undefined, model?: Model_2): Model_2; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State_2): Partial<State_2>; | ||
onReset(): void; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -306,0 +307,0 @@ context: any; |
import { Component, SyntheticEvent } from 'react'; | ||
import { Bridge } from './Bridge'; | ||
import { ChangedMap, Context, DeepPartial, ModelTransformMode } from './types'; | ||
export declare type BaseFormProps<Model> = { | ||
import { ChangedMap, Context, ModelTransformMode, UnknownObject } from './types'; | ||
export declare type BaseFormProps<Model extends UnknownObject> = { | ||
autosave: boolean; | ||
autosaveDelay: number; | ||
disabled?: boolean; | ||
error: any; | ||
error: unknown; | ||
id?: string; | ||
label: boolean; | ||
model: DeepPartial<Model>; | ||
modelTransform?: (mode: ModelTransformMode, model: DeepPartial<Model>) => DeepPartial<Model>; | ||
model: Model; | ||
modelTransform?: (mode: ModelTransformMode, model: Model) => Model; | ||
noValidate: boolean; | ||
onChange?: (key: string, value: any) => void; | ||
onSubmit: (model: DeepPartial<Model>) => void | Promise<any>; | ||
onChange?: (key: string, value: unknown) => void; | ||
onSubmit: (model: Model) => void | Promise<unknown>; | ||
placeholder?: boolean; | ||
@@ -21,3 +21,3 @@ readOnly?: boolean; | ||
}; | ||
export declare type BaseFormState<Model> = { | ||
export declare type BaseFormState<Model extends UnknownObject> = { | ||
changed: boolean; | ||
@@ -29,3 +29,3 @@ changedMap: ChangedMap<Model>; | ||
}; | ||
export declare class BaseForm<Model, Props extends BaseFormProps<Model> = BaseFormProps<Model>, State extends BaseFormState<Model> = BaseFormState<Model>> extends Component<Props, State> { | ||
export declare class BaseForm<Model extends UnknownObject, Props extends BaseFormProps<Model> = BaseFormProps<Model>, State extends BaseFormState<Model> = BaseFormState<Model>> extends Component<Props, State> { | ||
static displayName: string; | ||
@@ -45,7 +45,7 @@ static defaultProps: { | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: ReturnType<typeof setTimeout> | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: SyntheticEvent) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: SyntheticEvent) => Promise<unknown>; | ||
randomId: () => string; | ||
@@ -60,3 +60,3 @@ getContext(): Context<Model>; | ||
getContextOnSubmit(): Context<Model>['onSubmit']; | ||
getModel(mode?: ModelTransformMode, model?: DeepPartial<Model>): Context<Model>['model']; | ||
getModel(mode?: ModelTransformMode, model?: Model): Context<Model>['model']; | ||
getNativeFormProps(): { | ||
@@ -67,7 +67,7 @@ [key: string]: unknown; | ||
}; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onReset(): void; | ||
onSubmit(event?: SyntheticEvent): Promise<any>; | ||
onSubmit(event?: SyntheticEvent): Promise<unknown>; | ||
render(): JSX.Element; | ||
} |
@@ -143,3 +143,3 @@ import clone from 'lodash/clone'; | ||
if (this.delayId) { | ||
this.delayId = clearTimeout(this.delayId); | ||
clearTimeout(this.delayId); | ||
} | ||
@@ -146,0 +146,0 @@ // Delay autosave by `autosaveDelay` milliseconds... |
@@ -1,16 +0,59 @@ | ||
declare type Error = any; | ||
declare type Field = any; | ||
declare type FieldType = any; | ||
import { UnknownObject } from './types'; | ||
export declare abstract class Bridge { | ||
constructor(...args: any[]); | ||
getError(name: string, error: Error): Error; | ||
getErrorMessage(name: string, error: Error): string; | ||
getErrorMessages(error: Error): string[]; | ||
getField(name: string): Field; | ||
getInitialValue(name: string): any; | ||
getProps(name: string, props: Record<string, any>): Record<string, any>; | ||
/** | ||
* Get an error for field `name` out of `error`. There is no standarized | ||
* format, but fields treat truthy values as a sign of being invalid. Fields | ||
* receive this as a `error` guaranteed prop. | ||
*/ | ||
getError(name: string, error: unknown): unknown; | ||
/** | ||
* Get an error message for field `name` out of `error`. If there is no error, | ||
* return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
* prop. | ||
*/ | ||
getErrorMessage(name: string, error: unknown): string; | ||
/** | ||
* Get all error messages from `error`. Only `ErrorsField` make use of that | ||
* (in builtin themes). | ||
*/ | ||
getErrorMessages(error: unknown): string[]; | ||
/** | ||
* Get internal field definition for field `name`. Fields receive this as a | ||
* `field` guaranteed prop. There is no standarized field format. Most bridges | ||
* use it as a common object, used in calculation of initial values, props, | ||
* and types. | ||
*/ | ||
getField(name: string): unknown; | ||
/** | ||
* Get initial value of field `name`. It is used as a default when no value is | ||
* set (e.g. the form is rendered with an empty `model`). Additionally, | ||
* `props` are this field instance props. If a field is rendered multiple | ||
* times, this function will be called multiple times, possibly with different | ||
* `props`. | ||
*/ | ||
getInitialValue(name: string): unknown; | ||
/** | ||
* Get props defined in schema for a field `name`. There are no required nor | ||
* banned fields, however properties like `required` are often available. | ||
*/ | ||
getProps(name: string): UnknownObject; | ||
/** | ||
* Get a list of subfields of field `name` or top-level fields, if no `name` | ||
* is passed. | ||
*/ | ||
getSubfields(name?: string): string[]; | ||
getType(name: string): FieldType; | ||
getValidator(options?: any): (model: Record<string, any>) => null | Error | Promise<null | Error>; | ||
/** | ||
* There is no standarized field type format. However, `AutoField` component | ||
* will work correctly only with standard JavaScript constructors, like | ||
* `String` or `Number`. | ||
*/ | ||
getType(name: string): unknown; | ||
/** | ||
* Get a validator function. The `options` here are from the `validator` prop | ||
* of the form. A validator function receives a model and returns an error or | ||
* a promise that will resolve (not reject!) with an error. If there is no | ||
* error, return (or resolve with) a `null` value instead. | ||
*/ | ||
getValidator(options?: unknown): (model: UnknownObject) => unknown; | ||
} | ||
export {}; |
@@ -7,54 +7,72 @@ import invariant from 'invariant'; | ||
} | ||
// Get an error for field `name` out of `error`. There is no standarized | ||
// format, but fields treat truthy values as a sign of being invalid. Fields | ||
// receive this as a `error` guaranteed prop. | ||
/** | ||
* Get an error for field `name` out of `error`. There is no standarized | ||
* format, but fields treat truthy values as a sign of being invalid. Fields | ||
* receive this as a `error` guaranteed prop. | ||
*/ | ||
getError(name, error) { | ||
return invariant(false, '%s have not implemented `getError` method (args=%o).', this.constructor.name, { name, error }); | ||
} | ||
// Get an error message for field `name` out of `error`. If there is no error, | ||
// return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
// prop. | ||
/** | ||
* Get an error message for field `name` out of `error`. If there is no error, | ||
* return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
* prop. | ||
*/ | ||
getErrorMessage(name, error) { | ||
return invariant(false, '%s have not implemented `getErrorMessage` method (args=%o).', this.constructor.name, { name, error }); | ||
} | ||
// Get all error messages from `error`. Only `ErrorsField` make use of that | ||
// (in builtin themes). | ||
/** | ||
* Get all error messages from `error`. Only `ErrorsField` make use of that | ||
* (in builtin themes). | ||
*/ | ||
getErrorMessages(error) { | ||
return invariant(false, '%s have not implemented `getErrorMessages` method (args=%o).', this.constructor.name, { error }); | ||
} | ||
// Get internal field definition for field `name`. Fields receive this as a | ||
// `field` guaranteed prop. | ||
/** | ||
* Get internal field definition for field `name`. Fields receive this as a | ||
* `field` guaranteed prop. There is no standarized field format. Most bridges | ||
* use it as a common object, used in calculation of initial values, props, | ||
* and types. | ||
*/ | ||
getField(name) { | ||
return invariant(false, '%s have not implemented `getField` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get initial value of field `name`. It is used as a default when no value is | ||
// set (e.g. the form is rendered with an empty `model`). Additionally, | ||
// `props` are this field instance props. If a field is rendered multiple | ||
// times, this function will be called multiple times, possibly with different | ||
// `props`. | ||
/** | ||
* Get initial value of field `name`. It is used as a default when no value is | ||
* set (e.g. the form is rendered with an empty `model`). Additionally, | ||
* `props` are this field instance props. If a field is rendered multiple | ||
* times, this function will be called multiple times, possibly with different | ||
* `props`. | ||
*/ | ||
getInitialValue(name) { | ||
return invariant(false, '%s have not implemented `getInitialValue` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get props defined in schema for a field `name`. There are no required nor | ||
// banned fields, however properties like `required` are often available. | ||
// Additionally, `props` are this field instance props. If a field is rendered | ||
// multiple times, this function will be called multiple times, possibly with | ||
// different `props`. | ||
getProps(name, props) { | ||
return invariant(false, '%s have not implemented `getProps` method (args=%o).', this.constructor.name, { name, props }); | ||
/** | ||
* Get props defined in schema for a field `name`. There are no required nor | ||
* banned fields, however properties like `required` are often available. | ||
*/ | ||
getProps(name) { | ||
return invariant(false, '%s have not implemented `getProps` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get a list of subfields of field `name` or top-level fields, if no `name` | ||
// is passed. | ||
/** | ||
* Get a list of subfields of field `name` or top-level fields, if no `name` | ||
* is passed. | ||
*/ | ||
getSubfields(name) { | ||
return invariant(false, '%s have not implemented `getSubfields` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get a type of field `name`. See `FieldTypeType` for details. | ||
/** | ||
* There is no standarized field type format. However, `AutoField` component | ||
* will work correctly only with standard JavaScript constructors, like | ||
* `String` or `Number`. | ||
*/ | ||
getType(name) { | ||
return invariant(false, '%s have not implemented `getType` method (args=%o).', this.constructor.name, { name }); | ||
} | ||
// Get a validator function. The `options` here are from the `validator` prop | ||
// of the form. A validator function receives a model and returns an error or | ||
// a promise that will resolve (not reject!) with an error. If there is no | ||
// error, return (or resolve with) a `null` value instead. | ||
// eslint-disable-next-line prettier/prettier | ||
/** | ||
* Get a validator function. The `options` here are from the `validator` prop | ||
* of the form. A validator function receives a model and returns an error or | ||
* a promise that will resolve (not reject!) with an error. If there is no | ||
* error, return (or resolve with) a `null` value instead. | ||
*/ | ||
getValidator(options) { | ||
@@ -61,0 +79,0 @@ return invariant(false, '%s have not implemented `getValidator` method (args=%o).', this.constructor.name, { options }); |
import { ComponentType, FunctionComponent } from 'react'; | ||
import { GuaranteedProps, Override } from './types'; | ||
import { GuaranteedProps, Override, UnknownObject } from './types'; | ||
/** @internal */ | ||
@@ -9,3 +9,3 @@ export declare type ConnectFieldOptions = { | ||
/** @internal */ | ||
export declare type ConnectedFieldProps<Props extends Record<string, unknown>, Value = Props['value']> = Override<Props, Override<Partial<GuaranteedProps<Value>>, { | ||
export declare type ConnectedFieldProps<Props extends UnknownObject, Value = Props['value']> = Override<Props, Override<Partial<GuaranteedProps<Value>>, { | ||
label?: Props['label'] | boolean | null | string; | ||
@@ -16,6 +16,6 @@ name: string; | ||
/** @internal */ | ||
export declare type ConnectedField<Props extends Record<string, unknown>, Value = Props['value']> = FunctionComponent<ConnectedFieldProps<Props, Value>> & { | ||
export declare type ConnectedField<Props extends UnknownObject, Value = Props['value']> = FunctionComponent<ConnectedFieldProps<Props, Value>> & { | ||
Component: ComponentType<Props>; | ||
options?: ConnectFieldOptions; | ||
}; | ||
export declare function connectField<Props extends Record<string, unknown>, Value = Props['value']>(Component: ComponentType<Props>, options?: ConnectFieldOptions): ConnectedField<Props, Value>; | ||
export declare function connectField<Props extends UnknownObject, Value = Props['value']>(Component: ComponentType<Props>, options?: ConnectFieldOptions): ConnectedField<Props, Value>; |
import { ComponentType, ReactElement } from 'react'; | ||
import { connectField } from './connectField'; | ||
import { Context } from './types'; | ||
import { Context, UnknownObject } from './types'; | ||
import { useField } from './useField'; | ||
export declare type AutoFieldProps = { | ||
export declare type AutoFieldProps = UnknownObject & { | ||
component?: Component; | ||
experimental_absoluteName?: boolean; | ||
name: string; | ||
[prop: string]: unknown; | ||
}; | ||
@@ -13,6 +13,6 @@ /** @internal */ | ||
/** @internal */ | ||
export declare type ComponentDetector = (props: ReturnType<typeof useField>[0], uniforms: Context<Record<string, unknown>>) => Component; | ||
export declare function createAutoField(defaultComponentDetector: ComponentDetector): ((rawProps: AutoFieldProps) => ReactElement) & { | ||
export declare type ComponentDetector = (props: ReturnType<typeof useField>[0], uniforms: Context<UnknownObject>) => Component; | ||
export declare function createAutoField(defaultComponentDetector: ComponentDetector): (({ experimental_absoluteName: absoluteName, ...rawProps }: AutoFieldProps) => ReactElement) & { | ||
componentDetectorContext: import("react").Context<ComponentDetector>; | ||
defaultComponentDetector: ComponentDetector; | ||
}; |
@@ -0,1 +1,2 @@ | ||
import { __rest } from "tslib"; | ||
import invariant from 'invariant'; | ||
@@ -6,9 +7,11 @@ import { createContext, createElement, useContext, } from 'react'; | ||
const context = createContext(defaultComponentDetector); | ||
function AutoField(rawProps) { | ||
var _a; | ||
const [props, uniforms] = useField(rawProps.name, rawProps); | ||
function AutoField(_a) { | ||
var _b; | ||
var { experimental_absoluteName: absoluteName } = _a, rawProps = __rest(_a, ["experimental_absoluteName"]); | ||
const options = { absoluteName }; | ||
const [props, uniforms] = useField(rawProps.name, rawProps, options); | ||
const componentDetector = useContext(context); | ||
const component = componentDetector(props, uniforms); | ||
invariant(component, 'AutoField received no component for: %s', props.name); | ||
return 'options' in component && ((_a = component.options) === null || _a === void 0 ? void 0 : _a.kind) === 'leaf' | ||
return 'options' in component && ((_b = component.options) === null || _b === void 0 ? void 0 : _b.kind) === 'leaf' | ||
? createElement(component.Component, props) | ||
@@ -15,0 +18,0 @@ : createElement(component, rawProps); |
@@ -0,13 +1,15 @@ | ||
/// <reference types="node" /> | ||
import React, { ComponentType } from 'react'; | ||
import { BaseForm, BaseFormProps, BaseFormState } from './BaseForm'; | ||
export declare type QuickFormProps<Model> = BaseFormProps<Model> & { | ||
import { UnknownObject } from './types'; | ||
export declare type QuickFormProps<Model extends UnknownObject> = BaseFormProps<Model> & { | ||
errorsField?: ComponentType; | ||
submitField?: ComponentType; | ||
}; | ||
export declare type QuickFormState<Model> = BaseFormState<Model>; | ||
export declare type QuickFormState<Model extends UnknownObject> = BaseFormState<Model>; | ||
export declare function Quick<Base extends typeof BaseForm>(Base: Base): { | ||
new <Model, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -23,12 +25,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): import("./types").DeepPartial<Model>; | ||
getContextError(): unknown; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -42,9 +44,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model>): import("./types").DeepPartial<Model>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onReset(): void; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -85,6 +87,6 @@ context: any; | ||
export declare const QuickForm: { | ||
new <Model, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
new <Model extends UnknownObject, Props extends QuickFormProps<Model> = QuickFormProps<Model>, State extends QuickFormState<Model> = QuickFormState<Model>>(props: Props): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -100,12 +102,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: React.SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): import("./types").DeepPartial<Model>; | ||
getContextError(): unknown; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -119,9 +121,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model>): import("./types").DeepPartial<Model>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: React.SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onReset(): void; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: React.SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -128,0 +130,0 @@ context: any; |
@@ -7,11 +7,11 @@ import { HTMLProps, ReactNode, SyntheticEvent } from 'react'; | ||
} : Record<string, void>; | ||
export declare type Context<Model> = { | ||
export declare type Context<Model extends UnknownObject> = { | ||
changed: boolean; | ||
changedMap: ChangedMap<Model>; | ||
error: any; | ||
error: unknown; | ||
formRef: BaseForm<Model>; | ||
model: DeepPartial<Model>; | ||
model: Model; | ||
name: string[]; | ||
onChange: (key: string, value: any) => void; | ||
onSubmit: (event?: SyntheticEvent) => any | Promise<any>; | ||
onChange: (key: string, value: unknown) => void; | ||
onSubmit: (event?: SyntheticEvent) => unknown | Promise<unknown>; | ||
randomId: () => string; | ||
@@ -30,6 +30,2 @@ schema: Bridge; | ||
}; | ||
/** @internal */ | ||
export declare type DeepPartial<T> = { | ||
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]; | ||
}; | ||
export declare type FieldProps<Value, Base, Extension = object> = Override<Base, GuaranteedProps<Value> & Extension>; | ||
@@ -41,6 +37,6 @@ export interface FilterDOMProps { | ||
disabled: boolean; | ||
error?: any; | ||
error?: unknown; | ||
errorMessage?: string; | ||
field: any; | ||
fieldType: any; | ||
field: unknown; | ||
fieldType: unknown; | ||
fields: string[]; | ||
@@ -58,3 +54,3 @@ id: string; | ||
(value: Value): void; | ||
(value: any, name: string): void; | ||
(value: unknown, name: string): void; | ||
}; | ||
@@ -65,2 +61,4 @@ export declare type HTMLFieldProps<Value, Element, Extension = object> = FieldProps<Value, HTMLProps<Element>, Extension>; | ||
export declare type Override<T, U> = T extends any ? U & Omit<T, keyof U> : never; | ||
/** @internal */ | ||
export declare type UnknownObject = Record<string, unknown>; | ||
export declare type ValidateMode = 'onChange' | 'onChangeAfterSubmit' | 'onSubmit'; | ||
@@ -67,0 +65,0 @@ declare module '.' { |
@@ -1,5 +0,5 @@ | ||
import { GuaranteedProps } from './types'; | ||
export declare function useField<Props extends Record<string, any>, Value = Props['value'], Model = Record<string, any>>(fieldName: string, props: Props, options?: { | ||
import { GuaranteedProps, UnknownObject } from './types'; | ||
export declare function useField<Props extends Record<string, any>, Value = Props['value'], Model extends UnknownObject = UnknownObject>(fieldName: string, props: Props, options?: { | ||
absoluteName?: boolean; | ||
initialValue?: boolean; | ||
}): [GuaranteedProps<Value> & Props, import("./types").Context<Model>]; |
@@ -37,5 +37,9 @@ import get from 'lodash/get'; | ||
const fields = context.schema.getSubfields(name); | ||
const schemaProps = context.schema.getProps(name, Object.assign(Object.assign({}, state), props)); | ||
const [label, labelFallback] = propagate(props.label, schemaProps.label, state.label, ''); | ||
const [placeholder] = propagate(props.placeholder, schemaProps.placeholder, state.placeholder, label || labelFallback); | ||
const schemaProps = context.schema.getProps(name); | ||
const [label, labelFallback] = propagate(props.label, | ||
// @ts-expect-error The `schema.getProps` should be typed more precisely. | ||
schemaProps.label, state.label, ''); | ||
const [placeholder] = propagate(props.placeholder, | ||
// @ts-expect-error The `schema.getProps` should be typed more precisely. | ||
schemaProps.placeholder, state.placeholder, label || labelFallback); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
@@ -49,2 +53,3 @@ const id = useMemo(() => context.randomId(), []); | ||
[context.onChange, name]); | ||
// @ts-expect-error The `props` should be typed more precisely. | ||
const valueFromModel = get(context.model, name); | ||
@@ -51,0 +56,0 @@ let initialValue; |
@@ -1,2 +0,2 @@ | ||
import { Context } from './types'; | ||
export declare function useForm<Model>(): Context<Model>; | ||
import { Context, UnknownObject } from './types'; | ||
export declare function useForm<Model extends UnknownObject>(): Context<Model>; |
@@ -0,42 +1,43 @@ | ||
/// <reference types="node" /> | ||
import { SyntheticEvent } from 'react'; | ||
import { BaseForm, BaseFormProps, BaseFormState } from './BaseForm'; | ||
import { Context, DeepPartial, ValidateMode } from './types'; | ||
export declare type ValidatedFormProps<Model> = BaseFormProps<Model> & { | ||
onValidate: (model: DeepPartial<Model>, error: any) => any; | ||
import { Context, UnknownObject, ValidateMode } from './types'; | ||
export declare type ValidatedFormProps<Model extends UnknownObject> = BaseFormProps<Model> & { | ||
onValidate: (model: Model, error: unknown) => unknown; | ||
validate: ValidateMode; | ||
validator?: any; | ||
validator?: unknown; | ||
}; | ||
export declare type ValidatedFormState<Model> = BaseFormState<Model> & { | ||
error: any; | ||
export declare type ValidatedFormState<Model extends UnknownObject> = BaseFormState<Model> & { | ||
error: unknown; | ||
validate: boolean; | ||
validating: boolean; | ||
validator: (model: DeepPartial<Model>) => any; | ||
validator: (model: Model) => unknown; | ||
}; | ||
export declare function Validated<Base extends typeof BaseForm>(Base: Base): { | ||
new <Model, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model extends UnknownObject, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): Context<Model>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -50,5 +51,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: DeepPartial<Model>): DeepPartial<Model>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onReset(): void; | ||
@@ -79,3 +80,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -93,28 +94,28 @@ autosave: boolean; | ||
export declare const ValidatedForm: { | ||
new <Model, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model extends UnknownObject, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): Context<Model>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -128,5 +129,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: DeepPartial<Model>): DeepPartial<Model>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onReset(): void; | ||
@@ -157,3 +158,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -160,0 +161,0 @@ autosave: boolean; |
/// <reference types="react" /> | ||
/// <reference types="node" /> | ||
import { BaseForm } from './BaseForm'; | ||
import { Quick, QuickFormProps, QuickFormState } from './QuickForm'; | ||
import { Validated, ValidatedFormProps, ValidatedFormState } from './ValidatedForm'; | ||
export declare type ValidatedQuickFormProps<Model> = QuickFormProps<Model> & ValidatedFormProps<Model>; | ||
export declare type ValidatedQuickFormState<Model> = QuickFormState<Model> & ValidatedFormState<Model>; | ||
import { UnknownObject } from './types'; | ||
export declare type ValidatedQuickFormProps<Model extends UnknownObject> = QuickFormProps<Model> & ValidatedFormProps<Model>; | ||
export declare type ValidatedQuickFormState<Model extends UnknownObject> = QuickFormState<Model> & ValidatedFormState<Model>; | ||
export declare const ValidatedQuickForm: { | ||
new <Model, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: any) => Promise<any>; | ||
validateModel: (originalModel: any) => Promise<any>; | ||
getContextError(): any; | ||
new <Model extends UnknownObject, Props extends ValidatedFormProps<Model> = ValidatedFormProps<Model>, State extends ValidatedFormState<Model> = ValidatedFormState<Model>>(props: Props): { | ||
validate: (key?: string | undefined, value?: unknown) => Promise<unknown>; | ||
validateModel: (originalModel: any) => Promise<unknown>; | ||
getContextError(): unknown; | ||
getContext(): import("./types").Context<Model>; | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
}; | ||
componentDidUpdate(prevProps: Props, prevState: State, snapshot: never): void; | ||
onChange(key: string, value: any): void; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State): Partial<State>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onValidate(key?: string | undefined, value?: any): Promise<any>; | ||
onValidateModel(originalModel: Props["model"]): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
onValidate(key?: string | undefined, value?: unknown): Promise<unknown>; | ||
onValidateModel(originalModel: Props["model"]): Promise<unknown>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContextName(): string[]; | ||
getContextModel(): import("./types").DeepPartial<Model>; | ||
getContextModel(): Model; | ||
getContextState(): { | ||
@@ -42,5 +44,5 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model>): import("./types").DeepPartial<Model>; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model): Model; | ||
onReset(): void; | ||
@@ -71,3 +73,3 @@ render(): JSX.Element; | ||
defaultProps: { | ||
onValidate(model: unknown, error: any): any; | ||
onValidate(model: unknown, error: unknown): unknown; | ||
validate: string; | ||
@@ -84,6 +86,6 @@ autosave: boolean; | ||
} & { | ||
new <Model_1, Props_1 extends QuickFormProps<Model_1> = QuickFormProps<Model_1>, State_1 extends QuickFormState<Model_1> = QuickFormState<Model_1>>(props: Props_1): { | ||
new <Model_1 extends UnknownObject, Props_1 extends QuickFormProps<Model_1> = QuickFormProps<Model_1>, State_1 extends QuickFormState<Model_1> = QuickFormState<Model_1>>(props: Props_1): { | ||
getNativeFormProps(): { | ||
[key: string]: unknown; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
onSubmit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
key: string; | ||
@@ -99,12 +101,12 @@ }; | ||
componentWillUnmount(): void; | ||
delayId?: any; | ||
delayId?: NodeJS.Timeout | undefined; | ||
mounted: boolean; | ||
reset: () => void; | ||
change: (key: string, value: any) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<any>; | ||
change: (key: string, value: unknown) => void; | ||
submit: (event?: import("react").SyntheticEvent<Element, Event> | undefined) => Promise<unknown>; | ||
randomId: () => string; | ||
getContext(): import("./types").Context<Model_1>; | ||
getContextName(): string[]; | ||
getContextError(): any; | ||
getContextModel(): import("./types").DeepPartial<Model_1>; | ||
getContextError(): unknown; | ||
getContextModel(): Model_1; | ||
getContextState(): { | ||
@@ -118,9 +120,9 @@ disabled: boolean; | ||
getContextSchema(): import("./Bridge").Bridge; | ||
getContextOnChange(): (key: string, value: any) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => any; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: import("./types").DeepPartial<Model_1>): import("./types").DeepPartial<Model_1>; | ||
onChange(key: string, value: any): void; | ||
getContextOnChange(): (key: string, value: unknown) => void; | ||
getContextOnSubmit(): (event?: import("react").SyntheticEvent<Element, Event> | undefined) => unknown; | ||
getModel(mode?: import("./types").ModelTransformMode | undefined, model?: Model_1): Model_1; | ||
onChange(key: string, value: unknown): void; | ||
__reset(state: State_1): Partial<State_1>; | ||
onReset(): void; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<any>; | ||
onSubmit(event?: import("react").SyntheticEvent<Element, Event> | undefined): Promise<unknown>; | ||
render(): JSX.Element; | ||
@@ -127,0 +129,0 @@ context: any; |
{ | ||
"name": "uniforms", | ||
"version": "4.0.0-alpha.0", | ||
"version": "4.0.0-alpha.1", | ||
"license": "MIT", | ||
@@ -44,3 +44,3 @@ "main": "./cjs/index.js", | ||
}, | ||
"gitHead": "94084d3c005b7338d27ed206b67a5a43abba357e" | ||
"gitHead": "482d8993324be0eca6ec5717bf592c66050caa91" | ||
} |
import invariant from 'invariant'; | ||
// There is no standarized error format. While creating a custom bridge, one can | ||
// come up with an own error format, returned from validator and later analyzed | ||
// in `getError*` methods. | ||
type Error = any; | ||
import { UnknownObject } from './types'; | ||
// There is no standarized field format. Most bridges use it as a common object, | ||
// used in calculation of initial values, props, and types. | ||
type Field = any; | ||
// There is no standarized field type format. However, `AutoField` component | ||
// will work correctly only with standard JavaScript constructors, like `String` | ||
// or `Number`. | ||
type FieldType = any; | ||
export abstract class Bridge { | ||
@@ -27,6 +15,8 @@ // Each bridge can have a different set of parameters. | ||
// Get an error for field `name` out of `error`. There is no standarized | ||
// format, but fields treat truthy values as a sign of being invalid. Fields | ||
// receive this as a `error` guaranteed prop. | ||
getError(name: string, error: Error): Error { | ||
/** | ||
* Get an error for field `name` out of `error`. There is no standarized | ||
* format, but fields treat truthy values as a sign of being invalid. Fields | ||
* receive this as a `error` guaranteed prop. | ||
*/ | ||
getError(name: string, error: unknown): unknown { | ||
return invariant( | ||
@@ -40,6 +30,8 @@ false, | ||
// Get an error message for field `name` out of `error`. If there is no error, | ||
// return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
// prop. | ||
getErrorMessage(name: string, error: Error): string { | ||
/** | ||
* Get an error message for field `name` out of `error`. If there is no error, | ||
* return an empty string. Fields receive this as a `errorMessage` guaranteed | ||
* prop. | ||
*/ | ||
getErrorMessage(name: string, error: unknown): string { | ||
return invariant( | ||
@@ -53,5 +45,7 @@ false, | ||
// Get all error messages from `error`. Only `ErrorsField` make use of that | ||
// (in builtin themes). | ||
getErrorMessages(error: Error): string[] { | ||
/** | ||
* Get all error messages from `error`. Only `ErrorsField` make use of that | ||
* (in builtin themes). | ||
*/ | ||
getErrorMessages(error: unknown): string[] { | ||
return invariant( | ||
@@ -65,5 +59,9 @@ false, | ||
// Get internal field definition for field `name`. Fields receive this as a | ||
// `field` guaranteed prop. | ||
getField(name: string): Field { | ||
/** | ||
* Get internal field definition for field `name`. Fields receive this as a | ||
* `field` guaranteed prop. There is no standarized field format. Most bridges | ||
* use it as a common object, used in calculation of initial values, props, | ||
* and types. | ||
*/ | ||
getField(name: string): unknown { | ||
return invariant( | ||
@@ -77,8 +75,10 @@ false, | ||
// Get initial value of field `name`. It is used as a default when no value is | ||
// set (e.g. the form is rendered with an empty `model`). Additionally, | ||
// `props` are this field instance props. If a field is rendered multiple | ||
// times, this function will be called multiple times, possibly with different | ||
// `props`. | ||
getInitialValue(name: string): any { | ||
/** | ||
* Get initial value of field `name`. It is used as a default when no value is | ||
* set (e.g. the form is rendered with an empty `model`). Additionally, | ||
* `props` are this field instance props. If a field is rendered multiple | ||
* times, this function will be called multiple times, possibly with different | ||
* `props`. | ||
*/ | ||
getInitialValue(name: string): unknown { | ||
return invariant( | ||
@@ -92,8 +92,7 @@ false, | ||
// Get props defined in schema for a field `name`. There are no required nor | ||
// banned fields, however properties like `required` are often available. | ||
// Additionally, `props` are this field instance props. If a field is rendered | ||
// multiple times, this function will be called multiple times, possibly with | ||
// different `props`. | ||
getProps(name: string, props: Record<string, any>): Record<string, any> { | ||
/** | ||
* Get props defined in schema for a field `name`. There are no required nor | ||
* banned fields, however properties like `required` are often available. | ||
*/ | ||
getProps(name: string): UnknownObject { | ||
return invariant( | ||
@@ -103,8 +102,10 @@ false, | ||
this.constructor.name, | ||
{ name, props }, | ||
{ name }, | ||
); | ||
} | ||
// Get a list of subfields of field `name` or top-level fields, if no `name` | ||
// is passed. | ||
/** | ||
* Get a list of subfields of field `name` or top-level fields, if no `name` | ||
* is passed. | ||
*/ | ||
getSubfields(name?: string): string[] { | ||
@@ -119,4 +120,8 @@ return invariant( | ||
// Get a type of field `name`. See `FieldTypeType` for details. | ||
getType(name: string): FieldType { | ||
/** | ||
* There is no standarized field type format. However, `AutoField` component | ||
* will work correctly only with standard JavaScript constructors, like | ||
* `String` or `Number`. | ||
*/ | ||
getType(name: string): unknown { | ||
return invariant( | ||
@@ -130,8 +135,9 @@ false, | ||
// Get a validator function. The `options` here are from the `validator` prop | ||
// of the form. A validator function receives a model and returns an error or | ||
// a promise that will resolve (not reject!) with an error. If there is no | ||
// error, return (or resolve with) a `null` value instead. | ||
// eslint-disable-next-line prettier/prettier | ||
getValidator(options?: any): (model: Record<string, any>) => null | Error | Promise<null | Error> { | ||
/** | ||
* Get a validator function. The `options` here are from the `validator` prop | ||
* of the form. A validator function receives a model and returns an error or | ||
* a promise that will resolve (not reject!) with an error. If there is no | ||
* error, return (or resolve with) a `null` value instead. | ||
*/ | ||
getValidator(options?: unknown): (model: UnknownObject) => unknown { | ||
return invariant( | ||
@@ -138,0 +144,0 @@ false, |
import isEqual from 'lodash/isEqual'; | ||
import { joinName } from './joinName'; | ||
import { UnknownObject } from './types'; | ||
function isObject(value: unknown): value is Record<string, unknown> { | ||
function isObject(value: unknown): value is UnknownObject { | ||
return !!value && value === Object(value) && !(value instanceof Date); | ||
@@ -7,0 +8,0 @@ } |
@@ -10,11 +10,11 @@ import { HTMLProps, ReactNode, SyntheticEvent } from 'react'; | ||
export type Context<Model> = { | ||
export type Context<Model extends UnknownObject> = { | ||
changed: boolean; | ||
changedMap: ChangedMap<Model>; | ||
error: any; | ||
error: unknown; | ||
formRef: BaseForm<Model>; | ||
model: DeepPartial<Model>; | ||
model: Model; | ||
name: string[]; | ||
onChange: (key: string, value: any) => void; | ||
onSubmit: (event?: SyntheticEvent) => any | Promise<any>; | ||
onChange: (key: string, value: unknown) => void; | ||
onSubmit: (event?: SyntheticEvent) => unknown | Promise<unknown>; | ||
randomId: () => string; | ||
@@ -34,7 +34,2 @@ schema: Bridge; | ||
/** @internal */ | ||
export type DeepPartial<T> = { | ||
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]; | ||
}; | ||
export type FieldProps<Value, Base, Extension = object> = Override< | ||
@@ -51,6 +46,6 @@ Base, | ||
disabled: boolean; | ||
error?: any; | ||
error?: unknown; | ||
errorMessage?: string; | ||
field: any; | ||
fieldType: any; | ||
field: unknown; | ||
fieldType: unknown; | ||
fields: string[]; | ||
@@ -69,3 +64,3 @@ id: string; | ||
(value: Value): void; | ||
(value: any, name: string): void; | ||
(value: unknown, name: string): void; | ||
}; | ||
@@ -84,2 +79,6 @@ | ||
/** @internal */ | ||
// export type UnknownObject = {}; | ||
export type UnknownObject = Record<string, unknown>; | ||
export type ValidateMode = 'onChange' | 'onChangeAfterSubmit' | 'onSubmit'; | ||
@@ -86,0 +85,0 @@ |
@@ -5,5 +5,5 @@ import invariant from 'invariant'; | ||
import { context as contextReference } from './context'; | ||
import { Context } from './types'; | ||
import { Context, UnknownObject } from './types'; | ||
export function useForm<Model>(): Context<Model> { | ||
export function useForm<Model extends UnknownObject>(): Context<Model> { | ||
const context = useContext(contextReference); | ||
@@ -10,0 +10,0 @@ invariant( |
@@ -8,10 +8,11 @@ import { BaseForm } from './BaseForm'; | ||
} from './ValidatedForm'; | ||
import { UnknownObject } from './types'; | ||
export type ValidatedQuickFormProps<Model> = QuickFormProps<Model> & | ||
ValidatedFormProps<Model>; | ||
export type ValidatedQuickFormProps<Model extends UnknownObject> = | ||
QuickFormProps<Model> & ValidatedFormProps<Model>; | ||
export type ValidatedQuickFormState<Model> = QuickFormState<Model> & | ||
ValidatedFormState<Model>; | ||
export type ValidatedQuickFormState<Model extends UnknownObject> = | ||
QuickFormState<Model> & ValidatedFormState<Model>; | ||
export const ValidatedQuickForm = Validated(Quick(BaseForm)); | ||
export type ValidatedQuickForm = typeof ValidatedQuickForm; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
258212
92
6045