Skip to content

Commit

Permalink
feat(@uform/types): improve types (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jul 15, 2019
1 parent c12ecb9 commit 0e6fd69
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class Form {

public setFieldState = (
path: Path | IFormPathMatcher,
callback?: () => void
callback?: (fieldState: IFieldState) => void
) => {
if (this.destructed) {
return
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/shared/context.tsx
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/state/field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<StateField
name={name}
path={path}
form={form}
broadcast={broadcast}
schema={getSchema(schemaPath || path)}
locale={locale}
getSchema={getSchema}
Expand All @@ -172,10 +173,10 @@ export const FormField = StateField()((props: IFieldProps) => {
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
Expand Down
29 changes: 17 additions & 12 deletions packages/react/src/state/form.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -301,6 +305,7 @@ export const StateForm = createHOC((options, Form) => {
value={{
locale,
form: this.form,
actions: this.getActions(this.form),
getSchema: this.getSchema,
broadcast
}}
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface IFormOptions {
export interface IFormActions {
setFieldState: (
name: Path | IFormPathMatcher,
callback: (fieldState: IFieldState) => void
callback?: (fieldState: IFieldState) => void
) => Promise<any>
getFieldState: (
name: Path | IFormPathMatcher,
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 0e6fd69

Please sign in to comment.