Skip to content

Commit

Permalink
chore(core): improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 30, 2021
1 parent 8bfb713 commit 9e80189
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 43 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ English | [简体中文](./README.zh-cn.md)
<img src="https://img.alicdn.com/tfs/TB1fHhZu4D1gK0jSZFyXXciOVXa-2500-1200.png">
<img src="https://img.shields.io/npm/dt/@formily/antd"/>
<a href="https://www.npmjs.com/package/@formily/core"><img src="https://img.shields.io/npm/v/@formily/core.svg"></a>
<a href="https://www.npmjs.com/package/@formily/react"><img src="https://img.shields.io/npm/v/@formily/react.svg"></a>
<a href="https://travis-ci.com/alibaba/formily"><img src="https://travis-ci.com/alibaba/formily.svg?branch=master"></a>
<a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg"></a>
<a href="https://codecov.io/gh/alibaba/formily">
<img src="https://codecov.io/gh/alibaba/formily/branch/formily_next/graph/badge.svg?token=3V9RU8Wh9d"/>
</a>
<img alt="PRs Welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg"/>
<a href="https://app.netlify.com/sites/formily/deploys"><img src="https://api.netlify.com/api/v1/badges/7145918b-9cb5-47f8-8a42-111969e232ef/deploy-status"/></a>
</p>

Expand Down
11 changes: 6 additions & 5 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[English](./README.md) | 简体中文

<p align="center">
<img src="https://img.alicdn.com/tfs/TB1DIpUu7L0gK0jSZFxXXXWHVXa-2500-1200.png">
<img src="https://img.shields.io/npm/dt/@formily/core"/>
<img src="https://img.alicdn.com/tfs/TB1fHhZu4D1gK0jSZFyXXciOVXa-2500-1200.png">
<img src="https://img.shields.io/npm/dt/@formily/antd"/>
<a href="https://www.npmjs.com/package/@formily/core"><img src="https://img.shields.io/npm/v/@formily/core.svg"></a>
<a href="https://www.npmjs.com/package/@formily/react"><img src="https://img.shields.io/npm/v/@formily/react.svg"></a>
<a href="https://travis-ci.com/alibaba/formily"><img src="https://travis-ci.com/alibaba/formily.svg?branch=master"></a>
<a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg"></a>
<a href="https://codecov.io/gh/alibaba/formily">
<img src="https://codecov.io/gh/alibaba/formily/branch/formily_next/graph/badge.svg?token=3V9RU8Wh9d"/>
</a>
<img alt="PRs Welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg"/>
<a href="https://app.netlify.com/sites/formily/deploys"><img src="https://api.netlify.com/api/v1/badges/7145918b-9cb5-47f8-8a42-111969e232ef/deploy-status"/></a>
</p>

Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
import { Form } from './Form'
import {
JSXComponent,
JSXComponenntProps,
LifeCycleTypes,
IFieldFeedback,
FeedbackMessage,
Expand Down Expand Up @@ -229,7 +228,7 @@ export class Field<
() => this.value,
(value) => {
this.form.notify(LifeCycleTypes.ON_FIELD_VALUE_CHANGE, this)
if (isValid(value) && this.modified && !this.caches.inputing) {
if (isValid(value) && this.modified && !this.caches.inputting) {
this.validate()
}
}
Expand Down Expand Up @@ -614,9 +613,9 @@ export class Field<
}
}

