@formily/core
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -7,3 +7,13 @@ import { FormPath, FormPathPattern, BigData } from '@formily/shared'; | ||
export * from './types'; | ||
export declare function createForm<FieldProps, VirtualFieldProps>(options?: IFormCreatorOptions): { | ||
declare global { | ||
namespace FormilyCore { | ||
interface FieldProps { | ||
[key: string]: any; | ||
} | ||
interface VirtualFieldProps { | ||
[key: string]: any; | ||
} | ||
} | ||
} | ||
export declare function createForm(options?: IFormCreatorOptions): { | ||
submit: (onSubmit?: (values: {}) => any) => Promise<IFormSubmitResult>; | ||
@@ -16,4 +26,4 @@ reset: ({ selector, forceClear, validate, clearInitialValue }?: IFormResetOptions) => Promise<void | import("@formily/validator").ValidateNodeResult>; | ||
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; | ||
setFieldState: (path: FormPathPattern, callback?: (state: IFieldState<FormilyCore.FieldProps>) => void, silent?: boolean) => void; | ||
getFieldState: (path: FormPathPattern, callback?: (state: IFieldState<FormilyCore.FieldProps>) => any) => any; | ||
registerField: ({ path, name, value, initialValue, required, rules, editable, visible, display, computeState, dataType, useDirty, unmountRemoveValue, props }: IFieldStateProps<any>) => IField; | ||
@@ -20,0 +30,0 @@ registerVirtualField: ({ name, path, props, display, visible, computeState, useDirty }: IVirtualFieldStateProps<any>) => IVirtualField; |
@@ -470,7 +470,12 @@ "use strict"; | ||
var formInitialValue = getFormInitialValuesIn(state.name); | ||
if (shared_1.isValid(initialValue)) { | ||
state.initialValue = initialValue; | ||
} | ||
else if (shared_1.isValid(formInitialValue)) { | ||
state.initialValue = formInitialValue; | ||
} | ||
if (shared_1.isValid(value)) { | ||
state.value = value; | ||
} | ||
else if (existFormValuesIn(state.name) || | ||
formValue !== undefined) { | ||
else if (existFormValuesIn(state.name) || !shared_1.isEmpty(formValue)) { | ||
state.value = formValue; | ||
@@ -481,8 +486,2 @@ } | ||
} | ||
if (shared_1.isValid(initialValue)) { | ||
state.initialValue = initialValue; | ||
} | ||
else if (shared_1.isValid(formInitialValue)) { | ||
state.initialValue = formInitialValue; | ||
} | ||
if (shared_1.isValid(visible)) { | ||
@@ -489,0 +488,0 @@ state.visible = visible; |
{ | ||
"name": "@formily/core", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"license": "MIT", | ||
@@ -28,4 +28,4 @@ "main": "lib", | ||
"dependencies": { | ||
"@formily/shared": "^1.1.1", | ||
"@formily/validator": "^1.1.1", | ||
"@formily/shared": "^1.1.2", | ||
"@formily/validator": "^1.1.2", | ||
"immer": "^6.0.3" | ||
@@ -36,3 +36,3 @@ }, | ||
}, | ||
"gitHead": "02ca670ede75710c82a56bf1447b69824c4c92be" | ||
"gitHead": "e245f051a816e47d1ee7699f04540b53a81db4c9" | ||
} |
109
src/index.ts
@@ -10,2 +10,3 @@ import { | ||
isValid, | ||
isEmpty, | ||
FormPath, | ||
@@ -51,5 +52,14 @@ FormPathPattern, | ||
export function createForm<FieldProps, VirtualFieldProps>( | ||
options: IFormCreatorOptions = {} | ||
) { | ||
declare global { | ||
namespace FormilyCore { | ||
export interface FieldProps { | ||
[key: string]: any | ||
} | ||
export interface VirtualFieldProps { | ||
[key: string]: any | ||
} | ||
} | ||
} | ||
export function createForm(options: IFormCreatorOptions = {}) { | ||
function onGraphChange({ type, payload }) { | ||
@@ -200,5 +210,7 @@ heart.publish(LifeCycleTypes.ON_FORM_GRAPH_CHANGE, graph) | ||
parentState: | ||
| IVirtualFieldState<VirtualFieldProps> | ||
| IFieldState<FieldProps>, | ||
childState: IVirtualFieldState<VirtualFieldProps> | IFieldState<FieldProps>, | ||
| IVirtualFieldState<FormilyCore.VirtualFieldProps> | ||
| IFieldState<FormilyCore.FieldProps>, | ||
childState: | ||
| IVirtualFieldState<FormilyCore.VirtualFieldProps> | ||
| IFieldState<FormilyCore.FieldProps>, | ||
name: 'visible' | 'display' | ||
@@ -257,3 +269,3 @@ ) { | ||
} | ||
return (published: IFieldState<FieldProps>) => { | ||
return (published: IFieldState<FormilyCore.FieldProps>) => { | ||
const valueChanged = field.isDirty('value') | ||
@@ -275,3 +287,3 @@ const initialValueChanged = field.isDirty('initialValue') | ||
if (isEmptyValue || isEmptyInitialValue) { | ||
field.setSourceState((state: IFieldState<FieldProps>) => { | ||
field.setSourceState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
if (isEmptyValue) { | ||
@@ -310,5 +322,7 @@ const formValue = getFormValuesIn(state.name) | ||
if (isValid(published.value)) { | ||
field.setSourceState((state: IFieldState<FieldProps>) => { | ||
state.visibleCacheValue = published.value | ||
}) | ||
field.setSourceState( | ||
(state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.visibleCacheValue = published.value | ||
} | ||
) | ||
} | ||
@@ -331,3 +345,3 @@ deleteFormValuesIn(path) | ||
graph.eachChildren(path, childState => { | ||
childState.setState((state: IFieldState<FieldProps>) => { | ||
childState.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
if (visibleChanged) { | ||
@@ -349,5 +363,7 @@ updateRecoverableShownState(published, state, 'visible') | ||
if (isValid(published.value)) { | ||
field.setSourceState((state: IFieldState<FieldProps>) => { | ||
state.visibleCacheValue = published.value | ||
}) | ||
field.setSourceState( | ||
(state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.visibleCacheValue = published.value | ||
} | ||
) | ||
} | ||
@@ -397,3 +413,3 @@ deleteFormValuesIn(path, true) | ||
function onVirtualFieldChange({ field, path }) { | ||
return (published: IVirtualFieldState<VirtualFieldProps>) => { | ||
return (published: IVirtualFieldState<FormilyCore.VirtualFieldProps>) => { | ||
const visibleChanged = field.isDirty('visible') | ||
@@ -411,3 +427,3 @@ const displayChanged = field.isDirty('display') | ||
childState.setState( | ||
(state: IVirtualFieldState<VirtualFieldProps>) => { | ||
(state: IVirtualFieldState<FormilyCore.VirtualFieldProps>) => { | ||
if (visibleChanged) { | ||
@@ -464,12 +480,14 @@ updateRecoverableShownState(published, state, 'visible') | ||
field.batch(() => { | ||
field.setState((state: IVirtualFieldState<VirtualFieldProps>) => { | ||
state.initialized = true | ||
state.props = props | ||
if (isValid(visible)) { | ||
state.visible = visible | ||
field.setState( | ||
(state: IVirtualFieldState<FormilyCore.VirtualFieldProps>) => { | ||
state.initialized = true | ||
state.props = props | ||
if (isValid(visible)) { | ||
state.visible = visible | ||
} | ||
if (isValid(display)) { | ||
state.display = display | ||
} | ||
} | ||
if (isValid(display)) { | ||
state.display = display | ||
} | ||
}) | ||
) | ||
batchRunTaskQueue(field, nodePath) | ||
@@ -533,12 +551,17 @@ }) | ||
field.batch(() => { | ||
field.setState((state: IFieldState<FieldProps>) => { | ||
field.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
const formValue = getFormValuesIn(state.name) | ||
const formInitialValue = getFormInitialValuesIn(state.name) | ||
if (isValid(initialValue)) { | ||
state.initialValue = initialValue | ||
} else if (isValid(formInitialValue)) { | ||
state.initialValue = formInitialValue | ||
} | ||
if (isValid(value)) { | ||
// value > formValue > initialValue | ||
state.value = value | ||
} else if ( | ||
existFormValuesIn(state.name) || | ||
formValue !== undefined | ||
) { | ||
} else if (existFormValuesIn(state.name) || !isEmpty(formValue)) { | ||
//to resolve empty array checker | ||
state.value = formValue | ||
@@ -549,8 +572,2 @@ } else if (isValid(initialValue)) { | ||
if (isValid(initialValue)) { | ||
state.initialValue = initialValue | ||
} else if (isValid(formInitialValue)) { | ||
state.initialValue = formInitialValue | ||
} | ||
if (isValid(visible)) { | ||
@@ -610,3 +627,3 @@ state.visible = visible | ||
return new Promise(resolve => { | ||
field.setState((state: IFieldState<FieldProps>) => { | ||
field.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.validating = false | ||
@@ -803,3 +820,3 @@ state.ruleErrors = errors | ||
function setValue(...values: any[]) { | ||
field.setState((state: IFieldState<FieldProps>) => { | ||
field.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.value = values[0] | ||
@@ -891,3 +908,3 @@ state.values = values | ||
focus() { | ||
field.setState((state: IFieldState<FieldProps>) => { | ||
field.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.active = true | ||
@@ -897,3 +914,3 @@ }) | ||
blur() { | ||
field.setState((state: IFieldState<FieldProps>) => { | ||
field.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.active = false | ||
@@ -1013,3 +1030,3 @@ state.visited = true | ||
;(field as any).disabledValidate = true | ||
field.setState((state: IFieldState<FieldProps>) => { | ||
field.setState((state: IFieldState<FormilyCore.FieldProps>) => { | ||
state.modified = false | ||
@@ -1249,3 +1266,3 @@ state.ruleErrors = [] | ||
path: FormPathPattern, | ||
callback?: (state: IFieldState<FieldProps>) => void, | ||
callback?: (state: IFieldState<FormilyCore.FieldProps>) => void, | ||
silent?: boolean | ||
@@ -1303,3 +1320,3 @@ ) { | ||
path: FormPathPattern, | ||
callback?: (state: IFieldState<FieldProps>) => any | ||
callback?: (state: IFieldState<FormilyCore.FieldProps>) => any | ||
) { | ||
@@ -1320,3 +1337,5 @@ const field = graph.select(path) | ||
( | ||
node: IFieldState<FieldProps> | IVirtualFieldState<VirtualFieldProps>, | ||
node: | ||
| IFieldState<FormilyCore.FieldProps> | ||
| IVirtualFieldState<FormilyCore.VirtualFieldProps>, | ||
key | ||
@@ -1323,0 +1342,0 @@ ) => { |
250064
5413
Updated@formily/shared@^1.1.2
Updated@formily/validator@^1.1.2