@formvk/core 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.
Files changed (43) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +63 -0
  3. package/package.json +8 -4
  4. package/src/__tests__/array.spec.ts +0 -168
  5. package/src/__tests__/effects.spec.ts +0 -595
  6. package/src/__tests__/externals.spec.ts +0 -101
  7. package/src/__tests__/field.spec.ts +0 -572
  8. package/src/__tests__/form.spec.ts +0 -720
  9. package/src/__tests__/graph.spec.ts +0 -56
  10. package/src/__tests__/heart.spec.ts +0 -60
  11. package/src/__tests__/internals.spec.ts +0 -138
  12. package/src/__tests__/lifecycle.spec.ts +0 -51
  13. package/src/__tests__/object.spec.ts +0 -107
  14. package/src/__tests__/query.spec.ts +0 -100
  15. package/src/__tests__/shared.ts +0 -10
  16. package/src/__tests__/typed.spec.ts +0 -52
  17. package/src/__tests__/void.spec.ts +0 -163
  18. package/src/effects/index.ts +0 -2
  19. package/src/effects/onFieldEffects.ts +0 -97
  20. package/src/effects/onFormEffects.ts +0 -48
  21. package/src/global.d.ts +0 -8
  22. package/src/index.ts +0 -4
  23. package/src/models/ArrayField.ts +0 -133
  24. package/src/models/BaseField.ts +0 -359
  25. package/src/models/Field.ts +0 -450
  26. package/src/models/Form.ts +0 -586
  27. package/src/models/Graph.ts +0 -56
  28. package/src/models/Heart.ts +0 -75
  29. package/src/models/LifeCycle.ts +0 -42
  30. package/src/models/ObjectField.ts +0 -48
  31. package/src/models/Query.ts +0 -97
  32. package/src/models/VoidField.ts +0 -64
  33. package/src/models/index.ts +0 -9
  34. package/src/models/types.ts +0 -9
  35. package/src/shared/checkers.ts +0 -85
  36. package/src/shared/constants.ts +0 -60
  37. package/src/shared/effective.ts +0 -69
  38. package/src/shared/externals.ts +0 -65
  39. package/src/shared/internals.ts +0 -965
  40. package/src/types.ts +0 -401
  41. package/tsconfig.build.json +0 -9
  42. package/tsconfig.json +0 -4
  43. package/vite.config.ts +0 -46
