formula-one
Advanced tools
Comparing version 0.0.4 to 0.0.5
/// <reference types="react" /> | ||
import * as React from "react"; | ||
export declare type FeedbackStrategy = | ||
| "OnFirstBlur" | ||
| "OnFirstChange" | ||
| "OnFirstSuccess" | ||
| "OnFirstSuccessOrFirstBlur" | ||
| "OnSubmit"; | ||
export declare type FeedbackStrategy = "OnFirstBlur" | "OnFirstChange" | "OnFirstSuccess" | "OnFirstSuccessOrFirstBlur" | "OnSubmit"; | ||
export interface MetaField { | ||
touched: boolean; | ||
changed: boolean; | ||
succeeded: boolean; | ||
validationSucceeded: boolean; | ||
asyncValidationInFlight: boolean; | ||
touched: boolean; | ||
changed: boolean; | ||
succeeded: boolean; | ||
asyncValidationInFlight: boolean; | ||
} | ||
export interface Meta<DraftType extends object> { | ||
pristine: boolean; | ||
submitted: boolean; | ||
fields: {[P in keyof DraftType]: MetaField}; | ||
pristine: boolean; | ||
submitted: boolean; | ||
fields: { | ||
[P in keyof DraftType]: MetaField; | ||
}; | ||
} | ||
export interface FormError<DraftType> { | ||
message: string; | ||
fields?: Array<keyof DraftType>; | ||
message: string; | ||
fields?: Array<keyof DraftType>; | ||
} | ||
export declare type FieldValidator<FieldType> = (x: FieldType) => null | string; | ||
export declare type FormValidator<DraftType> = ( | ||
x: DraftType | ||
) => Array<FormError<DraftType>>; | ||
export declare type FormValidator<DraftType> = (x: DraftType) => Array<FormError<DraftType>>; | ||
export declare type SubmitFunction<DraftType> = (x: DraftType) => void; | ||
export interface FormProps<DraftType extends object> { | ||
initialValue: DraftType; | ||
children: ( | ||
formStuff: { | ||
onSubmit: (e: React.FormEvent<HTMLElement>) => void; | ||
formState: Readonly<DraftType>; | ||
meta: Meta<DraftType>; | ||
}, | ||
fields: FieldsObject<DraftType> | ||
) => React.ReactNode; | ||
fieldValidations?: Partial< | ||
{[P in keyof DraftType]: FieldValidator<DraftType[P]>} | ||
>; | ||
validations?: ReadonlyArray<FormValidator<DraftType>>; | ||
feedbackStrategy?: FeedbackStrategy; | ||
onSubmit: SubmitFunction<DraftType>; | ||
onValidationFailure?: (errors: ReadonlyArray<FormError<DraftType>>) => void; | ||
onFieldChange?: <P extends keyof DraftType>( | ||
fieldName: P, | ||
newValue: DraftType[P], | ||
updateField: <P2 extends keyof DraftType>( | ||
fieldName: P2, | ||
newValue: DraftType[P2] | ||
) => void | ||
) => void; | ||
initialValue: DraftType; | ||
children: (formParams: { | ||
onSubmit: (e: React.FormEvent<HTMLElement>) => void; | ||
formState: Readonly<DraftType>; | ||
meta: Meta<DraftType>; | ||
}, fields: FieldsObject<DraftType>) => React.ReactNode; | ||
fieldValidations?: Partial<{ | ||
[P in keyof DraftType]: FieldValidator<DraftType[P]>; | ||
}>; | ||
validations?: ReadonlyArray<FormValidator<DraftType>>; | ||
feedbackStrategy?: FeedbackStrategy; | ||
onSubmit: SubmitFunction<DraftType>; | ||
onValidationFailure?: (errors: ReadonlyArray<FormError<DraftType>>) => void; | ||
onFieldChange?: <P extends keyof DraftType>(fieldName: P, newValue: DraftType[P], updateField: <P2 extends keyof DraftType>(fieldName: P2, newValue: DraftType[P2]) => void) => void; | ||
} | ||
export interface FormState<DraftType extends object> { | ||
formState: DraftType; | ||
meta: Meta<DraftType>; | ||
formState: DraftType; | ||
meta: Meta<DraftType>; | ||
} | ||
export default class Form<DraftType extends object> extends React.Component< | ||
FormProps<DraftType>, | ||
FormState<DraftType> | ||
> { | ||
static defaultProps: { | ||
fieldValidations: {}; | ||
validations: never[]; | ||
feedbackStrategy: string; | ||
}; | ||
private fieldCache; | ||
constructor(props: FormProps<DraftType>); | ||
reset(): void; | ||
private readonly fields; | ||
private shouldShowFeedbackForFields<P>(fieldNames); | ||
private makeErrors(); | ||
private handleSubmit; | ||
private updateFieldMeta<P>(fieldName, metaKey, metaValue); | ||
private updateField; | ||
private handleFieldChange; | ||
private handleFieldBlur; | ||
render(): JSX.Element; | ||
export default class Form<DraftType extends object> extends React.Component<FormProps<DraftType>, FormState<DraftType>> { | ||
static defaultProps: { | ||
fieldValidations: {}; | ||
validations: never[]; | ||
feedbackStrategy: string; | ||
}; | ||
private fieldCache; | ||
constructor(props: FormProps<DraftType>); | ||
reset(): void; | ||
render(): JSX.Element; | ||
private readonly fields; | ||
private shouldShowFeedbackForFields<P>(fieldNames); | ||
private makeErrors(); | ||
private handleSubmit; | ||
private updateFieldMeta<P>(fieldName, metaKey, metaValue); | ||
private updateField; | ||
private handleFieldChange; | ||
private handleFieldBlur; | ||
} | ||
export interface FieldProps<DraftType, OutputType> { | ||
label?: string; | ||
children: ( | ||
foo: { | ||
value: null | OutputType; | ||
showError: boolean; | ||
formState: Readonly<DraftType>; | ||
onChange: (newValue: null | OutputType) => void; | ||
onBlur: () => void; | ||
} | ||
) => React.ReactNode; | ||
label?: string; | ||
children: (fieldParams: { | ||
value: OutputType; | ||
showError: boolean; | ||
formState: Readonly<DraftType>; | ||
onChange: (newValue: OutputType) => void; | ||
onBlur: () => void; | ||
}) => React.ReactNode; | ||
} | ||
export declare class Field< | ||
DraftType extends object, | ||
OutputType | ||
> extends React.Component<FieldProps<DraftType, OutputType>> { | ||
["constructor"]: typeof Field; | ||
static readonly fieldName: string; | ||
render(): JSX.Element; | ||
export declare class Field<DraftType extends object, OutputType> extends React.Component<FieldProps<DraftType, OutputType>> { | ||
["constructor"]: typeof Field; | ||
static readonly fieldName: string; | ||
render(): JSX.Element; | ||
} | ||
export declare type FieldsObject<DraftType extends object> = { | ||
[P in keyof DraftType]: { | ||
new (): Field<DraftType, DraftType[P]>; | ||
} | ||
[P in keyof DraftType]: { | ||
new (): Field<DraftType, DraftType[P]>; | ||
}; | ||
}; | ||
export declare class FormErrors extends React.Component<{}> { | ||
render(): JSX.Element; | ||
render(): JSX.Element; | ||
} |
@@ -24,3 +24,2 @@ var __extends = (this && this.__extends) || (function () { | ||
succeeded: false, | ||
validationSucceeded: false, | ||
asyncValidationInFlight: false, | ||
@@ -46,3 +45,4 @@ }; | ||
var errors = _this.makeErrors(); | ||
var fieldErrorsArray = Object.keys(errors.fieldErrors).reduce(function (memo, error) { | ||
var fieldErrorsArray = Object.keys(errors.fieldErrors).reduce(function (memo, key) { | ||
var error = errors.fieldErrors[key]; | ||
return error === null ? memo : memo.concat([error]); | ||
@@ -64,4 +64,2 @@ }, []); | ||
}, function () { | ||
// tslint:disable | ||
// debugger; | ||
// set succeeded | ||
@@ -112,2 +110,16 @@ var formErrors = []; | ||
}; | ||
Form.prototype.render = function () { | ||
return (React.createElement(FormContext.Provider, { value: { | ||
formState: this.state.formState, | ||
// Filthy monomorphization lies | ||
// tslint:disable-next-line no-any | ||
errors: this.makeErrors(), | ||
onChange: this.handleFieldChange, | ||
onBlur: this.handleFieldBlur, | ||
} }, this.props.children({ | ||
meta: this.state.meta, | ||
formState: this.state.formState, | ||
onSubmit: this.handleSubmit, | ||
}, this.fields))); | ||
}; | ||
Object.defineProperty(Form.prototype, "fields", { | ||
@@ -228,16 +240,2 @@ get: function () { | ||
}; | ||
Form.prototype.render = function () { | ||
return (React.createElement(FormContext.Provider, { value: { | ||
formState: this.state.formState, | ||
// Filthy monomorphization lies | ||
// tslint:disable-next-line no-any | ||
errors: this.makeErrors(), | ||
onChange: this.handleFieldChange, | ||
onBlur: this.handleFieldBlur, | ||
} }, this.props.children({ | ||
meta: this.state.meta, | ||
formState: this.state.formState, | ||
onSubmit: this.handleSubmit, | ||
}, this.fields))); | ||
}; | ||
Form.defaultProps = { | ||
@@ -274,2 +272,3 @@ fieldValidations: {}, | ||
// also the typechecker probably can't prove OutputType === DraftType[fieldName] | ||
// tslint:disable-next-line no-any | ||
var value = formState[fieldName]; | ||
@@ -276,0 +275,0 @@ var fieldError = errors.fieldErrors[fieldName]; |
{ | ||
"name": "formula-one", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Strongly-typed React form state management", | ||
@@ -15,3 +15,3 @@ "author": "Zach Gotsch", | ||
"build": "rm ./dist/*; tsc --declaration", | ||
"prepublishOnly": "npm build" | ||
"prepublishOnly": "npm run-script build" | ||
}, | ||
@@ -18,0 +18,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
29264
433