Skip to content

Commit

Permalink
chore(project): improve typings and fix typo (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jul 14, 2021
1 parent 837cfc0 commit af23767
Show file tree
Hide file tree
Showing 28 changed files with 134 additions and 203 deletions.
12 changes: 6 additions & 6 deletions packages/core/docs/api/entry/FormValidatorRegistry.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ order: 6
#### 签名

```ts
interface setValidateLanguage{
interface setValidateLanguage {
(language: string): void
}
```

#### 用例

```ts
import { setValidateLanguage} from '@formily/core'
import { setValidateLanguage } from '@formily/core'

setValidationLanguage('en-US')

Expand Down Expand Up @@ -82,7 +82,7 @@ registerValidateLocale({
})
```

## registerValidateMessageTemplateEnigne
## registerValidateMessageTemplateEngine

#### 描述

Expand All @@ -91,17 +91,17 @@ registerValidateLocale({
#### 签名

```ts
interface registerValidateMessageTemplateEnigne {
interface registerValidateMessageTemplateEngine {
(template: (message: ValidatorFunctionResponse, context: any) => any): void
}
```

#### 用例

```ts
import { registerValidateMessageTemplateEnigne } from '@formily/core'
import { registerValidateMessageTemplateEngine } from '@formily/core'

registerValidateMessageTemplateEnigne((message, context) => {
registerValidateMessageTemplateEngine((message, context) => {
return message.replace(/\<\%\s*([\w.]+)\s*\%\>/g, (_, $0) => {
return FormPath.getIn(context, $0)
})
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './shared/externals'
export * from './models/types'
export * from './effects'
export * from './types'
9 changes: 9 additions & 0 deletions packages/core/src/models/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type { Form } from './Form'
export type { Field } from './Field'
export type { Query } from './Query'
export type { Heart } from './Heart'
export type { Graph } from './Graph'
export type { LifeCycle } from './LifeCycle'
export type { ArrayField } from './ArrayField'
export type { ObjectField } from './ObjectField'
export type { VoidField } from './VoidField'
4 changes: 2 additions & 2 deletions packages/core/src/shared/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
setValidateLanguage,
registerValidateFormats,
registerValidateLocale,
registerValidateMessageTemplateEnigne,
registerValidateMessageTemplateEngine,
registerValidateRules,
} from '@formily/validator'
import {
Expand Down Expand Up @@ -58,7 +58,7 @@ export {
setValidateLanguage,
registerValidateFormats,
registerValidateLocale,
registerValidateMessageTemplateEnigne,
registerValidateMessageTemplateEngine,
registerValidateRules,
createEffectHook,
createEffectContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext, Fragment } from 'react'
import { ISchema, Schema } from '@formily/json-schema'
import { RecursionField } from '../components'
import { RecursionField } from '.'
import { render } from '../shared/render'
import {
SchemaMarkupContext,
Expand All @@ -10,8 +10,8 @@ import {
import {
ReactComponentPath,
JSXComponent,
ISchemaFieldFactoryOptions,
SchemaComponents,
ISchemaFieldReactFactoryOptions,
SchemaReactComponents,
ISchemaFieldProps,
ISchemaMarkupFieldProps,
ISchemaTypeFieldProps,
Expand All @@ -24,8 +24,8 @@ const getRandomName = () => {
return `NO_NAME_FIELD_$${env.nonameId++}`
}

export function createSchemaField<Components extends SchemaComponents>(
options: ISchemaFieldFactoryOptions<Components> = {}
export function createSchemaField<Components extends SchemaReactComponents>(
options: ISchemaFieldReactFactoryOptions<Components> = {}
) {
function SchemaField<
Decorator extends JSXComponent,
Expand Down Expand Up @@ -120,7 +120,6 @@ export function createSchemaField<Components extends SchemaComponents>(
Decorator extends ReactComponentPath<Components>,
Component extends ReactComponentPath<Components>
>(props: ISchemaTypeFieldProps<Components, Component, Decorator>) {
//@ts-ignore
return <MarkupField {...props} type="string" />
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export * from './ArrayField'
export * from './ObjectField'
export * from './VoidField'
export * from './RecursionField'
export * from './SchemaFieid'
export * from './SchemaField'
export * from './Field'
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from '@formily/json-schema'
export * from './components'
export * from './shared'
export * from './hooks'
export * from './types'
4 changes: 2 additions & 2 deletions packages/react/src/shared/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { createContext } from 'react'
import { Schema } from '@formily/json-schema'
import { ISchemaFieldFactoryOptions } from '../types'
import { ISchemaFieldReactFactoryOptions } from '../types'

const createContextCleaner = <T>(...contexts: React.Context<T>[]) => {
return ({ children }) => {
Expand All @@ -16,7 +16,7 @@ export const SchemaMarkupContext = createContext<Schema>(null)
export const SchemaContext = createContext<Schema>(null)
export const SchemaExpressionScopeContext = createContext<any>(null)
export const SchemaOptionsContext =
createContext<ISchemaFieldFactoryOptions>(null)
createContext<ISchemaFieldReactFactoryOptions>(null)

export const ContextCleaner = createContextCleaner(
FieldContext,
Expand Down
38 changes: 15 additions & 23 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export type IStateMapper<Props> =
}
| ((props: Props, field: Formily.Core.Types.GeneralField) => Props)

export type SchemaComponents = Record<string, JSXComponent>
export type SchemaReactComponents = Record<string, JSXComponent>

export interface ISchemaFieldFactoryOptions<
Components extends SchemaComponents = any
export interface ISchemaFieldReactFactoryOptions<
Components extends SchemaReactComponents = any
> {
components?: Components
scope?: any
Expand All @@ -73,12 +73,6 @@ export interface ISchemaFieldProps<
children?: React.ReactNode
}

export interface ISchemaFieldUpdateRequest {
state?: Formily.Core.Types.IFieldState
schema?: ISchema
run?: string
}

export interface ISchemaMapper {
(schema: Schema, name: SchemaKey): Schema
}
Expand Down Expand Up @@ -108,16 +102,18 @@ export type Path<T, Key extends keyof T = keyof T> = Key extends string
: Key
: never

export type PathValue<T, P extends Path<T>> =
P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
export type PathValue<
T,
P extends Path<T>
> = P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: P extends keyof T
? T[P]
: never
: P extends keyof T
? T[P]
: never

export type KeyOfReactComponent<T> = Exclude<
keyof T,
Expand Down Expand Up @@ -149,7 +145,7 @@ export type ReactComponentPropsByPathValue<
? React.ComponentProps<T[P]>
: never
export interface ISchemaMarkupFieldProps<
Components extends SchemaComponents,
Components extends SchemaReactComponents,
Decorator extends ReactComponentPath<Components>,
Component extends ReactComponentPath<Components>
> extends ISchema<
Expand All @@ -167,11 +163,7 @@ export interface ISchemaMarkupFieldProps<
}

export type ISchemaTypeFieldProps<
Components extends SchemaComponents,
Components extends SchemaReactComponents,
Decorator extends ReactComponentPath<Components>,
Component extends ReactComponentPath<Components>
> = Omit<ISchemaMarkupFieldProps<Components, Decorator, Component>, 'type'>

export interface ISchemaTransformerOptions extends ISchemaFieldFactoryOptions {
required?: ISchema['required']
}
48 changes: 2 additions & 46 deletions packages/reactive-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,2 @@
import React, { forwardRef, memo, Fragment } from 'react'
import hoistNonReactStatics from 'hoist-non-react-statics'
import { useObserver } from './hooks'
import { IObserverOptions, IObserverProps } from './types'

export function observer<P, Options extends IObserverOptions>(
component: React.FunctionComponent<P>,
options?: Options
): React.MemoExoticComponent<
React.FunctionComponent<
Options extends { forwardRef: true }
? P & {
ref?: 'ref' extends keyof P ? P['ref'] : React.RefAttributes<any>
}
: React.PropsWithoutRef<P>
>
> {
const realOptions = {
forwardRef: false,
...options,
}

const wrappedComponent = realOptions.forwardRef
? forwardRef((props: any, ref: any) => {
return useObserver(() => component({ ...props, ref }), realOptions)
})
: (props: any) => {
return useObserver(() => component(props), realOptions)
}

const memoComponent = memo(wrappedComponent)

hoistNonReactStatics(memoComponent, component)

if (realOptions.displayName) {
memoComponent.displayName = realOptions.displayName
}

return memoComponent
}

export const Observer = observer((props: IObserverProps) => {
const children =
typeof props.children === 'function' ? props.children() : props.children
return React.createElement(Fragment, {}, children)
})
export * from './observer'
export * from './types'
46 changes: 46 additions & 0 deletions packages/reactive-react/src/observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { forwardRef, memo, Fragment } from 'react'
import hoistNonReactStatics from 'hoist-non-react-statics'
import { useObserver } from './hooks'
import { IObserverOptions, IObserverProps } from './types'

export function observer<P, Options extends IObserverOptions>(
component: React.FunctionComponent<P>,
options?: Options
): React.MemoExoticComponent<
React.FunctionComponent<
Options extends { forwardRef: true }
? P & {
ref?: 'ref' extends keyof P ? P['ref'] : React.RefAttributes<any>
}
: React.PropsWithoutRef<P>
>
> {
const realOptions = {
forwardRef: false,
...options,
}

const wrappedComponent = realOptions.forwardRef
? forwardRef((props: any, ref: any) => {
return useObserver(() => component({ ...props, ref }), realOptions)
})
: (props: any) => {
return useObserver(() => component(props), realOptions)
}

const memoComponent = memo(wrappedComponent)

hoistNonReactStatics(memoComponent, component)

if (realOptions.displayName) {
memoComponent.displayName = realOptions.displayName
}

return memoComponent
}

export const Observer = observer((props: IObserverProps) => {
const children =
typeof props.children === 'function' ? props.children() : props.children
return React.createElement(Fragment, {}, children)
})
1 change: 1 addition & 0 deletions packages/reactive-vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './observer'
export * from './hooks'
export * from './types'
4 changes: 2 additions & 2 deletions packages/validator/src/__tests__/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
registerValidateRules,
registerValidateFormats,
setValidateLanguage,
registerValidateMessageTemplateEnigne,
registerValidateMessageTemplateEngine,
} from '../index'

registerValidateRules({
Expand Down Expand Up @@ -328,7 +328,7 @@ test('language', async () => {
})

test('validator template', async () => {
registerValidateMessageTemplateEnigne((message) => {
registerValidateMessageTemplateEngine((message) => {
if (typeof message !== 'string') return message
return message.replace(/\<\<\s*([\w.]+)\s*\>\>/g, (_, $0) => {
return { aa: 123 }[$0]
Expand Down
8 changes: 5 additions & 3 deletions packages/validator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const getBrowserlanguage = () => {
if (!self.navigator) {
return defaultLanguage
}
return self.navigator.browserlanguage || self.navigator.language || defaultLanguage
return (
self.navigator.browserlanguage || self.navigator.language || defaultLanguage
)
}

const registry = {
Expand Down Expand Up @@ -56,7 +58,7 @@ const getISOCode = (language: string) => {
return isoCode
}

export const getValidateLocaleIOSCode = getISOCode;
export const getValidateLocaleIOSCode = getISOCode

export const setValidateLanguage = (lang: string) => {
registry.locales.language = lang || defaultLanguage
Expand Down Expand Up @@ -104,7 +106,7 @@ export const registerValidateFormats = (formats: IRegistryFormats) => {
})
}

export const registerValidateMessageTemplateEnigne = (
export const registerValidateMessageTemplateEngine = (
template: (message: ValidatorFunctionResponse, context: any) => any
) => {
registry.template = template
Expand Down
Loading

0 comments on commit af23767

Please sign in to comment.