@formily/core
Advanced tools
Comparing version 1.0.8 to 1.0.9
import { FormPath, FormPathPattern, BigData } from '@formily/shared'; | ||
import { FormValidator, setValidationLanguage, setValidationLocale } from '@formily/validator'; | ||
import { FormGraph } from './shared/graph'; | ||
import { IFormCreatorOptions, IForm } from './types'; | ||
import { IFormState, IFieldState, IFormCreatorOptions, IFieldStateProps, IVirtualFieldStateProps, IFormSubmitResult, IFormResetOptions, IField, IVirtualField, FormHeartSubscriber, IFormExtendedValidateFieldOptions } from './types'; | ||
export * from './shared/lifecycle'; | ||
export * from './types'; | ||
export declare function createForm<FieldProps, VirtualFieldProps>(options?: IFormCreatorOptions): IForm; | ||
export declare function createForm<FieldProps, VirtualFieldProps>(options?: IFormCreatorOptions): { | ||
submit: (onSubmit?: (values: {}) => any) => Promise<IFormSubmitResult>; | ||
reset: ({ selector, forceClear, validate, clearInitialValue }?: IFormResetOptions) => Promise<void | import("@formily/validator").ValidateNodeResult>; | ||
hasChanged: (target: any, path: FormPathPattern) => boolean; | ||
clearErrors: (pattern?: FormPathPattern) => void; | ||
validate: (path?: FormPathPattern, opts?: IFormExtendedValidateFieldOptions) => Promise<import("@formily/validator").ValidateNodeResult>; | ||
setFormState: (callback?: (state: IFormState<any>) => any, silent?: boolean) => void; | ||
getFormState: (callback?: (state: IFormState<any>) => any) => any; | ||
setFieldState: (path: FormPathPattern, callback?: (state: IFieldState<FieldProps>) => void, silent?: boolean) => void; | ||
getFieldState: (path: FormPathPattern, callback?: (state: IFieldState<FieldProps>) => any) => any; | ||
registerField: ({ path, name, value, initialValue, required, rules, editable, visible, display, computeState, dataType, useDirty, props }: IFieldStateProps<any>) => IField; | ||
registerVirtualField: ({ name, path, props, display, visible, computeState, useDirty }: IVirtualFieldStateProps<any>) => IVirtualField; | ||
createMutators: (input: any) => { | ||
change(...values: any[]): any; | ||
focus(): void; | ||
blur(): void; | ||
push(value?: any): any[]; | ||
pop(): any[]; | ||
insert(index: number, value: any): any[]; | ||
remove(index?: string | number): void; | ||
exist(index?: string | number): any; | ||
unshift(value: any): any[]; | ||
shift(): any[]; | ||
move($from: number, $to: number): any[]; | ||
moveUp(index: number): any[]; | ||
moveDown(index: number): any[]; | ||
validate(opts?: IFormExtendedValidateFieldOptions): Promise<import("@formily/validator").ValidateNodeResult>; | ||
}; | ||
getFormGraph: () => any; | ||
setFormGraph: (nodes: {}) => void; | ||
setFieldValue: (path: FormPathPattern, value?: any, silent?: boolean) => void; | ||
unsafe_do_not_use_transform_data_path: (path: FormPathPattern) => FormPath; | ||
getFieldValue: (path?: FormPathPattern) => any; | ||
setFieldInitialValue: (path?: FormPathPattern, value?: any, silent?: boolean) => void; | ||
getFieldInitialValue: (path?: FormPathPattern) => any; | ||
isHostRendering: () => boolean; | ||
hostUpdate: (callback?: () => any) => any; | ||
subscribe: (callback?: FormHeartSubscriber) => number; | ||
unsubscribe: (id: number) => void; | ||
notify: <T>(type: string, payload: T) => void; | ||
}; | ||
export declare const registerValidationFormats: typeof FormValidator.registerFormats; | ||
@@ -9,0 +49,0 @@ export declare const registerValidationRules: typeof FormValidator.registerRules; |
@@ -644,6 +644,16 @@ "use strict"; | ||
} | ||
function createMutators(field) { | ||
if (!types_1.isField(field)) { | ||
throw new Error('The `createMutators` can only accept FieldState instance.'); | ||
function createMutators(input) { | ||
var field; | ||
if (!types_1.isField(input)) { | ||
var selected = graph.select(input); | ||
if (selected) { | ||
field = selected; | ||
} | ||
else { | ||
throw new Error('The `createMutators` can only accept FieldState instance or FormPathPattern.'); | ||
} | ||
} | ||
else { | ||
field = input; | ||
} | ||
function setValue() { | ||
@@ -722,4 +732,5 @@ var values = []; | ||
} | ||
function swapAfterState(start, arrayLength) { | ||
for (var i = arrayLength - 1; i >= start + 1; i--) { | ||
function swapAfterState(start, arrayLength, step) { | ||
if (step === void 0) { step = 1; } | ||
for (var i = arrayLength - 1; i >= start + 1; i -= step) { | ||
swapState(i, i - 1); | ||
@@ -792,9 +803,3 @@ } | ||
unshift: function (value) { | ||
var arr = shared_1.toArr(getValue()).slice(); | ||
arr.unshift(value); | ||
setValue(arr); | ||
onGraphChange(function () { | ||
swapAfterState(0, arr.length); | ||
}); | ||
return arr; | ||
return mutators.insert(0, value); | ||
}, | ||
@@ -957,5 +962,7 @@ shift: function () { | ||
payload = _a.sent(); | ||
heart.publish(types_1.LifeCycleTypes.ON_FORM_ON_SUBMIT_SUCCESS, payload); | ||
return [3, 5]; | ||
case 4: | ||
e_1 = _a.sent(); | ||
heart.publish(types_1.LifeCycleTypes.ON_FORM_ON_SUBMIT_FAILED, e_1); | ||
new Promise(function () { | ||
@@ -1077,3 +1084,5 @@ throw e_1; | ||
if (env.taskQueue[taskIndex] && | ||
!env.taskQueue[taskIndex].callbacks.some(function (fn) { return shared_1.isEqual(fn, callback); })) { | ||
!env.taskQueue[taskIndex].callbacks.some(function (fn) { | ||
return shared_1.isEqual(fn, callback) ? fn === callback : false; | ||
})) { | ||
env.taskQueue[taskIndex].callbacks.push(callback); | ||
@@ -1080,0 +1089,0 @@ } |
@@ -84,23 +84,11 @@ "use strict"; | ||
}; | ||
_this.watchProps = function (props, keys, callback) { | ||
if (!_this.cacheProps) { | ||
_this.cacheProps = __assign({}, props); | ||
} | ||
else { | ||
var changeNum_1 = 0; | ||
var changedProps_1 = {}; | ||
shared_1.toArr(keys).forEach(function (key) { | ||
if (!shared_1.isEqual(_this.cacheProps[key], props[key])) { | ||
changeNum_1++; | ||
changedProps_1[key] = props[key]; | ||
} | ||
}); | ||
if (changeNum_1 > 0) { | ||
if (shared_1.isFn(callback)) { | ||
callback(changedProps_1, props); | ||
} | ||
_this.cacheProps = __assign({}, props); | ||
} | ||
} | ||
_this.setCache = function (key, value) { | ||
_this.cache[key] = shared_1.shallowClone(value); | ||
}; | ||
_this.getCache = function (key) { | ||
return _this.cache[key]; | ||
}; | ||
_this.removeCache = function (key) { | ||
delete _this.cache[key]; | ||
}; | ||
_this.setState = function (callback, silent) { | ||
@@ -211,2 +199,3 @@ if (silent === void 0) { silent = false; } | ||
_this.dirtys = {}; | ||
_this.cache = {}; | ||
_this.dirtyNum = 0; | ||
@@ -213,0 +202,0 @@ _this.stackCount = 0; |
/// <reference types="react" /> | ||
import { FormPath, FormPathPattern, Subscribable } from '@formily/shared'; | ||
import { ValidatePatternRules, ValidateNodeResult, ValidateFieldOptions } from '@formily/validator'; | ||
import { createForm } from './index'; | ||
import { FormLifeCycle } from './shared/lifecycle'; | ||
@@ -24,2 +25,4 @@ import { Draft } from 'immer'; | ||
ON_FORM_SUBMIT_VALIDATE_FAILED = "onFormSubmitValidateFailed", | ||
ON_FORM_ON_SUBMIT_SUCCESS = "onFormOnSubmitSuccess", | ||
ON_FORM_ON_SUBMIT_FAILED = "onFormOnSubmitFailed", | ||
ON_FORM_VALUES_CHANGE = "onFormValuesChange", | ||
@@ -226,18 +229,3 @@ ON_FORM_INITIAL_VALUES_CHANGE = "onFormInitialValuesChange", | ||
}; | ||
export interface IMutators { | ||
change(...values: any[]): any; | ||
focus(): void; | ||
blur(): void; | ||
push(value?: any): any[]; | ||
pop(): any[]; | ||
insert(index: number, value: any): any[]; | ||
remove(index: number | string): any; | ||
unshift(value: any): any[]; | ||
shift(): any[]; | ||
move($from: number, $to: number): any[]; | ||
moveDown(index: number): any[]; | ||
moveUp(index: number): any[]; | ||
validate(opts?: IFormExtendedValidateFieldOptions): Promise<IFormValidateResult>; | ||
exist(index?: number | string): boolean; | ||
} | ||
export declare type IMutators = ReturnType<IForm['createMutators']>; | ||
export interface IModel<S = {}, P = {}> extends Subscribable { | ||
@@ -258,10 +246,8 @@ state: S; | ||
setSourceState: (callback?: (state: S) => void) => void; | ||
watchProps: <T extends { | ||
[key: string]: any; | ||
}>(props: T, keys: string[], callback: (changedProps: { | ||
[key: string]: any; | ||
}, props: T) => void) => void; | ||
hasChanged: (path?: FormPathPattern) => boolean; | ||
isDirty: (key?: string) => boolean; | ||
getDirtyInfo: () => StateDirtyMap<S>; | ||
setCache: (key: string, value: any) => void; | ||
getCache: (key: string) => any; | ||
removeCache: (key: string) => void; | ||
} | ||
@@ -271,27 +257,2 @@ export declare type IField = IModel<IFieldState, IFieldStateProps>; | ||
export declare type IFormInternal = IModel<IFormState, IFormStateProps>; | ||
export interface IForm { | ||
submit(onSubmit?: (values: IFormState['values']) => any | Promise<any>): Promise<IFormSubmitResult>; | ||
clearErrors: (pattern?: FormPathPattern) => void; | ||
hasChanged(target: any, path: FormPathPattern): boolean; | ||
reset(options?: IFormResetOptions): Promise<void | IFormValidateResult>; | ||
validate(path?: FormPathPattern, options?: IFormExtendedValidateFieldOptions): Promise<IFormValidateResult>; | ||
setFormState(callback?: (state: IFormState) => any, silent?: boolean): void; | ||
getFormState(callback?: (state: IFormState) => any): any; | ||
setFieldState(path: FormPathPattern, callback?: (state: IFieldUserState) => void, silent?: boolean): void; | ||
getFieldState(path: FormPathPattern, callback?: (state: IFieldState) => any): any; | ||
unsafe_do_not_use_transform_data_path(path: FormPathPattern): FormPathPattern; | ||
registerField(props: IFieldStateProps): IField; | ||
registerVirtualField(props: IVirtualFieldStateProps): IVirtualField; | ||
createMutators(field: IField): IMutators; | ||
getFormGraph(): IFormGraph; | ||
setFormGraph(graph: IFormGraph): void; | ||
subscribe(callback?: FormHeartSubscriber): number; | ||
unsubscribe(id: number): void; | ||
notify: <T>(type: string, payload?: T) => void; | ||
isHostRendering: () => boolean; | ||
hostUpdate: (callback?: () => any) => any; | ||
setFieldValue(path?: FormPathPattern, value?: any): void; | ||
getFieldValue(path?: FormPathPattern): any; | ||
setFieldInitialValue(path?: FormPathPattern, value?: any): void; | ||
getFieldInitialValue(path?: FormPathPattern): any; | ||
} | ||
export declare type IForm = ReturnType<typeof createForm>; |
@@ -18,2 +18,4 @@ "use strict"; | ||
LifeCycleTypes["ON_FORM_SUBMIT_VALIDATE_FAILED"] = "onFormSubmitValidateFailed"; | ||
LifeCycleTypes["ON_FORM_ON_SUBMIT_SUCCESS"] = "onFormOnSubmitSuccess"; | ||
LifeCycleTypes["ON_FORM_ON_SUBMIT_FAILED"] = "onFormOnSubmitFailed"; | ||
LifeCycleTypes["ON_FORM_VALUES_CHANGE"] = "onFormValuesChange"; | ||
@@ -20,0 +22,0 @@ LifeCycleTypes["ON_FORM_INITIAL_VALUES_CHANGE"] = "onFormInitialValuesChange"; |
{ | ||
"name": "@formily/core", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"license": "MIT", | ||
@@ -28,4 +28,4 @@ "main": "lib", | ||
"dependencies": { | ||
"@formily/shared": "^1.0.8", | ||
"@formily/validator": "^1.0.8", | ||
"@formily/shared": "^1.0.9", | ||
"@formily/validator": "^1.0.9", | ||
"immer": "^3.2.0" | ||
@@ -36,3 +36,3 @@ }, | ||
}, | ||
"gitHead": "b755498fb37387fb22d27180517575196d6ed0c0" | ||
"gitHead": "b6966b936875039b2bebe99f91875d922ffeb776" | ||
} |
@@ -444,2 +444,8 @@ # @formily/core | ||
// Triggered when the onSubmit success | ||
ON_FORM_ON_SUBMIT_SUCCESS = 'onFormOnSubmitSuccess', | ||
// Triggered when the onSubmit failed | ||
ON_FORM_ON_SUBMIT_FAILED = 'onFormOnSubmitFailed', | ||
// Triggered when the form value changes | ||
@@ -633,3 +639,3 @@ ON_FORM_VALUES_CHANGE = 'onFormValuesChange', | ||
*/ | ||
createMutators(field: IField): IMutators | ||
createMutators(field: IField | FormPathPattern): IMutators | ||
/* | ||
@@ -636,0 +642,0 @@ * Get the form observer tree |
@@ -442,2 +442,4 @@ # @formily/core | ||
ON_FORM_SUBMIT_VALIDATE_FAILED = 'onFormSubmitValidateFailed', // 表单提交时因为校验失败而触发 | ||
ON_FORM_ON_SUBMIT_SUCCESS = 'onFormOnSubmitSuccess', // 表单自定义onSubmit成功 | ||
ON_FORM_ON_SUBMIT_FAILED = 'onFormOnSubmitFailed', // 表单自定义onSubmit失败 | ||
ON_FORM_VALUES_CHANGE = 'onFormValuesChange', //表单值变化时触发 | ||
@@ -648,3 +650,3 @@ ON_FORM_INITIAL_VALUES_CHANGE = 'onFormInitialValuesChange', //表单初始值变化时触发 | ||
*/ | ||
createMutators(field: IField): IMutators | ||
createMutators(field: IField | FormPathPattern): IMutators | ||
@@ -651,0 +653,0 @@ /* |
@@ -33,3 +33,2 @@ import { | ||
IVirtualFieldStateProps, | ||
IForm, | ||
IFormSubmitResult, | ||
@@ -54,3 +53,3 @@ IFormValidateResult, | ||
options: IFormCreatorOptions = {} | ||
): IForm { | ||
) { | ||
function onGraphChange({ type, payload }) { | ||
@@ -730,8 +729,19 @@ heart.publish(LifeCycleTypes.ON_FORM_GRAPH_CHANGE, graph) | ||
} | ||
function createMutators(field: IField) { | ||
if (!isField(field)) { | ||
throw new Error( | ||
'The `createMutators` can only accept FieldState instance.' | ||
) | ||
/** | ||
* | ||
* @param input IField | FormPathPattern | ||
*/ | ||
function createMutators(input: any) { | ||
let field: IField | ||
if (!isField(input)) { | ||
const selected = graph.select(input) | ||
if (selected) { | ||
field = selected | ||
} else { | ||
throw new Error( | ||
'The `createMutators` can only accept FieldState instance or FormPathPattern.' | ||
) | ||
} | ||
} else { | ||
field = input | ||
} | ||
@@ -782,2 +792,4 @@ function setValue(...values: any[]) { | ||
//1. 无法自动交换通过移动来新增删除子列表元素的状态 | ||
//2. 暂时不支持通过setFieldState修改值场景的状态交换 | ||
function swapState($from: number, $to: number) { | ||
@@ -813,4 +825,4 @@ const keys: string[] = ['initialValue', 'visibleCacheValue', 'values'] | ||
function swapAfterState(start: number, arrayLength: number) { | ||
for (let i = arrayLength - 1; i >= start + 1; i--) { | ||
function swapAfterState(start: number, arrayLength: number, step = 1) { | ||
for (let i = arrayLength - 1; i >= start + 1; i -= step) { | ||
swapState(i, i - 1) | ||
@@ -882,9 +894,3 @@ } | ||
unshift(value: any) { | ||
const arr = toArr(getValue()).slice() | ||
arr.unshift(value) | ||
setValue(arr) | ||
onGraphChange(() => { | ||
swapAfterState(0, arr.length) | ||
}) | ||
return arr | ||
return mutators.insert(0, value) | ||
}, | ||
@@ -1048,3 +1054,5 @@ shift() { | ||
payload = await Promise.resolve(onSubmit(values)) | ||
heart.publish(LifeCycleTypes.ON_FORM_ON_SUBMIT_SUCCESS, payload) | ||
} catch (e) { | ||
heart.publish(LifeCycleTypes.ON_FORM_ON_SUBMIT_FAILED, e) | ||
new Promise(() => { | ||
@@ -1179,3 +1187,5 @@ throw e | ||
env.taskQueue[taskIndex] && | ||
!env.taskQueue[taskIndex].callbacks.some(fn => isEqual(fn, callback)) | ||
!env.taskQueue[taskIndex].callbacks.some(fn => | ||
isEqual(fn, callback) ? fn === callback : false | ||
) | ||
) { | ||
@@ -1182,0 +1192,0 @@ env.taskQueue[taskIndex].callbacks.push(callback) |
@@ -11,4 +11,4 @@ import { | ||
isValid, | ||
toArr, | ||
defaults | ||
defaults, | ||
shallowClone | ||
} from '@formily/shared' | ||
@@ -38,3 +38,3 @@ import produce, { Draft, setAutoFreeze } from 'immer' | ||
} | ||
public cacheProps?: any | ||
public cache?: any | ||
public displayName?: string | ||
@@ -54,2 +54,3 @@ public dirtyNum: number | ||
this.dirtys = {} | ||
this.cache = {} | ||
this.dirtyNum = 0 | ||
@@ -106,32 +107,14 @@ this.stackCount = 0 | ||
watchProps = <T extends { [key: string]: any }>( | ||
props: T, | ||
keys: string[], | ||
callback: ( | ||
changedProps: { | ||
[key: string]: any | ||
}, | ||
props?: T | ||
) => void | ||
) => { | ||
if (!this.cacheProps) { | ||
this.cacheProps = { ...props } | ||
} else { | ||
let changeNum = 0 | ||
let changedProps = {} | ||
toArr(keys).forEach((key: string) => { | ||
if (!isEqual(this.cacheProps[key], props[key])) { | ||
changeNum++ | ||
changedProps[key] = props[key] | ||
} | ||
}) | ||
if (changeNum > 0) { | ||
if (isFn(callback)) { | ||
callback(changedProps, props) | ||
} | ||
this.cacheProps = { ...props } | ||
} | ||
} | ||
setCache = (key: string, value: any) => { | ||
this.cache[key] = shallowClone(value) | ||
} | ||
getCache = (key: string) => { | ||
return this.cache[key] | ||
} | ||
removeCache = (key: string) => { | ||
delete this.cache[key] | ||
} | ||
setState = ( | ||
@@ -138,0 +121,0 @@ callback: (state: State | Draft<State>) => State | void, |
@@ -7,2 +7,3 @@ import { FormPath, FormPathPattern, isFn, Subscribable } from '@formily/shared' | ||
} from '@formily/validator' | ||
import { createForm } from './index' | ||
import { FormLifeCycle } from './shared/lifecycle' | ||
@@ -38,2 +39,4 @@ import { Draft } from 'immer' | ||
ON_FORM_SUBMIT_VALIDATE_FAILED = 'onFormSubmitValidateFailed', | ||
ON_FORM_ON_SUBMIT_SUCCESS = 'onFormOnSubmitSuccess', | ||
ON_FORM_ON_SUBMIT_FAILED = 'onFormOnSubmitFailed', | ||
ON_FORM_VALUES_CHANGE = 'onFormValuesChange', | ||
@@ -313,20 +316,3 @@ ON_FORM_INITIAL_VALUES_CHANGE = 'onFormInitialValuesChange', | ||
export interface IMutators { | ||
change(...values: any[]): any | ||
focus(): void | ||
blur(): void | ||
push(value?: any): any[] | ||
pop(): any[] | ||
insert(index: number, value: any): any[] | ||
remove(index: number | string): any | ||
unshift(value: any): any[] | ||
shift(): any[] | ||
move($from: number, $to: number): any[] | ||
moveDown(index: number): any[] | ||
moveUp(index: number): any[] | ||
validate( | ||
opts?: IFormExtendedValidateFieldOptions | ||
): Promise<IFormValidateResult> | ||
exist(index?: number | string): boolean | ||
} | ||
export type IMutators = ReturnType<IForm['createMutators']> | ||
@@ -348,15 +334,8 @@ export interface IModel<S = {}, P = {}> extends Subscribable { | ||
setSourceState: (callback?: (state: S) => void) => void | ||
watchProps: <T extends { [key: string]: any }>( | ||
props: T, | ||
keys: string[], | ||
callback: ( | ||
changedProps: { | ||
[key: string]: any | ||
}, | ||
props: T | ||
) => void | ||
) => void | ||
hasChanged: (path?: FormPathPattern) => boolean | ||
isDirty: (key?: string) => boolean | ||
getDirtyInfo: () => StateDirtyMap<S> | ||
setCache: (key: string, value: any) => void | ||
getCache: (key: string) => any | ||
removeCache: (key: string) => void | ||
} | ||
@@ -370,39 +349,2 @@ | ||
export interface IForm { | ||
submit( | ||
onSubmit?: (values: IFormState['values']) => any | Promise<any> | ||
): Promise<IFormSubmitResult> | ||
clearErrors: (pattern?: FormPathPattern) => void | ||
hasChanged(target: any, path: FormPathPattern): boolean | ||
reset(options?: IFormResetOptions): Promise<void | IFormValidateResult> | ||
validate( | ||
path?: FormPathPattern, | ||
options?: IFormExtendedValidateFieldOptions | ||
): Promise<IFormValidateResult> | ||
setFormState(callback?: (state: IFormState) => any, silent?: boolean): void | ||
getFormState(callback?: (state: IFormState) => any): any | ||
setFieldState( | ||
path: FormPathPattern, | ||
callback?: (state: IFieldUserState) => void, | ||
silent?: boolean | ||
): void | ||
getFieldState( | ||
path: FormPathPattern, | ||
callback?: (state: IFieldState) => any | ||
): any | ||
unsafe_do_not_use_transform_data_path(path: FormPathPattern): FormPathPattern //eslint-disable-line | ||
registerField(props: IFieldStateProps): IField | ||
registerVirtualField(props: IVirtualFieldStateProps): IVirtualField | ||
createMutators(field: IField): IMutators | ||
getFormGraph(): IFormGraph | ||
setFormGraph(graph: IFormGraph): void | ||
subscribe(callback?: FormHeartSubscriber): number | ||
unsubscribe(id: number): void | ||
notify: <T>(type: string, payload?: T) => void | ||
isHostRendering: () => boolean | ||
hostUpdate: (callback?: () => any) => any | ||
setFieldValue(path?: FormPathPattern, value?: any): void | ||
getFieldValue(path?: FormPathPattern): any | ||
setFieldInitialValue(path?: FormPathPattern, value?: any): void | ||
getFieldInitialValue(path?: FormPathPattern): any | ||
} | ||
export type IForm = ReturnType<typeof createForm> |
910
244308
5247
Updated@formily/shared@^1.0.9
Updated@formily/validator@^1.0.9