setComponent = <C extends JSXComponent>(
setComponent = <C extends JSXComponent, ComponentProps extends object = {}>(
component?: C,
props?: JSXComponenntProps<C>
props?: ComponentProps
) => {
if (component) {
this.componentType = component as any
Expand All @@ -627,18 +626,18 @@ export class Field<
}
}

setComponentProps = <C extends JSXComponent = Component>(
props?: JSXComponenntProps<C>
setComponentProps = <ComponentProps extends object = {}>(
props?: ComponentProps
) => {
if (props) {
this.componentProps = this.componentProps || {}
Object.assign(this.componentProps, props)
}
}

setDecorator = <D extends JSXComponent>(
setDecorator = <D extends JSXComponent, ComponentProps extends object = {}>(
component?: D,
props?: JSXComponenntProps<D>
props?: ComponentProps
) => {
if (component) {
this.decoratorType = component as any
Expand All @@ -649,8 +648,8 @@ export class Field<
}
}

setDecoratorProps = <D extends JSXComponent = Decorator>(
props?: JSXComponenntProps<D>
setDecoratorProps = <ComponentProps extends object = {}>(
props?: ComponentProps
) => {
if (props) {
this.decoratorProps = this.decoratorProps || {}
Expand Down Expand Up @@ -691,7 +690,7 @@ export class Field<
}
const values = getValuesFromEvent(args)
const value = values[0]
this.caches.inputing = true
this.caches.inputting = true
this.inputValue = value
this.inputValues = values
this.value = value
Expand All @@ -700,7 +699,7 @@ export class Field<
this.form.notify(LifeCycleTypes.ON_FIELD_INPUT_VALUE_CHANGE, this)
this.form.notify(LifeCycleTypes.ON_FORM_INPUT_CHANGE, this.form)
await this.validate('onInput')
this.caches.inputing = false
this.caches.inputting = false
}

onFocus = async (...args: any[]) => {
Expand Down
17 changes: 8 additions & 9 deletions packages/core/src/models/VoidField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { define, observable, autorun, batch } from '@formily/reactive'
import {
JSXComponent,
JSXComponenntProps,
LifeCycleTypes,
FieldDisplayTypes,
FieldPatternTypes,
Expand Down Expand Up @@ -288,9 +287,9 @@ export class VoidField<Decorator = any, Component = any, TextType = any> {
this.pattern = type
}

setComponent = <C extends JSXComponent>(
setComponent = <C extends JSXComponent, ComponentProps extends object = {}>(
component?: C,
props?: JSXComponenntProps<C>
props?: ComponentProps
) => {
if (component) {
this.componentType = component as any
Expand All @@ -301,18 +300,18 @@ export class VoidField<Decorator = any, Component = any, TextType = any> {
}
}

setComponentProps = <C extends JSXComponent = Component>(
props?: JSXComponenntProps<C>
setComponentProps = <ComponentProps extends object = {}>(
props?: ComponentProps
) => {
if (props) {
this.componentProps = this.componentProps || {}
Object.assign(this.componentProps, props)
}
}

setDecorator = <D extends JSXComponent>(
setDecorator = <D extends JSXComponent, ComponentProps extends object = {}>(
component?: D,
props?: JSXComponenntProps<D>
props?: ComponentProps
) => {
if (component) {
this.decoratorType = component as any
Expand All @@ -323,8 +322,8 @@ export class VoidField<Decorator = any, Component = any, TextType = any> {
}
}

setDecoratorProps = <D extends JSXComponent = Decorator>(
props?: JSXComponenntProps<D>
setDecoratorProps = <ComponentProps extends object = {}>(
props?: ComponentProps
) => {
if (props) {
this.decoratorProps = this.decoratorProps || {}
Expand Down
24 changes: 10 additions & 14 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export type AnyFunction = (...args: any[]) => any

export type JSXComponent = any

export type JSXComponenntProps<P> = Record<string, any>

export type LifeCycleHandler<T> = (payload: T, context: any) => void

export type LifeCyclePayload<T> = (
Expand Down Expand Up @@ -266,7 +264,7 @@ export interface IFieldCaches {
value?: any
initialValue?: any
feedbacks?: IFieldFeedback[]
inputing?: boolean
inputting?: boolean
}

export type FieldDisplayTypes = 'none' | 'hidden' | 'visible' | ({} & string)
Expand All @@ -290,17 +288,15 @@ export type FieldDataSource = {
[key: string]: any
}[]

export type FieldComponent<Component extends JSXComponent> =
| [Component]
| [Component, JSXComponenntProps<Component>]
| boolean
| any[]

export type FieldDecorator<Decorator extends JSXComponent> =
| [Decorator]
| [Decorator, JSXComponenntProps<Decorator>]
| boolean
| any[]
export type FieldComponent<
Component extends JSXComponent,
ComponentProps = any
> = [Component] | [Component, ComponentProps] | boolean | any[]

export type FieldDecorator<
Decorator extends JSXComponent,
ComponentProps = any
> = [Decorator] | [Decorator, ComponentProps] | boolean | any[]

export type FieldReaction = (field: Field) => void
export interface IFieldProps<
Expand Down
6 changes: 6 additions & 0 deletions scripts/rollup.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export default (filename, targetName, ...plugins) => [
format: 'umd',
file: `dist/${filename}.umd.development.js`,
name: targetName,
amd: {
id: filename,
},
},
plugins: [...presets(filename, targetName), ...plugins],
},
Expand All @@ -74,6 +77,9 @@ export default (filename, targetName, ...plugins) => [
format: 'umd',
file: `dist/${filename}.umd.production.js`,
name: targetName,
amd: {
id: filename,
},
},
plugins: [...presets(filename, targetName), terser(), ...plugins],
},
Expand Down

0 comments on commit 9e80189

Please sign in to comment.