Comparing version 0.2.0-beta.6 to 0.2.0-beta.7
import { FormStrategy, FormModel } from './models'; | ||
import { ValidateStrategy } from './validate'; | ||
import { IFormContext } from './context'; | ||
export interface IForm { | ||
validate(strategy: ValidateStrategy): void; | ||
ctx: IFormContext; | ||
@@ -7,0 +5,0 @@ model: FormModel; |
@@ -5,3 +5,2 @@ "use strict"; | ||
var models_1 = require("./models"); | ||
var validate_1 = require("./validate"); | ||
function useForm(arg) { | ||
@@ -20,6 +19,2 @@ return react_1.useMemo(function () { | ||
var validate$ = model.validate$; | ||
function validate(strategy) { | ||
if (strategy === void 0) { strategy = validate_1.ValidateStrategy.Normal; } | ||
model.validate(strategy); | ||
} | ||
var ctx = { | ||
@@ -32,3 +27,2 @@ validate$: validate$, | ||
return { | ||
validate: validate, | ||
ctx: ctx, | ||
@@ -35,0 +29,0 @@ model: model, |
@@ -17,2 +17,4 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
abstract isValid(): boolean; | ||
abstract patchValue(value: Value): void; | ||
abstract resetValue(): void; | ||
error: IMaybeErrors<Value>; | ||
@@ -31,2 +33,4 @@ initialize(value: Value): void; | ||
isValid(): boolean; | ||
patchValue(value: Value): void; | ||
resetValue(): void; | ||
} | ||
@@ -40,2 +44,4 @@ export declare class FieldSetModel<Value = Record<string, unknown>> extends BasicModel<Value> { | ||
isValid(): boolean; | ||
patchValue(value: Value): void; | ||
resetValue(): void; | ||
} | ||
@@ -50,6 +56,8 @@ export interface IFieldArrayChildFactory<Item> { | ||
constructor(factory: IFieldArrayChildFactory<Item>, defaultValue?: ReadonlyArray<Item>); | ||
initialize(values: Item[]): void; | ||
initialize(values: ReadonlyArray<Item>): void; | ||
models: ReadonlyArray<BasicModel<Item>>; | ||
isValid(): boolean; | ||
getRawValue(): Item[]; | ||
patchValue(value: Item[]): void; | ||
resetValue(): void; | ||
push(...items: ReadonlyArray<Item>): void; | ||
@@ -61,3 +69,3 @@ pop(): BasicModel<Item> | undefined; | ||
} | ||
export declare class FormModel extends FieldSetModel { | ||
export declare class FormModel<T extends object = any> extends FieldSetModel<T> { | ||
private readonly workingValidators; | ||
@@ -64,0 +72,0 @@ readonly isValidating$: BehaviorSubject<boolean>; |
@@ -70,2 +70,9 @@ "use strict"; | ||
}; | ||
FieldModel.prototype.patchValue = function (value) { | ||
this.value$.next(value); | ||
}; | ||
FieldModel.prototype.resetValue = function () { | ||
this.pristine = true; | ||
this.value$.next(this.initialValue); | ||
}; | ||
return FieldModel; | ||
@@ -111,2 +118,23 @@ }(BasicModel)); | ||
}; | ||
FieldSetModel.prototype.patchValue = function (value) { | ||
var keys = Object.keys(value); | ||
for (var i = 0; i < keys.length; i += 1) { | ||
var key = keys[i]; | ||
var child = this.children[key]; | ||
if (child) { | ||
child.patchValue(value[key]); | ||
} | ||
} | ||
}; | ||
FieldSetModel.prototype.resetValue = function () { | ||
this.pristine = true; | ||
var keys = Object.keys(this.children); | ||
for (var i = 0; i < keys.length; i += 1) { | ||
var key = keys[i]; | ||
var child = this.children[key]; | ||
if (child) { | ||
child.resetValue(); | ||
} | ||
} | ||
}; | ||
return FieldSetModel; | ||
@@ -155,2 +183,24 @@ }(BasicModel)); | ||
}; | ||
FieldArrayModel.prototype.patchValue = function (value) { | ||
var models = this.models$.getValue(); | ||
for (var i = 0; i < value.length; i += 1) { | ||
if (i >= models.length) { | ||
break; | ||
} | ||
var item = value[i]; | ||
var model = models[i]; | ||
model.patchValue(item); | ||
} | ||
if (value.length <= models.length) { | ||
this.splice(models.length - 1, value.length - models.length); | ||
return; | ||
} | ||
for (var i = models.length; i < value.length; i += 1) { | ||
var item = value[i]; | ||
this.push(item); | ||
} | ||
}; | ||
FieldArrayModel.prototype.resetValue = function () { | ||
this.initialize(this.initialValue); | ||
}; | ||
FieldArrayModel.prototype.push = function () { | ||
@@ -157,0 +207,0 @@ var items = []; |
import { IValidator } from './validate'; | ||
export declare function min(value: number, error: string): IValidator<string>; | ||
export declare function max(value: number, error: string): IValidator<string>; | ||
export declare function required(message: string): IValidator<any>; | ||
export declare function requiredTrue(message: string): IValidator<boolean>; | ||
export declare function email(message: string): IValidator<string>; | ||
export declare function min(value: number, message?: string): IValidator<string>; | ||
export declare function max(value: number, message?: string): IValidator<string>; | ||
export declare function required(message?: string): IValidator<any>; | ||
export declare function requiredTrue(message?: string): IValidator<boolean>; | ||
export declare function email(message?: string): IValidator<string>; | ||
export interface IWithLength { | ||
length: number; | ||
} | ||
export declare function minLength<T extends IWithLength>(len: number, message: string): IValidator<T>; | ||
export declare function maxLength<T extends IWithLength>(len: number, message: string): IValidator<T>; | ||
export declare function pattern(regexp: RegExp, message: string): IValidator<string>; | ||
export declare function minLength<T extends IWithLength>(length: number, message?: string): IValidator<T>; | ||
export declare function maxLength<T extends IWithLength>(length: number, message?: string): IValidator<T>; | ||
export declare function pattern(regexp: RegExp, message?: string): IValidator<string>; |
@@ -23,14 +23,14 @@ "use strict"; | ||
} | ||
function min(value, error) { | ||
function min(value, message) { | ||
return makeValidator('min', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed < value; | ||
}, error); | ||
}, message); | ||
} | ||
exports.min = min; | ||
function max(value, error) { | ||
function max(value, message) { | ||
return makeValidator('max', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed > value; | ||
}, error); | ||
}, message); | ||
} | ||
@@ -56,3 +56,3 @@ exports.max = max; | ||
message: message, | ||
expected: true, | ||
expect: true, | ||
actual: input, | ||
@@ -76,8 +76,9 @@ }; | ||
exports.email = email; | ||
function minLength(len, message) { | ||
function minLength(length, message) { | ||
function minLength(input) { | ||
return input.length < len | ||
return input.length < length | ||
? { | ||
message: message, | ||
actual: input, | ||
limit: length, | ||
} | ||
@@ -89,8 +90,9 @@ : null; | ||
exports.minLength = minLength; | ||
function maxLength(len, message) { | ||
function maxLength(length, message) { | ||
function maxLength(input) { | ||
return input.length > len | ||
return input.length > length | ||
? { | ||
message: message, | ||
actual: input, | ||
limit: length, | ||
} | ||
@@ -97,0 +99,0 @@ : null; |
import { FormStrategy, FormModel } from './models'; | ||
import { ValidateStrategy } from './validate'; | ||
import { IFormContext } from './context'; | ||
export interface IForm { | ||
validate(strategy: ValidateStrategy): void; | ||
ctx: IFormContext; | ||
@@ -7,0 +5,0 @@ model: FormModel; |
import { useMemo } from 'react'; | ||
import { FormStrategy, FormModel } from './models'; | ||
import { ValidateStrategy } from './validate'; | ||
export function useForm(arg) { | ||
@@ -17,6 +16,2 @@ return useMemo(function () { | ||
var validate$ = model.validate$; | ||
function validate(strategy) { | ||
if (strategy === void 0) { strategy = ValidateStrategy.Normal; } | ||
model.validate(strategy); | ||
} | ||
var ctx = { | ||
@@ -29,3 +24,2 @@ validate$: validate$, | ||
return { | ||
validate: validate, | ||
ctx: ctx, | ||
@@ -32,0 +26,0 @@ model: model, |
@@ -17,2 +17,4 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
abstract isValid(): boolean; | ||
abstract patchValue(value: Value): void; | ||
abstract resetValue(): void; | ||
error: IMaybeErrors<Value>; | ||
@@ -31,2 +33,4 @@ initialize(value: Value): void; | ||
isValid(): boolean; | ||
patchValue(value: Value): void; | ||
resetValue(): void; | ||
} | ||
@@ -40,2 +44,4 @@ export declare class FieldSetModel<Value = Record<string, unknown>> extends BasicModel<Value> { | ||
isValid(): boolean; | ||
patchValue(value: Value): void; | ||
resetValue(): void; | ||
} | ||
@@ -50,6 +56,8 @@ export interface IFieldArrayChildFactory<Item> { | ||
constructor(factory: IFieldArrayChildFactory<Item>, defaultValue?: ReadonlyArray<Item>); | ||
initialize(values: Item[]): void; | ||
initialize(values: ReadonlyArray<Item>): void; | ||
models: ReadonlyArray<BasicModel<Item>>; | ||
isValid(): boolean; | ||
getRawValue(): Item[]; | ||
patchValue(value: Item[]): void; | ||
resetValue(): void; | ||
push(...items: ReadonlyArray<Item>): void; | ||
@@ -61,3 +69,3 @@ pop(): BasicModel<Item> | undefined; | ||
} | ||
export declare class FormModel extends FieldSetModel { | ||
export declare class FormModel<T extends object = any> extends FieldSetModel<T> { | ||
private readonly workingValidators; | ||
@@ -64,0 +72,0 @@ readonly isValidating$: BehaviorSubject<boolean>; |
@@ -68,2 +68,9 @@ import * as tslib_1 from "tslib"; | ||
}; | ||
FieldModel.prototype.patchValue = function (value) { | ||
this.value$.next(value); | ||
}; | ||
FieldModel.prototype.resetValue = function () { | ||
this.pristine = true; | ||
this.value$.next(this.initialValue); | ||
}; | ||
return FieldModel; | ||
@@ -109,2 +116,23 @@ }(BasicModel)); | ||
}; | ||
FieldSetModel.prototype.patchValue = function (value) { | ||
var keys = Object.keys(value); | ||
for (var i = 0; i < keys.length; i += 1) { | ||
var key = keys[i]; | ||
var child = this.children[key]; | ||
if (child) { | ||
child.patchValue(value[key]); | ||
} | ||
} | ||
}; | ||
FieldSetModel.prototype.resetValue = function () { | ||
this.pristine = true; | ||
var keys = Object.keys(this.children); | ||
for (var i = 0; i < keys.length; i += 1) { | ||
var key = keys[i]; | ||
var child = this.children[key]; | ||
if (child) { | ||
child.resetValue(); | ||
} | ||
} | ||
}; | ||
return FieldSetModel; | ||
@@ -153,2 +181,24 @@ }(BasicModel)); | ||
}; | ||
FieldArrayModel.prototype.patchValue = function (value) { | ||
var models = this.models$.getValue(); | ||
for (var i = 0; i < value.length; i += 1) { | ||
if (i >= models.length) { | ||
break; | ||
} | ||
var item = value[i]; | ||
var model = models[i]; | ||
model.patchValue(item); | ||
} | ||
if (value.length <= models.length) { | ||
this.splice(models.length - 1, value.length - models.length); | ||
return; | ||
} | ||
for (var i = models.length; i < value.length; i += 1) { | ||
var item = value[i]; | ||
this.push(item); | ||
} | ||
}; | ||
FieldArrayModel.prototype.resetValue = function () { | ||
this.initialize(this.initialValue); | ||
}; | ||
FieldArrayModel.prototype.push = function () { | ||
@@ -155,0 +205,0 @@ var items = []; |
import { IValidator } from './validate'; | ||
export declare function min(value: number, error: string): IValidator<string>; | ||
export declare function max(value: number, error: string): IValidator<string>; | ||
export declare function required(message: string): IValidator<any>; | ||
export declare function requiredTrue(message: string): IValidator<boolean>; | ||
export declare function email(message: string): IValidator<string>; | ||
export declare function min(value: number, message?: string): IValidator<string>; | ||
export declare function max(value: number, message?: string): IValidator<string>; | ||
export declare function required(message?: string): IValidator<any>; | ||
export declare function requiredTrue(message?: string): IValidator<boolean>; | ||
export declare function email(message?: string): IValidator<string>; | ||
export interface IWithLength { | ||
length: number; | ||
} | ||
export declare function minLength<T extends IWithLength>(len: number, message: string): IValidator<T>; | ||
export declare function maxLength<T extends IWithLength>(len: number, message: string): IValidator<T>; | ||
export declare function pattern(regexp: RegExp, message: string): IValidator<string>; | ||
export declare function minLength<T extends IWithLength>(length: number, message?: string): IValidator<T>; | ||
export declare function maxLength<T extends IWithLength>(length: number, message?: string): IValidator<T>; | ||
export declare function pattern(regexp: RegExp, message?: string): IValidator<string>; |
@@ -21,13 +21,13 @@ var EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/; | ||
} | ||
export function min(value, error) { | ||
export function min(value, message) { | ||
return makeValidator('min', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed < value; | ||
}, error); | ||
}, message); | ||
} | ||
export function max(value, error) { | ||
export function max(value, message) { | ||
return makeValidator('max', function (input) { | ||
var parsed = parseFloat(input); | ||
return !isNaN(parsed) && parsed > value; | ||
}, error); | ||
}, message); | ||
} | ||
@@ -51,3 +51,3 @@ export function required(message) { | ||
message: message, | ||
expected: true, | ||
expect: true, | ||
actual: input, | ||
@@ -69,8 +69,9 @@ }; | ||
} | ||
export function minLength(len, message) { | ||
export function minLength(length, message) { | ||
function minLength(input) { | ||
return input.length < len | ||
return input.length < length | ||
? { | ||
message: message, | ||
actual: input, | ||
limit: length, | ||
} | ||
@@ -81,8 +82,9 @@ : null; | ||
} | ||
export function maxLength(len, message) { | ||
export function maxLength(length, message) { | ||
function maxLength(input) { | ||
return input.length > len | ||
return input.length > length | ||
? { | ||
message: message, | ||
actual: input, | ||
limit: length, | ||
} | ||
@@ -89,0 +91,0 @@ : null; |
{ | ||
"name": "formulr", | ||
"version": "0.2.0-beta.6", | ||
"version": "0.2.0-beta.7", | ||
"description": "Form toolkit for React", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -22,2 +22,4 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
abstract isValid(): boolean; | ||
abstract patchValue(value: Value): void; | ||
abstract resetValue(): void; | ||
@@ -75,2 +77,11 @@ get error() { | ||
} | ||
patchValue(value: Value) { | ||
this.value$.next(value); | ||
} | ||
resetValue() { | ||
this.pristine = true; | ||
this.value$.next(this.initialValue); | ||
} | ||
} | ||
@@ -118,2 +129,25 @@ | ||
} | ||
patchValue(value: Value) { | ||
const keys = Object.keys(value); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
const child = this.children[key]; | ||
if (child) { | ||
child.patchValue((value as any)[key]); | ||
} | ||
} | ||
} | ||
resetValue() { | ||
this.pristine = true; | ||
const keys = Object.keys(this.children); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
const child = this.children[key]; | ||
if (child) { | ||
child.resetValue(); | ||
} | ||
} | ||
} | ||
} | ||
@@ -135,3 +169,3 @@ | ||
initialize(values: Item[]) { | ||
initialize(values: ReadonlyArray<Item>) { | ||
super.initialize(values); | ||
@@ -167,2 +201,26 @@ this.models$.next(values.map(this.factory)); | ||
patchValue(value: Item[]) { | ||
const models = this.models$.getValue(); | ||
for (let i = 0; i < value.length; i += 1) { | ||
if (i >= models.length) { | ||
break; | ||
} | ||
const item = value[i]; | ||
const model = models[i]; | ||
model.patchValue(item); | ||
} | ||
if (value.length <= models.length) { | ||
this.splice(models.length - 1, value.length - models.length); | ||
return; | ||
} | ||
for (let i = models.length; i < value.length; i += 1) { | ||
const item = value[i]; | ||
this.push(item); | ||
} | ||
} | ||
resetValue() { | ||
this.initialize(this.initialValue); | ||
} | ||
push(...items: ReadonlyArray<Item>) { | ||
@@ -202,3 +260,3 @@ const nextModels: ReadonlyArray<BasicModel<Item>> = this.models$.getValue().concat(items.map(this.factory)); | ||
export class FormModel extends FieldSetModel { | ||
export class FormModel<T extends object = any> extends FieldSetModel<T> { | ||
private readonly workingValidators = new Set<Observable<unknown>>(); | ||
@@ -205,0 +263,0 @@ readonly isValidating$ = new BehaviorSubject(false); |
@@ -7,3 +7,3 @@ import { ValidatorResult, IValidator } from './validate'; | ||
function makeValidator<Value>(name: string, impl: ValidatorImpl<Value>, message: string): IValidator<Value> { | ||
function makeValidator<Value>(name: string, impl: ValidatorImpl<Value>, message?: string): IValidator<Value> { | ||
const validator: IValidator<Value> = (value: Value): ValidatorResult<Value> => { | ||
@@ -29,3 +29,3 @@ if (isEmptyInputValue(value)) { | ||
export function min(value: number, error: string) { | ||
export function min(value: number, message?: string) { | ||
return makeValidator( | ||
@@ -37,7 +37,7 @@ 'min', | ||
}, | ||
error, | ||
message, | ||
); | ||
} | ||
export function max(value: number, error: string) { | ||
export function max(value: number, message?: string) { | ||
return makeValidator( | ||
@@ -49,7 +49,7 @@ 'max', | ||
}, | ||
error, | ||
message, | ||
); | ||
} | ||
export function required(message: string): IValidator<any> { | ||
export function required(message?: string): IValidator<any> { | ||
function required(input: any) { | ||
@@ -66,3 +66,3 @@ return isEmptyInputValue(input) | ||
export function requiredTrue(message: string): IValidator<boolean> { | ||
export function requiredTrue(message?: string): IValidator<boolean> { | ||
function requiredTrue(input: boolean) { | ||
@@ -73,3 +73,3 @@ return input === true | ||
message, | ||
expected: true, | ||
expect: true, | ||
actual: input, | ||
@@ -81,3 +81,3 @@ }; | ||
export function email(message: string): IValidator<string> { | ||
export function email(message?: string): IValidator<string> { | ||
function email(input: string) { | ||
@@ -98,8 +98,9 @@ return EMAIL_REGEXP.test(input) | ||
export function minLength<T extends IWithLength>(len: number, message: string): IValidator<T> { | ||
export function minLength<T extends IWithLength>(length: number, message?: string): IValidator<T> { | ||
function minLength(input: T) { | ||
return input.length < len | ||
return input.length < length | ||
? { | ||
message, | ||
actual: input, | ||
limit: length, | ||
} | ||
@@ -111,8 +112,9 @@ : null; | ||
export function maxLength<T extends IWithLength>(len: number, message: string): IValidator<T> { | ||
export function maxLength<T extends IWithLength>(length: number, message?: string): IValidator<T> { | ||
function maxLength(input: T) { | ||
return input.length > len | ||
return input.length > length | ||
? { | ||
message, | ||
actual: input, | ||
limit: length, | ||
} | ||
@@ -124,3 +126,3 @@ : null; | ||
export function pattern(regexp: RegExp, message: string): IValidator<string> { | ||
export function pattern(regexp: RegExp, message?: string): IValidator<string> { | ||
function pattern(input: string) { | ||
@@ -127,0 +129,0 @@ return regexp.test(input) |
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
103401
2974