@formvk/react 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +60 -0
- package/dist/index.js +91 -295
- package/dist/index.js.map +1 -1
- package/package.json +10 -6
- package/src/__tests__/expression.spec.tsx +0 -87
- package/src/__tests__/form.spec.tsx +0 -164
- package/src/__tests__/hooks.spec.tsx +0 -198
- package/src/__tests__/render.spec.tsx +0 -704
- package/src/__tests__/schema.spec.tsx +0 -598
- package/src/__tests__/shared.tsx +0 -44
- package/src/components/ExpressionScope.tsx +0 -16
- package/src/components/FormConsumer.tsx +0 -20
- package/src/components/FormProvider.tsx +0 -35
- package/src/components/ReactiveField.tsx +0 -161
- package/src/components/RecursionField.tsx +0 -113
- package/src/components/SchemaField.tsx +0 -195
- package/src/components/index.ts +0 -7
- package/src/hooks/index.ts +0 -6
- package/src/hooks/useAttach.ts +0 -20
- package/src/hooks/useField.ts +0 -8
- package/src/hooks/useFieldSchema.ts +0 -8
- package/src/hooks/useForm.ts +0 -8
- package/src/hooks/useFormEffects.ts +0 -19
- package/src/hooks/useParentForm.ts +0 -18
- package/src/index.ts +0 -5
- package/src/shared/connect.tsx +0 -72
- package/src/shared/context.ts +0 -15
- package/src/shared/createForm.ts +0 -10
- package/src/shared/index.ts +0 -2
- package/src/types/index.ts +0 -144
- package/src/utils/getRawComponent.ts +0 -12
- package/src/utils/slots.ts +0 -9
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -7
- package/vite.config.ts +0 -32
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { PropsWithChildren } from 'react'
|
|
2
|
-
|
|
3
|
-
import { useAttach } from '../hooks/useAttach'
|
|
4
|
-
import {
|
|
5
|
-
FieldContext,
|
|
6
|
-
FormContext,
|
|
7
|
-
SchemaContext,
|
|
8
|
-
SchemaExpressionScopeContext,
|
|
9
|
-
SchemaOptionsContext,
|
|
10
|
-
} from '../shared/context'
|
|
11
|
-
import type { IProviderProps } from '../types'
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 挂载表单生命周期并注入 FormContext。
|
|
15
|
-
* 同时把字段 / schema / scope / options 四个上下文重置为空,
|
|
16
|
-
* 等价于 Vue 侧 useInjectionCleaner 的隔离效果,嵌套 FormProvider 不会串层。
|
|
17
|
-
*/
|
|
18
|
-
export function FormProvider(props: PropsWithChildren<IProviderProps>) {
|
|
19
|
-
const { form, children } = props
|
|
20
|
-
useAttach(form)
|
|
21
|
-
|
|
22
|
-
return (
|
|
23
|
-
<FormContext.Provider value={form}>
|
|
24
|
-
<FieldContext.Provider value={undefined}>
|
|
25
|
-
<SchemaContext.Provider value={undefined}>
|
|
26
|
-
<SchemaExpressionScopeContext.Provider value={undefined}>
|
|
27
|
-
<SchemaOptionsContext.Provider value={undefined}>{children}</SchemaOptionsContext.Provider>
|
|
28
|
-
</SchemaExpressionScopeContext.Provider>
|
|
29
|
-
</SchemaContext.Provider>
|
|
30
|
-
</FieldContext.Provider>
|
|
31
|
-
</FormContext.Provider>
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
FormProvider.displayName = 'FormProvider'
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { isVoidField } from '@formvk/core'
|
|
2
|
-
import type { GeneralField, IFieldFactoryProps } from '@formvk/core'
|
|
3
|
-
import { toJS } from '@formvk/reactive'
|
|
4
|
-
import { observer } from '@formvk/reactive-react'
|
|
5
|
-
import { each, isFn, isStr } from '@formvk/shared'
|
|
6
|
-
import { createElement, isValidElement, useContext, useMemo } from 'react'
|
|
7
|
-
import type { ReactNode } from 'react'
|
|
8
|
-
|
|
9
|
-
import { useAttach } from '../hooks/useAttach'
|
|
10
|
-
import { useField } from '../hooks/useField'
|
|
11
|
-
import { useForm } from '../hooks/useForm'
|
|
12
|
-
import { FieldContext, SchemaOptionsContext } from '../shared/context'
|
|
13
|
-
import type { IReactiveFieldProps, ISchemaFieldRuntimeOptions } from '../types'
|
|
14
|
-
import { getRawComponent } from '../utils/getRawComponent'
|
|
15
|
-
import { toArray } from '../utils/slots'
|
|
16
|
-
import type { SlotMap } from '../utils/slots'
|
|
17
|
-
|
|
18
|
-
function isReactComponentLike(value: any): boolean {
|
|
19
|
-
if (!value) return false
|
|
20
|
-
if (isFn(value)) return true
|
|
21
|
-
return typeof value === 'object' && ('$$typeof' in value || isFn(value.render))
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function renderContent(content: any): ReactNode {
|
|
25
|
-
if (isValidElement(content) || isStr(content)) return content
|
|
26
|
-
if (isReactComponentLike(content)) return createElement(content)
|
|
27
|
-
return content
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** 合并 RecursionField 生成的具名插槽与 schema 上的 content 声明。 */
|
|
31
|
-
function mergeSlots(slots: SlotMap, content: any): SlotMap {
|
|
32
|
-
const result: SlotMap = { ...slots }
|
|
33
|
-
|
|
34
|
-
if (!content) return result
|
|
35
|
-
|
|
36
|
-
function appendToSlot(name: string, value: any) {
|
|
37
|
-
const rendered = renderContent(value)
|
|
38
|
-
const origin = result[name]
|
|
39
|
-
result[name] = origin === undefined ? rendered : ([] as ReactNode[]).concat(toArray(origin), toArray(rendered))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (isStr(content) || isValidElement(content) || isReactComponentLike(content)) {
|
|
43
|
-
appendToSlot('default', content)
|
|
44
|
-
} else if (typeof content === 'object') {
|
|
45
|
-
// 普通对象视为具名插槽内容映射
|
|
46
|
-
each(content, (value, key) => {
|
|
47
|
-
appendToSlot(key, value)
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return result
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function normalizeProps(origin: Record<string, any>) {
|
|
55
|
-
const props: Record<string, any> = {}
|
|
56
|
-
const events: Record<string, any> = {}
|
|
57
|
-
each(origin, (value, key) => {
|
|
58
|
-
if (key.startsWith('on')) {
|
|
59
|
-
const eventName = `${key[2].toLowerCase()}${key.slice(3)}`
|
|
60
|
-
events[eventName] = events[eventName] ?? value
|
|
61
|
-
} else if (key.startsWith('@')) {
|
|
62
|
-
events[key.slice(1)] = value
|
|
63
|
-
} else {
|
|
64
|
-
props[key] = value
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
return { props, events }
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function toReactEventProps(events: Record<string, any>) {
|
|
71
|
-
const props: Record<string, any> = {}
|
|
72
|
-
each(events, (handler, name) => {
|
|
73
|
-
props[`on${name.charAt(0).toUpperCase()}${name.slice(1)}`] = handler
|
|
74
|
-
})
|
|
75
|
-
return props
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export const ReactiveField = observer((props: IReactiveFieldProps) => {
|
|
79
|
-
const form = useForm()
|
|
80
|
-
const parent = useField()
|
|
81
|
-
const options = useContext(SchemaOptionsContext) as ISchemaFieldRuntimeOptions | undefined
|
|
82
|
-
|
|
83
|
-
const { fieldType = 'Field', fieldProps, slots: incomingSlots, children } = props
|
|
84
|
-
|
|
85
|
-
// 与 Vue 侧 watch(fieldProps) 重建对齐:fieldProps 引用变化即创建新字段,旧实例由 useAttach 卸载
|
|
86
|
-
const field = useMemo(() => {
|
|
87
|
-
const factoryProps = (fieldProps ?? {}) as IFieldFactoryProps<any, any>
|
|
88
|
-
return (form as any)?.[`create${fieldType}`]?.({
|
|
89
|
-
...factoryProps,
|
|
90
|
-
basePath: factoryProps.basePath ?? parent?.address,
|
|
91
|
-
}) as GeneralField | undefined
|
|
92
|
-
}, [form, fieldType, fieldProps, parent])
|
|
93
|
-
|
|
94
|
-
useAttach(field)
|
|
95
|
-
|
|
96
|
-
let content: ReactNode = null
|
|
97
|
-
|
|
98
|
-
if (!field) {
|
|
99
|
-
content = children ?? null
|
|
100
|
-
} else if (field.display !== 'visible') {
|
|
101
|
-
content = null
|
|
102
|
-
} else {
|
|
103
|
-
const mergedSlots = mergeSlots(incomingSlots ?? {}, field.content)
|
|
104
|
-
|
|
105
|
-
function renderDecorator(inner: ReactNode) {
|
|
106
|
-
if (!field!.decoratorType) return inner
|
|
107
|
-
const Decorator = getRawComponent(options?.components, field!.decoratorType)
|
|
108
|
-
const { props: decoratorProps, events } = normalizeProps(toJS((field!.decorator as any)[1]) || {})
|
|
109
|
-
return createElement(Decorator, { ...decoratorProps, ...toReactEventProps(events) }, inner)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function renderComponent(): ReactNode {
|
|
113
|
-
if (!field!.componentType) return mergedSlots.default ?? null
|
|
114
|
-
|
|
115
|
-
const Component = getRawComponent(options?.components, field!.componentType)
|
|
116
|
-
const { props: originProps, events } = normalizeProps(toJS((field!.component as any)[1]) || {})
|
|
117
|
-
const originChange = events.change
|
|
118
|
-
const originFocus = events.focus
|
|
119
|
-
const originBlur = events.blur
|
|
120
|
-
|
|
121
|
-
events.change = (...args: any[]) => {
|
|
122
|
-
if (!isVoidField(field!)) field!.onInput(...args)
|
|
123
|
-
originChange?.(...args)
|
|
124
|
-
}
|
|
125
|
-
events.focus = (...args: any[]) => {
|
|
126
|
-
if (!isVoidField(field!)) field!.onFocus(...args)
|
|
127
|
-
originFocus?.(...args)
|
|
128
|
-
}
|
|
129
|
-
events.blur = (...args: any[]) => {
|
|
130
|
-
if (!isVoidField(field!)) field!.onBlur(...args)
|
|
131
|
-
originBlur?.(...args)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const componentProps: Record<string, any> = {
|
|
135
|
-
...(isVoidField(field!)
|
|
136
|
-
? {}
|
|
137
|
-
: {
|
|
138
|
-
disabled: field!.pattern === 'disabled' || field!.pattern === 'readPretty',
|
|
139
|
-
readOnly: field!.pattern === 'readOnly',
|
|
140
|
-
}),
|
|
141
|
-
...originProps,
|
|
142
|
-
...(isVoidField(field!) ? {} : { value: field!.value }),
|
|
143
|
-
...toReactEventProps(events),
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (isStr(Component)) {
|
|
147
|
-
// 原生标签不接收 slots 对象,所有具名插槽拍平成 children
|
|
148
|
-
const slotChildren = Object.keys(mergedSlots).flatMap(name => toArray(mergedSlots[name]))
|
|
149
|
-
return createElement(Component, componentProps, ...slotChildren)
|
|
150
|
-
}
|
|
151
|
-
// React 组件通过 props.slots 读取具名插槽,default 同时作为 children 透传
|
|
152
|
-
return createElement(Component, { ...componentProps, slots: mergedSlots }, mergedSlots.default)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
content = renderDecorator(renderComponent())
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return <FieldContext.Provider value={field}>{content}</FieldContext.Provider>
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
ReactiveField.displayName = 'ReactiveField'
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Schema } from '@formvk/schema'
|
|
2
|
-
import { isFn, isValid, lazyMerge } from '@formvk/shared'
|
|
3
|
-
import { createElement, useContext, useMemo } from 'react'
|
|
4
|
-
import type { ReactNode } from 'react'
|
|
5
|
-
|
|
6
|
-
import { useField } from '../hooks/useField'
|
|
7
|
-
import { SchemaContext, SchemaExpressionScopeContext, SchemaOptionsContext } from '../shared/context'
|
|
8
|
-
import type { IRecursionFieldProps, ISchemaFieldRuntimeOptions, ISchemaFilter, ISchemaMapper } from '../types'
|
|
9
|
-
import { toArray } from '../utils/slots'
|
|
10
|
-
import type { SlotMap } from '../utils/slots'
|
|
11
|
-
import { ReactiveField } from './ReactiveField'
|
|
12
|
-
|
|
13
|
-
function resolveEmptySlot(slots: SlotMap) {
|
|
14
|
-
const names = Object.keys(slots)
|
|
15
|
-
if (!names.length) return null
|
|
16
|
-
const children = names.flatMap(name => toArray(slots[name]))
|
|
17
|
-
// 无名根节点不创建字段,用 display: contents 的包裹层保持 DOM 结构与 Vue 侧一致
|
|
18
|
-
return createElement('div', { style: { display: 'contents' } }, ...children)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function RecursionField(props: IRecursionFieldProps) {
|
|
22
|
-
const parent = useField()
|
|
23
|
-
const options = (useContext(SchemaOptionsContext) ?? {}) as ISchemaFieldRuntimeOptions
|
|
24
|
-
const scope = useContext(SchemaExpressionScopeContext)
|
|
25
|
-
|
|
26
|
-
// React 无响应式代理包裹,Schema 实例直接缓存即可(对应 Vue 侧去掉 markRaw)
|
|
27
|
-
const fieldSchema = useMemo(
|
|
28
|
-
() => (Schema.isSchemaInstance(props.schema) ? props.schema : new Schema(props.schema as any)),
|
|
29
|
-
[props.schema]
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
const fieldProps = useMemo(
|
|
33
|
-
() =>
|
|
34
|
-
fieldSchema.toFieldProps({
|
|
35
|
-
...options,
|
|
36
|
-
get scope() {
|
|
37
|
-
return lazyMerge(options.scope as any, scope ?? {})
|
|
38
|
-
},
|
|
39
|
-
}),
|
|
40
|
-
[fieldSchema, options, scope]
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
function getBasePath() {
|
|
44
|
-
if (props.onlyRenderProperties) {
|
|
45
|
-
return (
|
|
46
|
-
props.basePath ?? (isValid(props.name) ? (parent?.address ?? '').concat(props.name as any) : parent?.address)
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
return props.basePath ?? parent?.address
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const basePath = getBasePath()
|
|
53
|
-
|
|
54
|
-
function generateSlots(): SlotMap {
|
|
55
|
-
if (props.onlyRenderSelf) return {}
|
|
56
|
-
const properties = Schema.getOrderProperties(fieldSchema)
|
|
57
|
-
if (!properties.length) return {}
|
|
58
|
-
|
|
59
|
-
const renderMap: Record<string, ReactNode[]> = {}
|
|
60
|
-
const mapProperties = props.mapProperties as ISchemaMapper | undefined
|
|
61
|
-
const filterProperties = props.filterProperties as ISchemaFilter | undefined
|
|
62
|
-
|
|
63
|
-
properties.forEach(({ schema: item, key: name }, index) => {
|
|
64
|
-
let itemSchema = item
|
|
65
|
-
if (isFn(mapProperties)) {
|
|
66
|
-
const mapped = mapProperties(item, name)
|
|
67
|
-
if (mapped) itemSchema = mapped as Schema
|
|
68
|
-
}
|
|
69
|
-
if (isFn(filterProperties)) {
|
|
70
|
-
if (filterProperties(itemSchema, name) === false) return
|
|
71
|
-
}
|
|
72
|
-
const slotName = itemSchema.slot ?? 'default'
|
|
73
|
-
const node = (
|
|
74
|
-
<RecursionField key={`${index}-${String(name)}`} schema={itemSchema} name={name} basePath={basePath} />
|
|
75
|
-
)
|
|
76
|
-
;(renderMap[slotName] ??= []).push(node)
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
return renderMap
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const withSchema = (content: ReactNode) => (
|
|
83
|
-
<SchemaContext.Provider value={fieldSchema}>{content}</SchemaContext.Provider>
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
if (!isValid(props.name)) {
|
|
87
|
-
return withSchema(resolveEmptySlot(generateSlots()))
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const commonFieldProps = {
|
|
91
|
-
...fieldProps,
|
|
92
|
-
name: props.name,
|
|
93
|
-
basePath,
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (fieldSchema.type === 'object') {
|
|
97
|
-
if (props.onlyRenderProperties) return withSchema(resolveEmptySlot(generateSlots()))
|
|
98
|
-
return withSchema(<ReactiveField fieldType="ObjectField" fieldProps={commonFieldProps} slots={generateSlots()} />)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (fieldSchema.type === 'array') {
|
|
102
|
-
return withSchema(<ReactiveField fieldType="ArrayField" fieldProps={commonFieldProps} />)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (fieldSchema.type === 'void') {
|
|
106
|
-
if (props.onlyRenderProperties) return withSchema(resolveEmptySlot(generateSlots()))
|
|
107
|
-
return withSchema(<ReactiveField fieldType="VoidField" fieldProps={commonFieldProps} slots={generateSlots()} />)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return withSchema(<ReactiveField fieldType="Field" fieldProps={commonFieldProps} />)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
RecursionField.displayName = 'RecursionField'
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import type { Form } from '@formvk/core'
|
|
2
|
-
import { createFragment, Schema } from '@formvk/schema'
|
|
3
|
-
import type { AnyComponent, ISchema, SchemaKey } from '@formvk/schema'
|
|
4
|
-
import { FormPath, isValid } from '@formvk/shared'
|
|
5
|
-
import { useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
6
|
-
|
|
7
|
-
import { useField } from '../hooks/useField'
|
|
8
|
-
import { useForm } from '../hooks/useForm'
|
|
9
|
-
import { SchemaExpressionScopeContext, SchemaOptionsContext } from '../shared/context'
|
|
10
|
-
import type {
|
|
11
|
-
InferArrayProps,
|
|
12
|
-
InferFieldProps,
|
|
13
|
-
InferVoidProps,
|
|
14
|
-
ISchemaFieldFactoryOptions,
|
|
15
|
-
ISchemaFieldProps,
|
|
16
|
-
ISchemaFieldRuntimeOptions,
|
|
17
|
-
SchemaFieldFactoryResult,
|
|
18
|
-
SchemaReactComponents,
|
|
19
|
-
} from '../types'
|
|
20
|
-
import { RecursionField } from './RecursionField'
|
|
21
|
-
|
|
22
|
-
interface ISchemaFieldInnerProps extends ISchemaFieldProps {
|
|
23
|
-
/** createSchemaField 预绑定的 components 与 scope,直接使用 SchemaField 时不存在 */
|
|
24
|
-
prebound?: ISchemaFieldRuntimeOptions
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function joinPath(prefix: string, segment: SchemaKey | '*') {
|
|
28
|
-
return prefix ? `${prefix}.${segment}` : String(segment)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface ISchemaPaths {
|
|
32
|
-
/** 字段的绝对路径,数组元素下标使用 * 通配 */
|
|
33
|
-
fields: string[]
|
|
34
|
-
/** 数组子树的清理模式,覆盖任意下标 */
|
|
35
|
-
arrays: string[]
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 收集 schema 声明的字段路径,用于热更新时定位需要销毁的字段。
|
|
40
|
-
* 数组元素的下标是运行时产物,这里统一用 * 通配。
|
|
41
|
-
*/
|
|
42
|
-
function collectSchemaPaths(schema: Schema, basePath: string, name?: SchemaKey): ISchemaPaths {
|
|
43
|
-
const fields: string[] = []
|
|
44
|
-
const arrays: string[] = []
|
|
45
|
-
|
|
46
|
-
const walkNode = (node: Schema, path: string) => {
|
|
47
|
-
if (node.type === 'array') arrays.push(joinPath(path, '*'))
|
|
48
|
-
walkChildren(node, path)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const walkChildren = (node: Schema, prefix: string) => {
|
|
52
|
-
const properties = node.properties ?? {}
|
|
53
|
-
Object.keys(properties).forEach(key => {
|
|
54
|
-
const path = joinPath(prefix, key)
|
|
55
|
-
fields.push(path)
|
|
56
|
-
walkNode(properties[key], path)
|
|
57
|
-
})
|
|
58
|
-
const items = node.items
|
|
59
|
-
if (items) {
|
|
60
|
-
const itemSchemas = Array.isArray(items) ? items : [items]
|
|
61
|
-
itemSchemas.forEach(item => {
|
|
62
|
-
if (item) walkChildren(item, joinPath(prefix, '*'))
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (isValid(name)) {
|
|
68
|
-
const rootPath = joinPath(basePath, String(name))
|
|
69
|
-
fields.push(rootPath)
|
|
70
|
-
walkNode(schema, rootPath)
|
|
71
|
-
} else {
|
|
72
|
-
walkChildren(schema, basePath)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return { fields, arrays }
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function toSchema(schema: ISchema | Schema): Schema {
|
|
79
|
-
return Schema.isSchemaInstance(schema) ? schema : new Schema({ type: 'object', ...schema })
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 热更新:销毁旧 schema 声明的字段(保留值),字段会在本次渲染的 fieldProps 更新后重建。
|
|
84
|
-
*/
|
|
85
|
-
function applyHotReload(form: Form, prev: Schema, next: Schema, basePath: string, name?: SchemaKey) {
|
|
86
|
-
const prevPaths = collectSchemaPaths(prev, basePath, name)
|
|
87
|
-
const nextPaths = collectSchemaPaths(next, basePath, name)
|
|
88
|
-
const nextFields = new Set(nextPaths.fields)
|
|
89
|
-
|
|
90
|
-
prevPaths.fields.forEach(path => {
|
|
91
|
-
if (nextFields.has(path)) return
|
|
92
|
-
form.deleteValuesIn(path)
|
|
93
|
-
form.deleteInitialValuesIn(path)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
const patterns = new Set<string>([...prevPaths.fields, ...nextPaths.fields, ...prevPaths.arrays, ...nextPaths.arrays])
|
|
97
|
-
patterns.forEach(pattern => {
|
|
98
|
-
form.clearFormGraph(pattern, false)
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function SchemaField(props: ISchemaFieldInnerProps) {
|
|
103
|
-
const form = useForm()
|
|
104
|
-
const parent = useField()
|
|
105
|
-
|
|
106
|
-
const fieldSchema = useMemo(() => toSchema(props.schema), [props.schema])
|
|
107
|
-
|
|
108
|
-
const options = useMemo<ISchemaFieldRuntimeOptions>(
|
|
109
|
-
() => ({
|
|
110
|
-
components: { ...props.prebound?.components, ...props.components },
|
|
111
|
-
scope: { ...props.prebound?.scope, ...props.scope },
|
|
112
|
-
}),
|
|
113
|
-
[props.prebound, props.components, props.scope]
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
// 对齐 Vue watch(schema, { flush: 'pre' }):外部 store 的销毁不能放在渲染期
|
|
117
|
-
// (destroy 会调度其他 observer 组件更新,触发 React 跨组件 setState 告警)。
|
|
118
|
-
// 改为 layout effect 中清理旧字段,再通过 key 让 RecursionField 子树整体重挂载;
|
|
119
|
-
// layout effect 在浏览器绘制前同步完成重渲染,无中间态闪烁,ref 守卫保证 StrictMode 双调用幂等。
|
|
120
|
-
const [reloadVersion, setReloadVersion] = useState(0)
|
|
121
|
-
const prevSchemaRef = useRef<Schema>(fieldSchema)
|
|
122
|
-
const basePath = props.basePath ? FormPath.parse(props.basePath).toString() : (parent?.address?.toString() ?? '')
|
|
123
|
-
|
|
124
|
-
useLayoutEffect(() => {
|
|
125
|
-
if (!props.hotReload || !form || prevSchemaRef.current === fieldSchema) return
|
|
126
|
-
const prev = prevSchemaRef.current
|
|
127
|
-
prevSchemaRef.current = fieldSchema
|
|
128
|
-
applyHotReload(form, prev, fieldSchema, basePath, props.name)
|
|
129
|
-
setReloadVersion(version => version + 1)
|
|
130
|
-
}, [props.hotReload, form, fieldSchema, basePath, props.name])
|
|
131
|
-
|
|
132
|
-
return (
|
|
133
|
-
<SchemaOptionsContext.Provider value={options}>
|
|
134
|
-
<SchemaExpressionScopeContext.Provider value={options.scope}>
|
|
135
|
-
<RecursionField
|
|
136
|
-
key={reloadVersion}
|
|
137
|
-
schema={fieldSchema}
|
|
138
|
-
name={props.name}
|
|
139
|
-
basePath={props.basePath}
|
|
140
|
-
onlyRenderProperties={props.onlyRenderProperties}
|
|
141
|
-
onlyRenderSelf={props.onlyRenderSelf}
|
|
142
|
-
mapProperties={props.mapProperties}
|
|
143
|
-
filterProperties={props.filterProperties}
|
|
144
|
-
/>
|
|
145
|
-
</SchemaExpressionScopeContext.Provider>
|
|
146
|
-
</SchemaOptionsContext.Provider>
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
SchemaField.displayName = 'SchemaField'
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* 基于扁平组件映射与分类组件 map 创建 SchemaField + fragment:
|
|
154
|
-
* - voidComponents / arrayComponents 只接收组件 map,分类名由工厂内部提取后传给 fragment
|
|
155
|
-
* - 分类组件会自动合并进运行时组件映射,无需在 components 中重复注册
|
|
156
|
-
* - 返回的 SchemaField 已预绑定拍平后的组件与 scope,props 仍可传扁平 map 覆盖同名组件
|
|
157
|
-
* - fragment 包含 Field / Object / Array / Void / transform 与按组件名生成的动态构造器
|
|
158
|
-
* 组件 props、字段值与 scope 类型全部自动推断,无需手写类型参数。
|
|
159
|
-
*/
|
|
160
|
-
export function createSchemaField<
|
|
161
|
-
F extends SchemaReactComponents = SchemaReactComponents,
|
|
162
|
-
V extends SchemaReactComponents = {},
|
|
163
|
-
A extends SchemaReactComponents = {},
|
|
164
|
-
Scope extends object = Record<string, any>,
|
|
165
|
-
Decorator extends AnyComponent<InferVoidProps<V, A>> = AnyComponent<InferVoidProps<V, A>>,
|
|
166
|
-
>(
|
|
167
|
-
factoryOptions: ISchemaFieldFactoryOptions<F, V, A, Scope, Decorator> = {}
|
|
168
|
-
): SchemaFieldFactoryResult<F, V, A, Scope, Decorator> {
|
|
169
|
-
const voidComponents = factoryOptions.voidComponents ?? {}
|
|
170
|
-
const arrayComponents = factoryOptions.arrayComponents ?? {}
|
|
171
|
-
const flatComponents: SchemaReactComponents = {
|
|
172
|
-
...factoryOptions.components,
|
|
173
|
-
...voidComponents,
|
|
174
|
-
...arrayComponents,
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const fragment = createFragment<InferFieldProps<F, V, A>, InferVoidProps<V, A>, InferArrayProps<A>, Scope, Decorator>(
|
|
178
|
-
{
|
|
179
|
-
patches: factoryOptions.patches,
|
|
180
|
-
voidComponentNames: Object.keys(voidComponents),
|
|
181
|
-
arrayComponentNames: Object.keys(arrayComponents),
|
|
182
|
-
defaultDecorator: factoryOptions.defaultDecorator,
|
|
183
|
-
}
|
|
184
|
-
)
|
|
185
|
-
|
|
186
|
-
const prebound: ISchemaFieldRuntimeOptions = {
|
|
187
|
-
components: flatComponents,
|
|
188
|
-
scope: factoryOptions.scope,
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const BoundSchemaField = (props: ISchemaFieldProps) => <SchemaField {...props} prebound={prebound} />
|
|
192
|
-
BoundSchemaField.displayName = 'SchemaField'
|
|
193
|
-
|
|
194
|
-
return { SchemaField: BoundSchemaField as typeof SchemaField, fragment }
|
|
195
|
-
}
|
package/src/components/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { ExpressionScope } from './ExpressionScope'
|
|
2
|
-
export { FormConsumer } from './FormConsumer'
|
|
3
|
-
export type { IFormConsumerProps } from './FormConsumer'
|
|
4
|
-
export { FormProvider } from './FormProvider'
|
|
5
|
-
export { ReactiveField } from './ReactiveField'
|
|
6
|
-
export { RecursionField } from './RecursionField'
|
|
7
|
-
export { createSchemaField, SchemaField } from './SchemaField'
|
package/src/hooks/index.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { useAttach } from './useAttach'
|
|
2
|
-
export { useField } from './useField'
|
|
3
|
-
export { useFieldSchema } from './useFieldSchema'
|
|
4
|
-
export { useForm } from './useForm'
|
|
5
|
-
export { useFormEffects } from './useFormEffects'
|
|
6
|
-
export { useParentForm } from './useParentForm'
|
package/src/hooks/useAttach.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'react'
|
|
2
|
-
|
|
3
|
-
interface IRecycleTarget {
|
|
4
|
-
onMount: () => void
|
|
5
|
-
onUnmount: () => void
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 把字段生命周期挂到 React 组件生命周期上:
|
|
10
|
-
* target 变化时旧实例 onUnmount、新实例 onMount,组件卸载时清理当前实例。
|
|
11
|
-
*/
|
|
12
|
-
export function useAttach<T extends IRecycleTarget>(target: T | undefined): T | undefined {
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
target?.onMount()
|
|
15
|
-
return () => {
|
|
16
|
-
target?.onUnmount()
|
|
17
|
-
}
|
|
18
|
-
}, [target])
|
|
19
|
-
return target
|
|
20
|
-
}
|
package/src/hooks/useField.ts
DELETED
package/src/hooks/useForm.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { Form } from '@formvk/core'
|
|
2
|
-
import { uid } from '@formvk/shared'
|
|
3
|
-
import { useEffect } from 'react'
|
|
4
|
-
|
|
5
|
-
import { useForm } from './useForm'
|
|
6
|
-
|
|
7
|
-
export function useFormEffects(effects?: (form: Form) => void): void {
|
|
8
|
-
const form = useForm()
|
|
9
|
-
|
|
10
|
-
useEffect(() => {
|
|
11
|
-
if (!form || !effects) return
|
|
12
|
-
const id = uid()
|
|
13
|
-
form.addEffects(id, effects)
|
|
14
|
-
return () => {
|
|
15
|
-
// 捕获注册时的 form,避免 form 替换后清理到了新实例上
|
|
16
|
-
form.removeEffects(id)
|
|
17
|
-
}
|
|
18
|
-
}, [form, effects])
|
|
19
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { isObjectField } from '@formvk/core'
|
|
2
|
-
import type { Form, GeneralField, ObjectField } from '@formvk/core'
|
|
3
|
-
|
|
4
|
-
import { useField } from './useField'
|
|
5
|
-
import { useForm } from './useForm'
|
|
6
|
-
|
|
7
|
-
export function useParentForm(): Form | ObjectField | undefined {
|
|
8
|
-
const field = useField()
|
|
9
|
-
const form = useForm()
|
|
10
|
-
|
|
11
|
-
function findObjectParent(target?: GeneralField): Form | ObjectField | undefined {
|
|
12
|
-
if (!target) return form
|
|
13
|
-
if (isObjectField(target)) return target
|
|
14
|
-
return findObjectParent(target.parent)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return findObjectParent(field)
|
|
18
|
-
}
|