@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
package/src/shared/connect.tsx
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { isVoidField } from '@formvk/core'
|
|
2
|
-
import type { GeneralField } from '@formvk/core'
|
|
3
|
-
import { observer } from '@formvk/reactive-react'
|
|
4
|
-
import { each, FormPath, isFn, isStr, isValid } from '@formvk/shared'
|
|
5
|
-
import hoistNonReactStatics from 'hoist-non-react-statics'
|
|
6
|
-
import { createElement } from 'react'
|
|
7
|
-
|
|
8
|
-
import { useField } from '../hooks/useField'
|
|
9
|
-
import type { IComponentMapper, IStateMapper, ReactComponent } from '../types'
|
|
10
|
-
|
|
11
|
-
function getComponentName(component: ReactComponent): string | undefined {
|
|
12
|
-
return component.displayName ?? component.name
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function mapProps<T extends ReactComponent = ReactComponent>(...args: IStateMapper[]) {
|
|
16
|
-
function transform(input: Record<string, any>, field: GeneralField) {
|
|
17
|
-
return args.reduce((props, mapper) => {
|
|
18
|
-
if (isFn(mapper)) {
|
|
19
|
-
props = Object.assign(props, mapper(props, field))
|
|
20
|
-
} else {
|
|
21
|
-
each(mapper, (to, extract) => {
|
|
22
|
-
const extractValue = FormPath.getIn(field, extract)
|
|
23
|
-
const targetValue = isStr(to) ? to : extract
|
|
24
|
-
const originalValue = FormPath.getIn(props, targetValue)
|
|
25
|
-
if (extract === 'value') {
|
|
26
|
-
if (to !== extract) {
|
|
27
|
-
delete props.value
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (isValid(originalValue) && !isValid(extractValue)) return
|
|
31
|
-
FormPath.setIn(props, targetValue, extractValue)
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
return props
|
|
35
|
-
}, input)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return function (target: T): T {
|
|
39
|
-
const Connected: ReactComponent = observer(props => {
|
|
40
|
-
const field = useField()
|
|
41
|
-
// 无字段上下文时原样透传,支持 connect 组件脱离表单单独使用
|
|
42
|
-
const nextProps = field ? transform({ ...props }, field) : { ...props }
|
|
43
|
-
return createElement(target, nextProps)
|
|
44
|
-
})
|
|
45
|
-
const name = getComponentName(target)
|
|
46
|
-
Connected.displayName = name ? `Connected${name}` : 'ConnectedComponent'
|
|
47
|
-
hoistNonReactStatics(Connected, target)
|
|
48
|
-
return Connected as unknown as T
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function mapReadPretty(component: ReactComponent, readPrettyProps?: Record<string, any>) {
|
|
53
|
-
return function <T extends ReactComponent>(target: T): T {
|
|
54
|
-
const Read: ReactComponent = observer(props => {
|
|
55
|
-
const field = useField()
|
|
56
|
-
const Current = field && !isVoidField(field) && field.pattern === 'readPretty' ? component : target
|
|
57
|
-
return createElement(Current, { ...readPrettyProps, ...props })
|
|
58
|
-
})
|
|
59
|
-
const name = getComponentName(target)
|
|
60
|
-
Read.displayName = name ? `Read${name}` : 'ReadComponent'
|
|
61
|
-
hoistNonReactStatics(Read, target)
|
|
62
|
-
return Read as unknown as T
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function connect<T extends ReactComponent>(target: T, ...args: IComponentMapper[]): T {
|
|
67
|
-
const Component = args.reduce<ReactComponent>((current, mapper) => mapper(current), target)
|
|
68
|
-
const Connected: ReactComponent = props => createElement(Component, props)
|
|
69
|
-
Connected.displayName = getComponentName(target)
|
|
70
|
-
hoistNonReactStatics(Connected, target)
|
|
71
|
-
return Connected as unknown as T
|
|
72
|
-
}
|
package/src/shared/context.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Form, GeneralField } from '@formvk/core'
|
|
2
|
-
import type { Schema } from '@formvk/schema'
|
|
3
|
-
import { createContext } from 'react'
|
|
4
|
-
|
|
5
|
-
import type { ISchemaFieldRuntimeOptions } from '../types'
|
|
6
|
-
|
|
7
|
-
export const FormContext = createContext<Form | undefined>(undefined)
|
|
8
|
-
|
|
9
|
-
export const FieldContext = createContext<GeneralField | undefined>(undefined)
|
|
10
|
-
|
|
11
|
-
export const SchemaContext = createContext<Schema | undefined>(undefined)
|
|
12
|
-
|
|
13
|
-
export const SchemaExpressionScopeContext = createContext<Record<string, any> | undefined>(undefined)
|
|
14
|
-
|
|
15
|
-
export const SchemaOptionsContext = createContext<ISchemaFieldRuntimeOptions | undefined>(undefined)
|
package/src/shared/createForm.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { IFormProps } from '@formvk/core'
|
|
2
|
-
import { createForm as createCoreForm } from '@formvk/core'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* React 侧表单工厂:core 的 Form 自身管理响应式状态,
|
|
6
|
-
* React 不会自动代理普通对象,无需 Vue 侧的 markRaw 处理。
|
|
7
|
-
*/
|
|
8
|
-
export function createForm<T extends object = any>(options?: IFormProps<T>) {
|
|
9
|
-
return createCoreForm<T>(options)
|
|
10
|
-
}
|
package/src/shared/index.ts
DELETED
package/src/types/index.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import type { Form, GeneralField, IFieldFactoryProps } from '@formvk/core'
|
|
2
|
-
import type {
|
|
3
|
-
AnyComponent,
|
|
4
|
-
Fragment,
|
|
5
|
-
ISchema,
|
|
6
|
-
Schema,
|
|
7
|
-
SchemaKey,
|
|
8
|
-
SchemaPatchInput,
|
|
9
|
-
TPropsOfComponent,
|
|
10
|
-
} from '@formvk/schema'
|
|
11
|
-
import type { FormPathPattern } from '@formvk/shared'
|
|
12
|
-
import type { ComponentType, ComponentProps as ReactComponentProps } from 'react'
|
|
13
|
-
|
|
14
|
-
// 仅用于工厂返回值类型,type-only 引用不产生运行时循环依赖
|
|
15
|
-
import type { SchemaField } from '../components/SchemaField'
|
|
16
|
-
|
|
17
|
-
export type ReactComponent = ComponentType<any>
|
|
18
|
-
|
|
19
|
-
/** 提取 React 组件定义的 props 类型;非组件类型退化为 never。 */
|
|
20
|
-
export type ComponentProps<T> = T extends ReactComponent ? ReactComponentProps<T> : never
|
|
21
|
-
|
|
22
|
-
export type SchemaReactComponents = Record<string, ReactComponent>
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 从扁平 components 与两个分类组件 map 分别推断 fragment 的三个 props map,组件 props 自动提取。
|
|
26
|
-
* 分类优先级与运行时递归渲染一致:array > void > field,已分类的名字不会出现在 field 映射中,
|
|
27
|
-
* 避免同一组件名同时命中 field / void / array 多个动态构造器重载。
|
|
28
|
-
* 分类泛型默认空 map,keyof 结果为 never,省略分类时不会污染 field 推断。
|
|
29
|
-
*/
|
|
30
|
-
export type InferFieldProps<
|
|
31
|
-
F extends SchemaReactComponents,
|
|
32
|
-
V extends SchemaReactComponents,
|
|
33
|
-
A extends SchemaReactComponents,
|
|
34
|
-
> = {
|
|
35
|
-
[K in keyof F as K extends keyof V | keyof A ? never : K]: TPropsOfComponent<F[K]>
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type InferVoidProps<V extends SchemaReactComponents, A extends SchemaReactComponents> = {
|
|
39
|
-
[K in keyof V as K extends keyof A ? never : K]: TPropsOfComponent<V[K]>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type InferArrayProps<A extends SchemaReactComponents> = {
|
|
43
|
-
[K in keyof A]: TPropsOfComponent<A[K]>
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface IProviderProps {
|
|
47
|
-
form: Form
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface IFormConsumerRenderProps {
|
|
51
|
-
form: Form
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** 注入后代组件的运行时选项,components 始终是扁平 name → component。 */
|
|
55
|
-
export interface ISchemaFieldRuntimeOptions {
|
|
56
|
-
components?: SchemaReactComponents
|
|
57
|
-
scope?: Record<string, any>
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface ISchemaFieldFactoryOptions<
|
|
61
|
-
F extends SchemaReactComponents = SchemaReactComponents,
|
|
62
|
-
V extends SchemaReactComponents = {},
|
|
63
|
-
A extends SchemaReactComponents = {},
|
|
64
|
-
Scope extends object = Record<string, any>,
|
|
65
|
-
Decorator extends AnyComponent<InferVoidProps<V, A>> = AnyComponent<InferVoidProps<V, A>>,
|
|
66
|
-
> {
|
|
67
|
-
/** 扁平 name → component 映射,未声明分类的组件默认按 field 处理 */
|
|
68
|
-
components?: F
|
|
69
|
-
/** void / 布局分类组件 map,自动合并进运行时组件映射 */
|
|
70
|
-
voidComponents?: V
|
|
71
|
-
/** array 分类组件 map,自动合并进运行时组件映射 */
|
|
72
|
-
arrayComponents?: A
|
|
73
|
-
/** 表达式与 reactions 的自定义 scope,传入后类型自动推断,无需手写泛型 */
|
|
74
|
-
scope?: Scope
|
|
75
|
-
/** 默认装饰器,行为同 createFragment 的 defaultDecorator */
|
|
76
|
-
defaultDecorator?: Decorator
|
|
77
|
-
/** 应用到 fragment 产出的每个 schema 上的 patch */
|
|
78
|
-
patches?: SchemaPatchInput | SchemaPatchInput[]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* createSchemaField 的返回值:
|
|
83
|
-
* - SchemaField:预绑定 components 与 scope 的渲染组件
|
|
84
|
-
* - fragment:同配置的 fragment 构造器集合(Field / Object / Array / Void / transform 与动态构造器)
|
|
85
|
-
*/
|
|
86
|
-
export type SchemaFieldFactoryResult<
|
|
87
|
-
F extends SchemaReactComponents,
|
|
88
|
-
V extends SchemaReactComponents,
|
|
89
|
-
A extends SchemaReactComponents,
|
|
90
|
-
Scope extends object,
|
|
91
|
-
Decorator extends AnyComponent<InferVoidProps<V, A>>,
|
|
92
|
-
> = {
|
|
93
|
-
SchemaField: typeof SchemaField
|
|
94
|
-
fragment: Fragment<InferFieldProps<F, V, A>, InferVoidProps<V, A>, InferArrayProps<A>, Scope, Decorator>
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export interface ISchemaMapper {
|
|
98
|
-
(schema: Schema, name: SchemaKey): ISchema | Schema | void
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface ISchemaFilter {
|
|
102
|
-
(schema: Schema, name: SchemaKey): boolean
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface IRecursionFieldProps {
|
|
106
|
-
schema: ISchema | Schema
|
|
107
|
-
name?: SchemaKey
|
|
108
|
-
basePath?: FormPathPattern
|
|
109
|
-
onlyRenderProperties?: boolean
|
|
110
|
-
onlyRenderSelf?: boolean
|
|
111
|
-
mapProperties?: ISchemaMapper
|
|
112
|
-
filterProperties?: ISchemaFilter
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export interface ISchemaFieldProps extends IRecursionFieldProps {
|
|
116
|
-
/** 扁平 name → component 映射 */
|
|
117
|
-
components?: SchemaReactComponents
|
|
118
|
-
scope?: Record<string, any>
|
|
119
|
-
/**
|
|
120
|
-
* schema 引用变化时重建字段,默认关闭。
|
|
121
|
-
* 仅建议在开发环境开启,且 schema 需要保持稳定引用。
|
|
122
|
-
*/
|
|
123
|
-
hotReload?: boolean
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export type ReactiveFieldType = 'Field' | 'ObjectField' | 'ArrayField' | 'VoidField'
|
|
127
|
-
|
|
128
|
-
export interface IReactiveFieldProps {
|
|
129
|
-
fieldType?: ReactiveFieldType
|
|
130
|
-
fieldProps?: IFieldFactoryProps<any, any>
|
|
131
|
-
/** RecursionField 按 schema.slot 分组生成的具名插槽节点 */
|
|
132
|
-
slots?: Record<string, React.ReactNode>
|
|
133
|
-
children?: React.ReactNode
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export interface IExpressionScopeProps {
|
|
137
|
-
value: any
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export type IComponentMapper<T extends ReactComponent = ReactComponent> = (target: T) => ReactComponent
|
|
141
|
-
|
|
142
|
-
export type IStateMapper<Props = Record<string, any>> =
|
|
143
|
-
| Record<string, string | boolean>
|
|
144
|
-
| ((props: Props, field: GeneralField) => Props)
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { FormPath, isStr } from '@formvk/shared'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 解析 schema 中的组件描述:字符串按路径从 components 中查找,组件类型直接使用。
|
|
5
|
-
* React 侧不存在响应式代理包裹,无需 toRaw。
|
|
6
|
-
*/
|
|
7
|
-
export function getRawComponent(components: Record<string, any> | undefined, component: any): any {
|
|
8
|
-
if (isStr(component)) {
|
|
9
|
-
return FormPath.getIn(components, component) ?? component
|
|
10
|
-
}
|
|
11
|
-
return component
|
|
12
|
-
}
|
package/src/utils/slots.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ReactNode } from 'react'
|
|
2
|
-
|
|
3
|
-
/** 具名插槽映射,default 为默认插槽,对应 Vue 侧的 slots 对象。 */
|
|
4
|
-
export type SlotMap = Record<string, ReactNode>
|
|
5
|
-
|
|
6
|
-
export function toArray(value: ReactNode): ReactNode[] {
|
|
7
|
-
if (Array.isArray(value)) return value
|
|
8
|
-
return value === undefined || value === null ? [] : [value]
|
|
9
|
-
}
|
package/tsconfig.build.json
DELETED
package/tsconfig.json
DELETED
package/vite.config.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import react from '@vitejs/plugin-react'
|
|
2
|
-
import { defineConfig } from 'vite'
|
|
3
|
-
import dts from 'vite-plugin-dts'
|
|
4
|
-
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
plugins: [react(), dts({ tsconfigPath: './tsconfig.build.json', entryRoot: 'src', bundleTypes: true })],
|
|
7
|
-
test: {
|
|
8
|
-
environment: 'happy-dom',
|
|
9
|
-
},
|
|
10
|
-
build: {
|
|
11
|
-
lib: {
|
|
12
|
-
entry: 'src/index.ts',
|
|
13
|
-
name: 'Formvk.React',
|
|
14
|
-
fileName: 'index',
|
|
15
|
-
formats: ['es'],
|
|
16
|
-
},
|
|
17
|
-
sourcemap: true,
|
|
18
|
-
target: 'esnext',
|
|
19
|
-
rollupOptions: {
|
|
20
|
-
external: [
|
|
21
|
-
'react',
|
|
22
|
-
'react-dom',
|
|
23
|
-
'react/jsx-runtime',
|
|
24
|
-
'@formvk/core',
|
|
25
|
-
'@formvk/schema',
|
|
26
|
-
'@formvk/shared',
|
|
27
|
-
'@formvk/reactive',
|
|
28
|
-
'@formvk/reactive-react',
|
|
29
|
-
],
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
})
|