Skip to content

Commit

Permalink
docs(react): add react docs
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Feb 18, 2021
1 parent 7940cab commit 76b799a
Show file tree
Hide file tree
Showing 12 changed files with 1,190 additions and 45 deletions.
10 changes: 6 additions & 4 deletions packages/json-schema/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ISchema,
SchemaEnum,
SchemaProperties,
SchemaExtendReaction,
SchemaReaction,
SchemaTypes,
SchemaKey,
ISchemaTransformerOptions,
Expand Down Expand Up @@ -137,7 +137,7 @@ export class Schema<
//组件属性
['x-component-props']?: ComponentProps;

['x-reactions']?: SchemaExtendReaction<ReactionField>[];
['x-reactions']?: SchemaReaction<ReactionField>[];

['x-content']?: any;

Expand Down Expand Up @@ -196,8 +196,9 @@ export class Schema<
}

removeProperty = (key: SchemaKey) => {
const schema = this.properties[key]
delete this.properties[key]
return this
return schema
}

setProperties = (
Expand Down Expand Up @@ -239,8 +240,9 @@ export class Schema<
}

removePatternProperty = (key: SchemaKey) => {
const schema = this.patternProperties[key]
delete this.patternProperties[key]
return this
return schema
}

setPatternProperties = (
Expand Down
13 changes: 8 additions & 5 deletions packages/json-schema/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ISchema,
ISchemaFieldFactoryOptions,
ISchemaFieldUpdateRequest,
SchemaExtendReactions,
SchemaReactions,
} from './types'
import '@formily/core'

Expand Down Expand Up @@ -139,7 +139,9 @@ const getFieldInternalPropsBySchema = (
title: schema.title,
description: schema.description,
disabled: schema['x-disabled'],
editable: schema['x-editable'],
editable: isValid(schema['x-editable'])
? schema['x-editable']
: schema['writeOnly'],
readOnly: isValid(schema['x-read-only'])
? schema['x-read-only']
: schema['readOnly'],
Expand Down Expand Up @@ -219,7 +221,7 @@ const getSchemaFieldReactions = (
)
}
if (isStr(request.run)) {
compile(`async function(){${request.run}}`)()
compile(`{{async function(){${request.run}}}}`)()
}
})
}
Expand All @@ -240,7 +242,7 @@ const getSchemaFieldReactions = (
}

return (field: Formily.Core.Models.Field) => {
const reactions: SchemaExtendReactions = toArr(schema['x-reactions'])
const reactions: SchemaReactions = toArr(schema['x-reactions'])
reactions.forEach((reaction) => {
if (!reaction) return
if (isFn(reaction)) {
Expand All @@ -258,10 +260,11 @@ const getSchemaFieldReactions = (
$dependencies,
}
const when = Schema.compile(reaction?.when, scope)
const condition = isValid(when) ? when : true
const compile = (expression: any) => {
return Schema.compile(expression, scope)
}
if (when) {
if (condition) {
if (reaction.target) {
field.query(reaction.target).forEach((field) => {
setSchemaFieldState(field, reaction.fullfill, compile)
Expand Down
10 changes: 5 additions & 5 deletions packages/json-schema/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type SchemaPatch = (schema: ISchema) => ISchema

export type SchemaKey = string | number

export type SchemaExtendReaction<Field = any> =
export type SchemaReaction<Field = any> =
| {
dependencies?: string[]
when?: string | boolean
Expand All @@ -61,9 +61,9 @@ export type SchemaExtendReaction<Field = any> =
}
| ((field: Field) => void)

export type SchemaExtendReactions<Field = any> =
| SchemaExtendReaction<Field>
| SchemaExtendReaction<Field>[]
export type SchemaReactions<Field = any> =
| SchemaReaction<Field>
| SchemaReaction<Field>[]

export type SchemaItems<
Decorator,
Expand Down Expand Up @@ -220,7 +220,7 @@ export interface ISchema<
//组件属性
['x-component-props']?: ComponentProps
//组件响应器
['x-reactions']?: SchemaExtendReactions<ReactionField>
['x-reactions']?: SchemaReactions<ReactionField>
//内容
['x-content']?: any

Expand Down
2 changes: 1 addition & 1 deletion packages/react/docs/api/components/SchemaField.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type ComposeSchemaField = React.FC<ISchemaFieldProps> & {
//工厂函数参数属性
interface ISchemaFieldFactoryProps {
components?: {
[key: string]: React.JSXElementConstructor<any> //组件列表
[key: string]: React.FC //组件列表
}
scope?: any //全局作用域,用于实现协议表达式变量注入
}
Expand Down
33 changes: 4 additions & 29 deletions packages/react/docs/api/hooks/useField.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

主要用在自定义组件内读取当前字段属性,操作字段状态等,在所有 Field 组件的子树内都能使用,注意,拿到的是[GeneralField](https://core.formilyjs.org/api/models/field#generalfield),如果需要对不同类型的字段做处理,请使用[Type Checker](https://core.formilyjs.org/api/entry/form-checker)

<Alert>
注意:如果要在自定义组件内使用useField,并响应字段模型变化,那需要给自定义组件包装observer
</Alert>

## 签名

```ts
Expand Down Expand Up @@ -45,19 +49,6 @@ const FormItem = observer(({ children }) => {

export default () => {
const form = useMemo(() => createForm({ validateFirst: true }))

const createPasswordEqualValidate = (equalName) => (field) => {
if (
form.values.confirm_password &&
field.value &&
form.values[equalName] !== field.value
) {
field.errors = ['Password does not match Confirm Password.']
} else {
field.errors = []
}
}

return (
<FormProvider form={form}>
<Form layout="vertical">
Expand All @@ -68,22 +59,6 @@ export default () => {
decorator={[FormItem]}
component={[Input, { placeholder: 'Please Input' }]}
/>
<Field
name="password"
title="Password"
required
decorator={[FormItem]}
component={[Input, { type: 'password', placeholder: 'Please Input' }]}
reactions={createPasswordEqualValidate('confirm_password')}
/>
<Field
name="confirm_password"
title="Confirm Password"
required
decorator={[FormItem]}
component={[Input, { type: 'password', placeholder: 'Please Input' }]}
reactions={createPasswordEqualValidate('password')}
/>
<code>
<pre>
<FormConsumer>
Expand Down
Loading

0 comments on commit 76b799a

Please sign in to comment.