diff --git a/packages/core/src/form.ts b/packages/core/src/form.ts index 899129c3769..b7979338e2f 100644 --- a/packages/core/src/form.ts +++ b/packages/core/src/form.ts @@ -125,7 +125,7 @@ export class Form { public setFieldState = ( path: Path | IFormPathMatcher, - callback?: () => void + callback?: (fieldState: IFieldState) => void ) => { if (this.destructed) { return @@ -188,7 +188,7 @@ export class Form { return Promise.resolve(this.checkState(published)) } - public registerField(name, options) { + public registerField(name: string, options: IFieldOptions) { const value = this.getValue(name) const initialValue = this.getInitialValue(name, options.path) const field = this.fields[name] diff --git a/packages/react/src/shared/context.tsx b/packages/react/src/shared/context.tsx index ab3b01143d2..db019bed755 100644 --- a/packages/react/src/shared/context.tsx +++ b/packages/react/src/shared/context.tsx @@ -1,11 +1,12 @@ import React from 'react' import { Form } from '@uform/core' -import { ISchema } from '@uform/types' +import { ISchema, IFormActions } from '@uform/types' import { IBroadcast } from '@uform/utils' export interface IStateContext { getSchema: (path: string) => ISchema form: Form + actions: IFormActions locale: { [key: string]: any } broadcast: IBroadcast } diff --git a/packages/react/src/state/field.tsx b/packages/react/src/state/field.tsx index 5463c27983b..b801ea14d57 100644 --- a/packages/react/src/state/field.tsx +++ b/packages/react/src/state/field.tsx @@ -153,12 +153,13 @@ const StateField = createHOC((options, Field) => { return props => { const { name, path, schemaPath } = props - const { form, getSchema, locale } = useContext(StateContext) + const { form, getSchema, locale, broadcast } = useContext(StateContext) return ( { const schema = props.schema const fieldComponentName = lowercase(schema['x-component'] || schema.type) const renderComponent = schema['x-render'] - ? $props => { + ? (innerProps: any) => { return React.createElement(getFormField(fieldComponentName), { ...props, - ...$props, + ...innerProps, schema, path: props.path, name: props.name diff --git a/packages/react/src/state/form.tsx b/packages/react/src/state/form.tsx index d85951c9be6..e17dfaee051 100644 --- a/packages/react/src/state/form.tsx +++ b/packages/react/src/state/form.tsx @@ -1,7 +1,7 @@ import React, { Component } from 'react' import { connect } from 'react-eva' import { createForm, Form } from '@uform/core' -import { IFormState } from '@uform/types' +import { IFormState, IFormActions } from '@uform/types' import { createHOC, @@ -47,23 +47,27 @@ export const StateForm = createHOC((options, Form) => { onValidateFailed: this.onValidateFailed(props), onReset: this.onResetHandler(props), onFormWillInit: form => { - props.implementActions({ - setFormState: form.setFormState, - getFormState: form.getFormState, - setFieldState: form.setFieldState, - getFieldState: form.getFieldState, - reset: this.reset, - submit: this.submit, - validate: this.validate, - getSchema: this.getSchema, - dispatch: this.dispatch - }) + props.implementActions(this.getActions(form)) } }) this.state = {} as IFormState this.initialized = true } + public getActions(form: Form): IFormActions { + return { + setFormState: form.setFormState, + getFormState: form.getFormState, + setFieldState: form.setFieldState, + getFieldState: form.getFieldState, + reset: this.reset, + submit: this.submit, + validate: this.validate, + getSchema: this.getSchema, + dispatch: this.dispatch + } + } + public notify(payload) { const { broadcast, schema } = this.props if (broadcast) { @@ -301,6 +305,7 @@ export const StateForm = createHOC((options, Form) => { value={{ locale, form: this.form, + actions: this.getActions(this.form), getSchema: this.getSchema, broadcast }} diff --git a/packages/react/src/type.ts b/packages/react/src/type.ts index 6e6f04bfb58..667daeb93a1 100644 --- a/packages/react/src/type.ts +++ b/packages/react/src/type.ts @@ -30,6 +30,7 @@ export interface IStateFieldProps { schemaPath: any locale: { [key: string]: any } getSchema: (path: string) => ISchema + broadcast: IBroadcast form: Form // TODO mutators 文件应该暴露出来 interface mutators?: any diff --git a/packages/types/src/form.ts b/packages/types/src/form.ts index 512a99b03f2..941c114f038 100644 --- a/packages/types/src/form.ts +++ b/packages/types/src/form.ts @@ -52,7 +52,7 @@ export interface IFormOptions { export interface IFormActions { setFieldState: ( name: Path | IFormPathMatcher, - callback: (fieldState: IFieldState) => void + callback?: (fieldState: IFieldState) => void ) => Promise getFieldState: ( name: Path | IFormPathMatcher, diff --git a/packages/utils/src/clone.ts b/packages/utils/src/clone.ts index 23626f2bccb..8a5ca33a725 100644 --- a/packages/utils/src/clone.ts +++ b/packages/utils/src/clone.ts @@ -50,6 +50,9 @@ export const clone = (values: any, filter?: Filter) => { if ('$$typeof' in values && '_owner' in values) { return values } + if (Object.getOwnPropertySymbols(values || {}).length) { + return values + } const res = {} for (const key in values) { if (Object.hasOwnProperty.call(values, key)) {