@@ -1,163 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm, isVoidField, LifeCycleTypes } from '..'
4
- import { attach } from './shared'
5
-
6
- test('创建虚拟字段', () => {
7
- const form = attach(createForm())
8
- const field = attach(form.createVoidField({ name: 'void', title: '虚拟字段' })!)
9
-
10
- expect(field).not.toBeUndefined()
11
- expect(isVoidField(field)).toBeTruthy()
12
- expect(field.address.toString()).toEqual('void')
13
- expect(field.path.toString()).toEqual('void')
14
- expect(field.title).toEqual('虚拟字段')
15
- expect(field.initialized).toBeTruthy()
16
- expect((field as any).value).toBeUndefined()
17
- expect(form.createVoidField({ name: '' })).toBeUndefined()
18
- })
19
-
20
- test('虚拟字段的子字段路径', () => {
21
- const form = attach(createForm())
22
- const parent = attach(form.createVoidField({ name: 'parent' })!)
23
- const voidField = attach(form.createVoidField({ name: 'void', basePath: 'parent' })!)
24
- const children = attach(form.createField({ name: 'children', basePath: 'parent.void' })!)
25
-
26
- expect(children.address.toString()).toEqual('parent.void.children')
27
- expect(children.path.toString()).toEqual('children')
28
- expect(children.parent).toBe(voidField)
29
- expect(voidField.parent).toBe(parent)
30
- })
31
-
32
- test('虚拟字段的显示与交互模式继承', () => {
33
- const form = attach(createForm())
34
- const parent = attach(form.createVoidField({ name: 'parent' })!)
35
- const child = attach(form.createField({ name: 'child', basePath: 'parent' })!)
36
-
37
- expect(parent.display).toEqual('visible')
38
- expect(child.display).toEqual('visible')
39
- expect(parent.pattern).toEqual('editable')
40
- expect(child.pattern).toEqual('editable')
41
-
42
- parent.setDisplay('none')
43
- expect(child.display).toEqual('none')
44
-
45
- parent.setDisplay('hidden')
46
- expect(child.display).toEqual('hidden')
47
-
48
- parent.setPattern('readOnly')
49
- expect(child.pattern).toEqual('readOnly')
50
-
51
- parent.setPattern('disabled')
52
- expect(child.pattern).toEqual('disabled')
53
-
54
- form.setDisplay('hidden')
55
- expect(parent.display).toEqual('hidden')
56
- })
57
-
58
- test('虚拟字段挂载与卸载生命周期', () => {
59
- const form = attach(createForm())
60
- const field = form.createVoidField({ name: 'void' })!
61
- const types: string[] = []
62
- form.subscribe(({ type }) => {
63
- types.push(type)
64
- })
65
-
66
- expect(field.mounted).toBeFalsy()
67
- attach(field)
68
- expect(field.mounted).toBeTruthy()
69
- expect(types).toContain(LifeCycleTypes.ON_FIELD_MOUNT)
70
-
71
- field.onUnmount()
72
- expect(field.mounted).toBeFalsy()
73
- expect(field.unmounted).toBeTruthy()
74
- expect(types).toContain(LifeCycleTypes.ON_FIELD_UNMOUNT)
75
- })
76
-
77
- test('设置与读取虚拟字段状态', () => {
78
- const form = attach(createForm())
79
- const field = attach(form.createVoidField({ name: 'void' })!)
80
-
81
- field.setState({ title: '标题', description: '描述', display: 'none' })
82
- expect(field.title).toEqual('标题')
83
- expect(field.description).toEqual('描述')
84
- expect(field.display).toEqual('none')
85
- expect(field.getState(state => state.title)).toEqual('标题')
86
-
87
- const state: any = field.getState()
88
- expect(state.address).toEqual('void')
89
- expect(state.path).toEqual('void')
90
- expect(state.display).toEqual('none')
91
- })
92
-
93
- test('虚拟字段查询子字段', () => {
94
- const form = attach(createForm())
95
- const parent = attach(form.createVoidField({ name: 'parent' })!)
96
- attach(form.createField({ name: 'aa', basePath: 'parent' }))
97
- attach(form.createField({ name: 'bb', basePath: 'parent' }))
98
-
99
- const addresses = parent.query('*').map(field => field.address.toString())
100
- expect(addresses).toEqual(['parent', 'parent.aa', 'parent.bb'])
101
- expect(parent.query('parent.aa').take()?.address.toString()).toEqual('parent.aa')
102
- })
103
-
104
- test('销毁虚拟字段', () => {
105
- const form = attach(createForm())
106
- const field = attach(form.createVoidField({ name: 'void' })!)
107
-
108
- expect(form.fields.void).toBe(field)
109
- field.destroy()
110
- expect(form.fields.void).toBeUndefined()
111
- })
112
-
113
- test('虚拟字段支持组件与装饰器配置', () => {
114
- const form = attach(createForm())
115
- const field = attach(form.createVoidField({ name: 'void' })!)
116
- function Component() {
117
- return null
118
- }
119
- function Decorator() {
120
- return null
121
- }
122
-
123
- field.setComponent(Component, { a: 1 })
124
- field.setComponentProps({ b: 2 })
125
- field.setDecorator(Decorator, { c: 3 })
126
- field.setDecoratorProps({ d: 4 })
127
-
128
- expect(field.componentType).toBe(Component)
129
- expect(field.componentProps).toEqual({ a: 1, b: 2 })
130
- expect(field.decoratorType).toBe(Decorator)
131
- expect(field.decoratorProps).toEqual({ c: 3, d: 4 })
132
- })
133
-
134
- test('虚拟字段支持标题、描述、内容与数据', () => {
135
- const form = attach(createForm())
136
- const field = attach(form.createVoidField({ name: 'void' })!)
137
-
138
- field.setTitle('标题')
139
- field.setDescription('描述')
140
- field.setContent('内容')
141
- field.setData({ extra: 1 })
142
-
143
- expect(field.title).toEqual('标题')
144
- expect(field.description).toEqual('描述')
145
- expect(field.content).toEqual('内容')
146
- expect(field.data).toEqual({ extra: 1 })
147
- })
148
-
149
- test('虚拟字段 reactions 响应式执行', () => {
150
- const form = attach(createForm())
151
- const field = attach(
152
- form.createVoidField({
153
- name: 'void',
154
- reactions(field) {
155
- field.title = field.display === 'none' ? '隐藏' : '显示'
156
- },
157
- })!
158
- )
159
- expect(field.title).toEqual('显示')
160
-
161
- field.setDisplay('none')
162
- expect(field.title).toEqual('隐藏')
163
- })
@@ -1,2 +0,0 @@
1
- export * from './onFormEffects'
2
- export * from './onFieldEffects'
@@ -1,97 +0,0 @@
1
- import { autorun, batch, reaction } from '@formvk/reactive'
2
- import { FormPath, isFn, toArr } from '@formvk/shared'
3
-
4
- import type { Form } from '../models'
5
- import { createEffectHook, useEffectForm } from '../shared/effective'
6
- import type { DataField, FormPathPattern, GeneralField, IFieldState } from '../types'
7
- import { LifeCycleTypes } from '../types'
8
-
9
- function createFieldEffect<Result extends GeneralField = GeneralField>(type: LifeCycleTypes) {
10
- return createEffectHook(
11
- type,
12
- (field: Result, form: Form) => (pattern: FormPathPattern, callback: (field: Result, form: Form) => void) => {
13
- if (FormPath.parse(pattern).matchAliasGroup(field.address, field.path)) {
14
- batch(() => {
15
- callback(field, form)
16
- })
17
- }
18
- }
19
- )
20
- }
21
- const _onFieldInit = createFieldEffect(LifeCycleTypes.ON_FIELD_INIT)
22
- export const onFieldMount = createFieldEffect(LifeCycleTypes.ON_FIELD_MOUNT)
23
- export const onFieldUnmount = createFieldEffect(LifeCycleTypes.ON_FIELD_UNMOUNT)
24
- export const onFieldValueChange = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_VALUE_CHANGE)
25
- export const onFieldInitialValueChange = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE)
26
- export const onFieldInputValueChange = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_INPUT_VALUE_CHANGE)
27
- export const onFieldValidateStart = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_VALIDATE_START)
28
- export const onFieldValidateEnd = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_VALIDATE_END)
29
- export const onFieldValidating = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_VALIDATING)
30
- export const onFieldValidateFailed = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_VALIDATE_FAILED)
31
- export const onFieldValidateSuccess = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_VALIDATE_SUCCESS)
32
- export const onFieldSubmit = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT)
33
- export const onFieldSubmitStart = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_START)
34
- export const onFieldSubmitEnd = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_END)
35
- export const onFieldSubmitValidateStart = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_START)
36
- export const onFieldSubmitValidateEnd = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_END)
37
- export const onFieldSubmitSuccess = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_SUCCESS)
38
- export const onFieldSubmitFailed = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_FAILED)
39
- export const onFieldSubmitValidateSuccess = createFieldEffect<DataField>(
40
- LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_SUCCESS
41
- )
42
- export const onFieldSubmitValidateFailed = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_SUBMIT_VALIDATE_FAILED)
43
- export const onFieldReset = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_RESET)
44
- export const onFieldLoading = createFieldEffect<DataField>(LifeCycleTypes.ON_FIELD_LOADING)
45
-
46
- export function onFieldInit(pattern: FormPathPattern, callback?: (field: GeneralField, form: Form) => void) {
47
- const form = useEffectForm()
48
- const count = form.query(pattern).reduce((count, field) => {
49
- callback?.(field, form)
50
- return count + 1
51
- }, 0)
52
- if (count === 0) {
53
- _onFieldInit(pattern, callback!)
54
- }
55
- }
56
-
57
- export function onFieldReact(pattern: FormPathPattern, callback?: (field: GeneralField, form: Form) => void) {
58
- onFieldInit(pattern, (field, form) => {
59
- field.disposers.push(
60
- autorun(() => {
61
- if (isFn(callback)) callback(field, form)
62
- })
63
- )
64
- })
65
- }
66
- export function onFieldChange(pattern: FormPathPattern, callback?: (field: GeneralField, form: Form) => void): void
67
- export function onFieldChange(
68
- pattern: FormPathPattern,
69
- watches: (keyof IFieldState)[],
70
- callback?: (field: GeneralField, form: Form) => void
71
- ): void
72
- export function onFieldChange(
73
- pattern: FormPathPattern,
74
- watches: any,
75
- callback?: (field: GeneralField, form: Form) => void
76
- ): void {
77
- if (isFn(watches)) {
78
- callback = watches
79
- watches = ['value']
80
- } else {
81
- watches = watches || ['value']
82
- }
83
- onFieldInit(pattern, (field, form) => {
84
- if (isFn(callback)) callback(field, form)
85
- const dispose = reaction(
86
- () => {
87
- return toArr(watches).map((key: keyof IFieldState) => {
88
- return (field as unknown as Record<string, any>)[key]
89
- })
90
- },
91
- () => {
92
- if (isFn(callback)) callback(field, form)
93
- }
94
- )
95
- field.disposers.push(dispose)
96
- })
97
- }
@@ -1,48 +0,0 @@
1
- import { autorun, batch } from '@formvk/reactive'
2
- import { isFn } from '@formvk/shared'
3
-
4
- import type { Form } from '../models'
5
- import { createEffectHook } from '../shared/effective'
6
- import { LifeCycleTypes } from '../types'
7
-
8
- function createFormEffect(type: LifeCycleTypes) {
9
- return createEffectHook(type, (form: Form) => (callback: (form: Form) => void) => {
10
- batch(() => {
11
- callback(form)
12
- })
13
- })
14
- }
15
-
16
- export const onFormInit = createFormEffect(LifeCycleTypes.ON_FORM_INIT)
17
- export const onFormMount = createFormEffect(LifeCycleTypes.ON_FORM_MOUNT)
18
- export const onFormUnmount = createFormEffect(LifeCycleTypes.ON_FORM_UNMOUNT)
19
- export const onFormValuesChange = createFormEffect(LifeCycleTypes.ON_FORM_VALUES_CHANGE)
20
- export const onFormInitialValuesChange = createFormEffect(LifeCycleTypes.ON_FORM_INITIAL_VALUES_CHANGE)
21
- export const onFormInputChange = createFormEffect(LifeCycleTypes.ON_FORM_INPUT_CHANGE)
22
- export const onFormSubmit = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT)
23
- export const onFormReset = createFormEffect(LifeCycleTypes.ON_FORM_RESET)
24
- export const onFormSubmitStart = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_START)
25
- export const onFormSubmitEnd = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_END)
26
- export const onFormSubmitSuccess = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_SUCCESS)
27
- export const onFormSubmitFailed = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_FAILED)
28
- export const onFormSubmitValidateStart = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_START)
29
- export const onFormSubmitValidateSuccess = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_SUCCESS)
30
- export const onFormSubmitValidateFailed = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_FAILED)
31
- export const onFormSubmitValidateEnd = createFormEffect(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_END)
32
- export const onFormValidateStart = createFormEffect(LifeCycleTypes.ON_FORM_VALIDATE_START)
33
- export const onFormValidateSuccess = createFormEffect(LifeCycleTypes.ON_FORM_VALIDATE_SUCCESS)
34
- export const onFormValidateFailed = createFormEffect(LifeCycleTypes.ON_FORM_VALIDATE_FAILED)
35
- export const onFormValidateEnd = createFormEffect(LifeCycleTypes.ON_FORM_VALIDATE_END)
36
- export const onFormGraphChange = createFormEffect(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
37
- export const onFormLoading = createFormEffect(LifeCycleTypes.ON_FORM_LOADING)
38
- export function onFormReact(callback?: (form: Form) => void) {
39
- let dispose: (() => void) | undefined
40
- onFormInit(form => {
41
- dispose = autorun(() => {
42
- if (isFn(callback)) callback(form)
43
- })
44
- })
45
- onFormUnmount(() => {
46
- dispose?.()
47
- })
48
- }
package/src/global.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import * as Models from './models'
2
- import * as Types from './types'
3
-
4
- declare global {
5
- namespace Formily.Core {
6
- export { Types, Models }
7
- }
8
- }
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './shared/externals'
2
- export * from './models/types'
3
- export * from './effects'
4
- export * from './types'
@@ -1,133 +0,0 @@
1
- import { action, reaction } from '@formvk/reactive'
2
- import { isArr, move } from '@formvk/shared'
3
-
4
- import { cleanupArrayChildren, exchangeArrayState, spliceArrayState } from '../shared/internals'
5
- import type { FormPathPattern, IFieldProps } from '../types'
6
- import { Field } from './Field'
7
- import type { Form } from './Form'
8
-
9
- export class ArrayField<
10
- Value extends any[] = any[],
11
- ComponentProps = any,
12
- DecoratorProps = any,
13
- IRecord extends object = any,
14
- > extends Field<Value, ComponentProps, DecoratorProps, IRecord> {
15
- displayName = 'ArrayField' as const
16
-
17
- constructor(address: FormPathPattern, props: IFieldProps<any, any, any, Value>, form: Form, designable?: boolean) {
18
- super(address, props, form, designable)
19
- this.makeAutoCleanable()
20
- }
21
-
22
- protected makeAutoCleanable() {
23
- this.disposers.push(
24
- reaction(
25
- () => this.value?.length,
26
- (newLength, oldLength) => {
27
- if (oldLength && !newLength) {
28
- cleanupArrayChildren(this, 0)
29
- } else if (newLength! < oldLength!) {
30
- cleanupArrayChildren(this, newLength!)
31
- }
32
- }
33
- )
34
- )
35
- }
36
-
37
- push = (...items: any[]) => {
38
- return action(() => {
39
- if (!isArr(this.value)) {
40
- this.value = [] as any
41
- }
42
- ;(this.value as any[]).push(...items)
43
- return this.onInput(this.value)
44
- })
45
- }
46
-
47
- pop = () => {
48
- if (!isArr(this.value)) return
49
- return action(() => {
50
- const index = (this.value as any[]).length - 1
51
- spliceArrayState(this, {
52
- startIndex: index,
53
- deleteCount: 1,
54
- })
55
- ;(this.value as any[]).pop()
56
- return this.onInput(this.value)
57
- })
58
- }
59
-
60
- insert = (index: number, ...items: any[]) => {
61
- return action(() => {
62
- if (!isArr(this.value)) {
63
- this.value = [] as any
64
- }
65
- if (items.length === 0) {
66
- return
67
- }
68
- spliceArrayState(this, {
69
- startIndex: index,
70
- insertCount: items.length,
71
- })
72
- ;(this.value as any[]).splice(index, 0, ...items)
73
- return this.onInput(this.value)
74
- })
75
- }
76
-
77
- remove = (index: number) => {
78
- if (!isArr(this.value)) return
79
- return action(() => {
80
- spliceArrayState(this, {
81
- startIndex: index,
82
- deleteCount: 1,
83
- })
84
- ;(this.value as any[]).splice(index, 1)
85
- return this.onInput(this.value)
86
- })
87
- }
88
-
89
- shift = () => {
90
- if (!isArr(this.value)) return
91
- return action(() => {
92
- ;(this.value as any[]).shift()
93
- return this.onInput(this.value)
94
- })
95
- }
96
-
97
- unshift = (...items: any[]) => {
98
- return action(() => {
99
- if (!isArr(this.value)) {
100
- this.value = [] as any
101
- }
102
- spliceArrayState(this, {
103
- startIndex: 0,
104
- insertCount: items.length,
105
- })
106
- ;(this.value as any[]).unshift(...items)
107
- return this.onInput(this.value)
108
- })
109
- }
110
-
111
- move = (fromIndex: number, toIndex: number) => {
112
- if (!isArr(this.value)) return
113
- if (fromIndex === toIndex) return
114
- return action(() => {
115
- move(this.value as any[], fromIndex, toIndex)
116
- exchangeArrayState(this, {
117
- fromIndex,
118
- toIndex,
119
- })
120
- return this.onInput(this.value)
121
- })
122
- }
123
-
124
- moveUp = (index: number) => {
125
- if (!isArr(this.value)) return
126
- return this.move(index, index - 1 < 0 ? (this.value as any[]).length - 1 : index - 1)
127
- }
128
-
129
- moveDown = (index: number) => {
130
- if (!isArr(this.value)) return
131
- return this.move(index, index + 1 >= (this.value as any[]).length ? 0 : index + 1)
132
- }
133
- }