@formvk/vue 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.
@@ -1,6 +0,0 @@
1
- import { provide, ref } from 'vue'
2
- import type { InjectionKey, Ref } from 'vue'
3
-
4
- export function useInjectionCleaner(injectionKeys: InjectionKey<Ref<unknown>>[]) {
5
- injectionKeys.forEach(key => provide(key, ref()))
6
- }
@@ -1,20 +0,0 @@
1
- import { isObjectField } from '@formvk/core'
2
- import type { Form, GeneralField, ObjectField } from '@formvk/core'
3
- import { computed } from 'vue'
4
- import type { ComputedRef } from 'vue'
5
-
6
- import { useField } from './useField'
7
- import { useForm } from './useForm'
8
-
9
- export function useParentForm(): ComputedRef<Form | ObjectField> {
10
- const field = useField()
11
- const form = useForm()
12
-
13
- function findObjectParent(target?: GeneralField): Form | ObjectField {
14
- if (!target) return form.value
15
- if (isObjectField(target)) return target
16
- return findObjectParent(target.parent)
17
- }
18
-
19
- return computed(() => findObjectParent(field.value))
20
- }
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from '@formvk/schema'
2
- export * from './components'
3
- export * from './hooks'
4
- export * from './shared'
5
- export * from './types'
@@ -1,75 +0,0 @@
1
- import { isVoidField } from '@formvk/core'
2
- import type { GeneralField } from '@formvk/core'
3
- import { each, FormPath, isFn, isStr, isValid } from '@formvk/shared'
4
- import { defineComponent, h, markRaw } from 'vue'
5
-
6
- import { useField } from '../hooks/useField'
7
- import type { IComponentMapper, IStateMapper, VueComponent } from '../types'
8
-
9
- export function mapProps<T extends VueComponent = VueComponent>(...args: IStateMapper[]) {
10
- function transform(input: Record<string, any>, field: GeneralField) {
11
- return args.reduce((props, mapper) => {
12
- if (isFn(mapper)) {
13
- props = Object.assign(props, mapper(props, field))
14
- } else {
15
- each(mapper, (to, extract) => {
16
- const extractValue = FormPath.getIn(field, extract)
17
- const targetValue = isStr(to) ? to : extract
18
- const originalValue = FormPath.getIn(props, targetValue)
19
- if (extract === 'value') {
20
- if (to !== extract) {
21
- delete props.value
22
- }
23
- }
24
- if (isValid(originalValue) && !isValid(extractValue)) return
25
- FormPath.setIn(props, targetValue, extractValue)
26
- })
27
- }
28
- return props
29
- }, input)
30
- }
31
-
32
- return function (target: T): T {
33
- return defineComponent({
34
- name: target.name ? `Connected${target.name}` : 'ConnectedComponent',
35
- inheritAttrs: false,
36
- setup(_, { attrs, slots }) {
37
- const fieldRef = useField()
38
- return () => {
39
- const newAttrs = fieldRef.value ? transform({ ...attrs }, fieldRef.value) : { ...attrs }
40
- return h(target, newAttrs, slots)
41
- }
42
- },
43
- }) as unknown as T
44
- }
45
- }
46
-
47
- export function mapReadPretty(component: VueComponent, readPrettyProps?: Record<string, any>) {
48
- return function <T extends VueComponent>(target: T): T {
49
- return defineComponent({
50
- name: target.name ? `Read${target.name}` : 'ReadComponent',
51
- inheritAttrs: false,
52
- setup(_, { attrs, slots }) {
53
- const fieldRef = useField()
54
- return () => {
55
- const field = fieldRef.value
56
- const Current = field && !isVoidField(field) && field.pattern === 'readPretty' ? component : target
57
- return h(Current, { ...readPrettyProps, ...attrs }, slots)
58
- }
59
- },
60
- }) as unknown as T
61
- }
62
- }
63
-
64
- export function connect<T extends VueComponent>(target: T, ...args: IComponentMapper[]): T {
65
- const Component = args.reduce((current: VueComponent, mapper) => mapper(current), target as VueComponent)
66
- return markRaw(
67
- defineComponent({
68
- name: target.name,
69
- inheritAttrs: false,
70
- setup(_, { attrs, slots }) {
71
- return () => h(Component, attrs, slots)
72
- },
73
- })
74
- ) as unknown as T
75
- }
@@ -1,15 +0,0 @@
1
- import type { Form, GeneralField } from '@formvk/core'
2
- import type { Schema } from '@formvk/schema'
3
- import type { InjectionKey, Ref } from 'vue'
4
-
5
- import type { ISchemaFieldRuntimeOptions } from '../types'
6
-
7
- export const FormSymbol: InjectionKey<Ref<Form>> = Symbol('form')
8
-
9
- export const FieldSymbol: InjectionKey<Ref<GeneralField>> = Symbol('field')
10
-
11
- export const SchemaSymbol: InjectionKey<Ref<Schema>> = Symbol('schema')
12
-
13
- export const SchemaExpressionScopeSymbol: InjectionKey<Ref<Record<string, any>>> = Symbol('schemaExpression')
14
-
15
- export const SchemaOptionsSymbol: InjectionKey<Ref<ISchemaFieldRuntimeOptions>> = Symbol('schemaOptions')
@@ -1,7 +0,0 @@
1
- import type { Form, IFormProps } from '@formvk/core'
2
- import { createForm as createCoreForm } from '@formvk/core'
3
- import { markRaw } from 'vue'
4
-
5
- export function createForm<T extends object = any>(options?: IFormProps<T>): Form<T> {
6
- return markRaw(createCoreForm<T>(options)) as Form<T>
7
- }
@@ -1,2 +0,0 @@
1
- export { connect, mapProps, mapReadPretty } from './connect'
2
- export { createForm } from './createForm'
@@ -1,141 +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 { Component } from 'vue'
13
-
14
- // 仅用于工厂返回值类型,type-only 引用不产生运行时循环依赖
15
- import type { SchemaField } from '../components/SchemaField'
16
-
17
- export type VueComponent = Component
18
-
19
- /** 提取 Vue 组件定义的 props 类型。 */
20
- export type ComponentProps<T> = T extends abstract new (...args: any) => { $props: infer Props } ? Props : never
21
-
22
- export type SchemaVueComponents = Record<string, VueComponent>
23
-
24
- /**
25
- * 从扁平 components 与两个分类组件 map 分别推断 fragment 的三个 props map,组件 props 自动提取。
26
- * 分类优先级与运行时 Proxy 一致:array > void > field,已分类的名字不会出现在 field 映射中,
27
- * 避免同一组件名同时命中 field / void / array 多个动态构造器重载。
28
- * 分类泛型默认空 map,keyof 结果为 never,省略分类时不会污染 field 推断。
29
- */
30
- export type InferFieldProps<
31
- F extends SchemaVueComponents,
32
- V extends SchemaVueComponents,
33
- A extends SchemaVueComponents,
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 SchemaVueComponents, A extends SchemaVueComponents> = {
39
- [K in keyof V as K extends keyof A ? never : K]: TPropsOfComponent<V[K]>
40
- }
41
-
42
- export type InferArrayProps<A extends SchemaVueComponents> = {
43
- [K in keyof A]: TPropsOfComponent<A[K]>
44
- }
45
-
46
- export interface IProviderProps {
47
- form: Form
48
- }
49
-
50
- export interface IFormConsumerSlotProps {
51
- form: Form
52
- }
53
-
54
- /** 注入后代组件的运行时选项,components 始终是扁平 name → component。 */
55
- export interface ISchemaFieldRuntimeOptions {
56
- components?: SchemaVueComponents
57
- scope?: Record<string, any>
58
- }
59
-
60
- export interface ISchemaFieldFactoryOptions<
61
- F extends SchemaVueComponents = SchemaVueComponents,
62
- V extends SchemaVueComponents = {},
63
- A extends SchemaVueComponents = {},
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 SchemaVueComponents,
88
- V extends SchemaVueComponents,
89
- A extends SchemaVueComponents,
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?: SchemaVueComponents
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
- }
132
-
133
- export interface IExpressionScopeProps {
134
- value: any
135
- }
136
-
137
- export type IComponentMapper<T extends VueComponent = VueComponent> = (target: T) => VueComponent
138
-
139
- export type IStateMapper<Props = Record<string, any>> =
140
- | Record<string, string | boolean>
141
- | ((props: Props, field: GeneralField) => Props)
@@ -1,11 +0,0 @@
1
- import { FormPath, isStr } from '@formvk/shared'
2
- import { toRaw } from 'vue'
3
-
4
- /**
5
- * 解析 schema 中的组件描述:字符串按路径从 components 中查找,组件对象直接使用。
6
- * 返回前统一 toRaw,避免组件定义被响应式代理包裹。
7
- */
8
- export function getRawComponent(components: Record<string, any> | undefined, component: any): any {
9
- const resolved = isStr(component) ? FormPath.getIn(components, component) : undefined
10
- return toRaw(resolved ?? component)
11
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "paths": {},
5
- "types": []
6
- },
7
- "include": ["./src/**/*.ts", "./src/**/*.tsx"],
8
- "exclude": ["./src/**/__tests__/**", "./__tests__/**"]
9
- }
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "jsx": "preserve",
5
- "jsxImportSource": "vue"
6
- },
7
- "include": ["./src/**/*.ts", "./src/**/*.tsx"]
8
- }
package/vite.config.ts DELETED
@@ -1,23 +0,0 @@
1
- import vueJsx from '@vitejs/plugin-vue-jsx'
2
- import { defineConfig } from 'vite'
3
- import dts from 'vite-plugin-dts'
4
-
5
- export default defineConfig({
6
- plugins: [vueJsx(), 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.Vue',
14
- fileName: 'index',
15
- formats: ['es'],
16
- },
17
- sourcemap: true,
18
- target: 'esnext',
19
- rollupOptions: {
20
- external: ['vue', '@formvk/core', '@formvk/schema', '@formvk/shared', '@formvk/reactive'],
21
- },
22
- },
23
- })