-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript type definitions #318
Open
kettanaito
wants to merge
14
commits into
kettanaito:master
Choose a base branch
from
lemcii:typings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+362
−1
Open
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a406439
Started TypeScript definition file
lemcii 5367187
Adds missing field state values
lemcii 2dbd8c8
Corrects action response type
lemcii 8462878
Adds missing form props
lemcii c7f7743
Adds interface for action func arguments
lemcii c0c032f
Adds ValidationSchema values
lemcii 9fb3045
Improves vaidation message types/interface
lemcii b9ea7fe
Adds generic input props type for fieldProps
lemcii ee8851e
Adds stricter typing for ValidationSchema
lemcii 067332d
Fixes "missing" property name for ValidationMessageSet type
lemcii a336ce9
Fixes fieldProps and error types
lemcii decc14e
Typings: Improves annotations for validation and components
kettanaito eb242c3
Typings: Adds missing touched FieldState
lemcii ebce16e
Typings: Adds explanatory comments for better DX
kettanaito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,293 @@ | ||
import * as React from 'react' | ||
|
||
export interface FormProviderProps { | ||
rules?: ValidationSchema | ||
messages?: ValidationMessages | ||
debounceTime?: number | ||
} | ||
|
||
export class FormProvider extends React.Component<FormProviderProps> {} | ||
|
||
/** | ||
* Field.Group | ||
*/ | ||
export interface FieldGroupProps { | ||
name: string | ||
} | ||
|
||
export interface Field { | ||
Group: React.ComponentClass<FieldGroupProps> | ||
} | ||
|
||
export interface InitialValues { | ||
[key: string]: string | InitialValues | ||
} | ||
|
||
export interface SerializedFields { | ||
[key: string]: string | SerializedFields | ||
} | ||
|
||
export interface FormErrors { | ||
[key: string]: string | string[] | FormErrors | ||
} | ||
|
||
export interface Fields { | ||
[fieldPath: string]: FieldState<string> | Fields | ||
} | ||
|
||
/** | ||
* Validation schema | ||
*/ | ||
export interface ResolverArgs { | ||
get: (fieldPath: string[]) => any | ||
value: string | ||
fieldProps: FieldState<string> | ||
fields: Fields | ||
form: Form | ||
} | ||
|
||
export type Resolver = (args: ResolverArgs) => boolean | ||
|
||
export type AsyncRulePayload = { | ||
valid: boolean | ||
extra?: { | ||
[exraKey: string]: any | ||
} | ||
} | ||
|
||
export interface ResolverGroup { | ||
[ruleName: string]: Resolver | ||
} | ||
|
||
export interface ValidationSchema { | ||
type?: { | ||
[key: string]: Resolver | ResolverGroup | ||
} | ||
name?: { | ||
[key: string]: Resolver | ResolverGroup | ||
} | ||
} | ||
|
||
/** | ||
* Validation messages | ||
*/ | ||
export interface MessageResolverArgs { | ||
extra?: Object | ||
fieldProps: FieldState<string> | ||
fields: Fields | ||
form: Form | ||
} | ||
|
||
export type MessageResolver = (args: MessageResolverArgs) => string | string | ||
|
||
export interface ValidationMessageSet { | ||
[key: string]: { | ||
invalid?: MessageResolver | ||
missing?: MessageResolver | ||
rule?: { | ||
[ruleName: string]: MessageResolver | ||
} | ||
} | ||
} | ||
|
||
export interface ValidationMessageGroup { | ||
[key: string]: ValidationMessageSet | ||
} | ||
|
||
export interface ValidationMessages { | ||
general?: ValidationMessageSet | ||
type?: ValidationMessageSet | ||
name?: ValidationMessageSet | ValidationMessageGroup | ||
} | ||
|
||
export type Errors = string[] | string | null | ||
|
||
export interface FieldErrors { | ||
[key: string]: | ||
| Errors | ||
| { | ||
[key: string]: Errors | ||
} | ||
} | ||
|
||
type GenericFormPayload = (args: { fields: Fields; form: Form }) => void | ||
|
||
type FormSubmitPayload = { | ||
serialized: SerializedFields | ||
fields: Fields | ||
form: Form | ||
} | ||
|
||
type SubmitFormEvent = (args: FormSubmitPayload) => void | ||
|
||
export interface FormProps { | ||
id?: string | ||
className?: string | ||
style?: React.CSSProperties | ||
innerRef?: (ref: Form) => void | ||
action: (args: FormSubmitPayload) => Promise<void> /** @todo */ | ||
initialValues?: InitialValues | ||
|
||
/* Validation */ | ||
rules?: ValidationSchema | ||
messages?: ValidationMessages | ||
|
||
/* Callbacks */ | ||
onFirstChange?(args: { | ||
event: React.FormEvent | ||
nextValue: any /** @todo Field value generics */ | ||
prevValue: any | ||
fieldProps: FieldState<string> /** @todo */ | ||
fields: Fields | ||
form: Form | ||
}): void | ||
onClear?: GenericFormPayload | ||
onReset?: GenericFormPayload | ||
onSerialize?( | ||
args: GenericFormPayload & { serialized: SerializedFields }, | ||
): void | ||
onInvalid?( | ||
args: GenericFormPayload & { | ||
invalidFields: Fields | ||
fields: Fields | ||
form: Form | ||
}, | ||
): void | ||
onSubmitStart?: SubmitFormEvent | ||
onSubmitted?: SubmitFormEvent | ||
onSubmitFailed?: SubmitFormEvent | ||
onSubmitEnd?: SubmitFormEvent | ||
} | ||
|
||
export interface FormState { | ||
fields: Fields | ||
applicableRules: Object /** @todo */ | ||
dirty: boolean | ||
} | ||
|
||
export class Form extends React.Component<FormProps, FormState> { | ||
/* Private */ | ||
private withRegisteredField | ||
private registerField | ||
private updateFieldsWith | ||
private unregisterField | ||
private handleFieldFocus | ||
private handleFieldChange | ||
private handleFieldBlur | ||
private validateField | ||
|
||
/* Public */ | ||
clear(predicate?: (fieldState: Field) => boolean): void | ||
reset(predicate?: (fieldState: Field) => boolean): void | ||
validate(predicate?: (fieldState: Field) => boolean): Promise<boolean> | ||
serialize(): SerializedFields | ||
setValues(fieldsPatch: SerializedFields): void | ||
setErrors(errors: FieldErrors): void | ||
submit(): Promise<void> /** @todo */ | ||
} | ||
|
||
interface Event { | ||
event: React.FormEvent<HTMLInputElement> | ||
fieldProps: FieldState<string> | ||
fields: Fields | ||
form: Form | ||
} | ||
|
||
export interface BlurEvent extends Event {} | ||
export interface FocusEvent extends Event {} | ||
|
||
export interface ChangeEvent extends Event { | ||
event: React.FormEvent<HTMLInputElement> | ||
prevValue: string | ||
nextValue: string | ||
} | ||
|
||
export type Rule = RegExp | ((args: ResolverArgs) => boolean) | ||
export type AsyncRule = RegExp | ((args: ResolverArgs) => AsyncRulePayload) | ||
|
||
export type BlurFunction = (args: BlurEvent) => void | ||
export type ChangeFunction = (args: ChangeEvent) => void | ||
export type FocusFunction = (args: FocusEvent) => void | ||
|
||
export interface FieldProps { | ||
rule?: Rule | ||
asyncRule?: AsyncRule | ||
required?: boolean | ||
skip?: boolean | ||
onBlur?: BlurFunction | ||
onChange?: ChangeFunction | ||
onFocus?: FocusFunction | ||
} | ||
|
||
/** @todo Value generic */ | ||
export interface FieldState<V> { | ||
asyncRule?: AsyncRule | ||
controlled: boolean | ||
debounceValidate: () => any /** @todo */ | ||
errors: string[] | null | ||
expected: boolean | ||
fieldGroup?: string /** @todo */ | ||
fieldPath: string[] | ||
focused: boolean | ||
getRef: any /** @todo */ | ||
initialValue: string | ||
invalid: boolean | ||
mapValue: (value: V) => any | ||
onBlur?: BlurFunction | ||
onChange?: ChangeFunction | ||
onFocus?: FocusFunction | ||
pendingAsyncValidation?: boolean /** @todo */ | ||
reactiveProps: any /** @todo */ | ||
required: boolean | ||
rule?: Rule | ||
serialize: (value: V) => string | ||
skip?: boolean | ||
touched: boolean | ||
type: string | ||
valid: boolean | ||
validAsync: boolean | ||
validatedAsync: boolean | ||
validatedSync: boolean | ||
validating: boolean | ||
validSync: boolean | ||
value: V | ||
valuePropName: string | ||
} | ||
|
||
export const fieldPresets: { | ||
checkbox: CreateFieldOptions<boolean, 'checked'> | ||
input: CreateFieldOptions<string> | ||
radio: CreateFieldOptions<string> | ||
select: CreateFieldOptions<string> | ||
textarea: CreateFieldOptions<string> | ||
} | ||
|
||
export interface CreateFieldOptions<V, ValuePropName = 'value'> { | ||
allowMultiple?: boolean | ||
valuePropName?: ValuePropName | ||
initialValue?: any | ||
beforeRegister: () => any /** @todo */ | ||
shouldValidateOnMount: ( | ||
args: { | ||
valuePropName: ValuePropName /** @todo [valuePropName] dynamic property */ | ||
fieldRecord: any | ||
context: any | ||
}, | ||
) => boolean | ||
mapPropsToField: () => any /** @todo */ | ||
enforceProps: () => Object /** @todo */ | ||
assertValue: (value: V) => boolean | ||
mapValue: (value: any) => any | ||
serialize: (value: V) => any | ||
} | ||
|
||
export function createField<P>( | ||
options: CreateFieldOptions<string>, | ||
): ( | ||
component: ( | ||
props: P & { | ||
fieldProps: React.InputHTMLAttributes<HTMLInputElement> | ||
fieldState: FieldState<string> | ||
}, | ||
) => React.ReactElement<P & FieldProps>, | ||
) => React.ComponentClass<P & FieldProps> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,5 +116,6 @@ | |
"invariant": "^2.2.4", | ||
"ramda": "^0.25.0", | ||
"rxjs": "^6.3.2" | ||
} | ||
}, | ||
"typings": "index.d.ts" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React.HTMLAttributes
rather thanReact.InputHTMLAttributes<HTMLInputElement>
might be a better choice, I thought about adding all variants but that'd require type assertion.