@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.
- package/CHANGELOG.md +14 -0
- package/README.md +63 -0
- package/package.json +8 -4
- package/src/__tests__/array.spec.ts +0 -168
- package/src/__tests__/effects.spec.ts +0 -595
- package/src/__tests__/externals.spec.ts +0 -101
- package/src/__tests__/field.spec.ts +0 -572
- package/src/__tests__/form.spec.ts +0 -720
- package/src/__tests__/graph.spec.ts +0 -56
- package/src/__tests__/heart.spec.ts +0 -60
- package/src/__tests__/internals.spec.ts +0 -138
- package/src/__tests__/lifecycle.spec.ts +0 -51
- package/src/__tests__/object.spec.ts +0 -107
- package/src/__tests__/query.spec.ts +0 -100
- package/src/__tests__/shared.ts +0 -10
- package/src/__tests__/typed.spec.ts +0 -52
- package/src/__tests__/void.spec.ts +0 -163
- package/src/effects/index.ts +0 -2
- package/src/effects/onFieldEffects.ts +0 -97
- package/src/effects/onFormEffects.ts +0 -48
- package/src/global.d.ts +0 -8
- package/src/index.ts +0 -4
- package/src/models/ArrayField.ts +0 -133
- package/src/models/BaseField.ts +0 -359
- package/src/models/Field.ts +0 -450
- package/src/models/Form.ts +0 -586
- package/src/models/Graph.ts +0 -56
- package/src/models/Heart.ts +0 -75
- package/src/models/LifeCycle.ts +0 -42
- package/src/models/ObjectField.ts +0 -48
- package/src/models/Query.ts +0 -97
- package/src/models/VoidField.ts +0 -64
- package/src/models/index.ts +0 -9
- package/src/models/types.ts +0 -9
- package/src/shared/checkers.ts +0 -85
- package/src/shared/constants.ts +0 -60
- package/src/shared/effective.ts +0 -69
- package/src/shared/externals.ts +0 -65
- package/src/shared/internals.ts +0 -965
- package/src/types.ts +0 -401
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -4
- package/vite.config.ts +0 -46
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @formvk/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic form engine behind Formvk. It owns the form model — `Form`, `Field`, `ArrayField`, `ObjectField` and `VoidField` — with reactive values, field queries, lifecycle effects and the validation/submit pipeline. It has no dependency on Vue or React.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @formvk/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createForm, onFieldValueChange } from '@formvk/core'
|
|
15
|
+
|
|
16
|
+
const form = createForm({
|
|
17
|
+
values: {
|
|
18
|
+
name: 'alice',
|
|
19
|
+
address: { city: '', zip: '' },
|
|
20
|
+
},
|
|
21
|
+
effects() {
|
|
22
|
+
onFieldValueChange('address.city', (_field, form) => {
|
|
23
|
+
form.setFieldState('address.zip', state => {
|
|
24
|
+
state.value = ''
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// values are reactive: any mutation is tracked by the model
|
|
31
|
+
form.values.name = 'bob'
|
|
32
|
+
|
|
33
|
+
await form.validate()
|
|
34
|
+
await form.submit(values => {
|
|
35
|
+
console.log(values)
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Fields can also be created imperatively:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
const email = form.createField({ name: 'email', required: true })
|
|
43
|
+
email.setValue('alice@example.com')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
- **Form model** — `createForm(options)`, `Form`, `Field`, `ArrayField`, `ObjectField`, `VoidField`, `Query`, `Graph`, `Heart`, `LifeCycle`.
|
|
49
|
+
- **Effects** — `onFormInit`, `onFormMount`, `onFormValuesChange`, `onFormSubmit`, `onFieldInit`, `onFieldValueChange`, `onFieldInputValueChange`, `onFieldReact`, ...
|
|
50
|
+
- **Validation & submit** — `form.validate()`, `form.submit()`, `form.reset()`, `form.setValues()`.
|
|
51
|
+
- **Type guards** — `isForm`, `isField`, `isDataField`, `isArrayField`, `isObjectField`, `isVoidField`, `isGeneralField`, ...
|
|
52
|
+
- **Validator registry** — `registerValidateRules`, `registerValidateFormats`, `registerValidateLocale`, `setValidateLanguage`, ...
|
|
53
|
+
|
|
54
|
+
`@formvk/schema` compiles schemas into this model, and `@formvk/react` / `@formvk/vue` render it.
|
|
55
|
+
|
|
56
|
+
## Documentation
|
|
57
|
+
|
|
58
|
+
- Core API: https://formvk.github.io/core/api/core
|
|
59
|
+
- Getting started: https://formvk.github.io/core/guide/getting-started
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formvk/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Formvk Core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"core",
|
|
@@ -10,14 +10,18 @@
|
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"author": "yiyunwan",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"CHANGELOG.md"
|
|
16
|
+
],
|
|
13
17
|
"type": "module",
|
|
14
18
|
"main": "dist/index.js",
|
|
15
19
|
"module": "dist/index.js",
|
|
16
20
|
"types": "dist/index.d.ts",
|
|
17
21
|
"dependencies": {
|
|
18
|
-
"@formvk/reactive": "
|
|
19
|
-
"@formvk/shared": "
|
|
20
|
-
"@formvk/validator": "
|
|
22
|
+
"@formvk/reactive": "~1.0.0",
|
|
23
|
+
"@formvk/shared": "~1.0.0",
|
|
24
|
+
"@formvk/validator": "~1.0.0",
|
|
21
25
|
"@swc/helpers": "^0.5.24"
|
|
22
26
|
},
|
|
23
27
|
"scripts": {
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { expect, test } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { createForm, isArrayField } from '..'
|
|
4
|
-
import { attach } from './shared'
|
|
5
|
-
|
|
6
|
-
test('创建数组字段', () => {
|
|
7
|
-
const form = attach(createForm())
|
|
8
|
-
const field = attach(form.createArrayField({ name: 'array' })!)
|
|
9
|
-
expect(isArrayField(field)).toBeTruthy()
|
|
10
|
-
expect(field.value).toEqual([])
|
|
11
|
-
expect(form.values.array).toEqual([])
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
test('数组字段的增删操作', async () => {
|
|
15
|
-
const form = attach(createForm())
|
|
16
|
-
const field = attach(form.createArrayField({ name: 'array', value: [1, 2] })!)
|
|
17
|
-
|
|
18
|
-
await field.push(3)
|
|
19
|
-
expect(field.value).toEqual([1, 2, 3])
|
|
20
|
-
|
|
21
|
-
await field.pop()
|
|
22
|
-
expect(field.value).toEqual([1, 2])
|
|
23
|
-
|
|
24
|
-
await field.unshift(0)
|
|
25
|
-
expect(field.value).toEqual([0, 1, 2])
|
|
26
|
-
|
|
27
|
-
await field.shift()
|
|
28
|
-
expect(field.value).toEqual([1, 2])
|
|
29
|
-
|
|
30
|
-
await field.insert(1, 'x')
|
|
31
|
-
expect(field.value).toEqual([1, 'x', 2])
|
|
32
|
-
|
|
33
|
-
await field.remove(1)
|
|
34
|
-
expect(field.value).toEqual([1, 2])
|
|
35
|
-
expect(form.values.array).toEqual([1, 2])
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test('数组字段的移动操作', async () => {
|
|
39
|
-
const form = attach(createForm())
|
|
40
|
-
const field = attach(form.createArrayField({ name: 'array', value: [1, 2, 3] })!)
|
|
41
|
-
|
|
42
|
-
await field.move(0, 2)
|
|
43
|
-
expect(field.value).toEqual([2, 3, 1])
|
|
44
|
-
|
|
45
|
-
await field.moveUp(0)
|
|
46
|
-
expect(field.value).toEqual([3, 1, 2])
|
|
47
|
-
|
|
48
|
-
await field.moveDown(0)
|
|
49
|
-
expect(field.value).toEqual([1, 3, 2])
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('数组字段值同步到表单', () => {
|
|
53
|
-
const form = attach(createForm())
|
|
54
|
-
const field = attach(form.createArrayField({ name: 'array' })!)
|
|
55
|
-
|
|
56
|
-
field.value = [1, 2]
|
|
57
|
-
expect(form.values.array).toEqual([1, 2])
|
|
58
|
-
|
|
59
|
-
form.setValuesIn('array', [3, 4])
|
|
60
|
-
expect(field.value).toEqual([3, 4])
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
test('删除数组元素会同步子字段', async () => {
|
|
64
|
-
const form = attach(createForm())
|
|
65
|
-
const field = attach(form.createArrayField({ name: 'array', value: ['a', 'b', 'c'] })!)
|
|
66
|
-
const first = attach(form.createField({ name: '0', basePath: 'array' })!)
|
|
67
|
-
const second = attach(form.createField({ name: '1', basePath: 'array' })!)
|
|
68
|
-
const third = attach(form.createField({ name: '2', basePath: 'array' })!)
|
|
69
|
-
|
|
70
|
-
expect(first.value).toEqual('a')
|
|
71
|
-
expect(second.value).toEqual('b')
|
|
72
|
-
expect(third.value).toEqual('c')
|
|
73
|
-
|
|
74
|
-
await field.remove(0)
|
|
75
|
-
expect(field.value).toEqual(['b', 'c'])
|
|
76
|
-
expect(form.fields['array.0']).toBe(second)
|
|
77
|
-
expect(form.fields['array.1']).toBe(third)
|
|
78
|
-
expect(form.fields['array.2']).toBeUndefined()
|
|
79
|
-
expect(second.value).toEqual('b')
|
|
80
|
-
expect(third.value).toEqual('c')
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('重置数组字段', async () => {
|
|
84
|
-
const form = attach(createForm())
|
|
85
|
-
const field = attach(form.createArrayField({ name: 'array', initialValue: [1, 2] })!)
|
|
86
|
-
|
|
87
|
-
expect(field.value).toEqual([1, 2])
|
|
88
|
-
await field.push(3)
|
|
89
|
-
expect(field.value).toEqual([1, 2, 3])
|
|
90
|
-
|
|
91
|
-
await field.reset()
|
|
92
|
-
expect(field.value).toEqual([1, 2])
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
test('非数组值时增删操作容错', async () => {
|
|
96
|
-
const form = attach(createForm())
|
|
97
|
-
const field = attach(form.createArrayField({ name: 'array', value: [1, 2] })!)
|
|
98
|
-
|
|
99
|
-
form.setValuesIn('array', 'not-array')
|
|
100
|
-
expect(field.pop()).toBeUndefined()
|
|
101
|
-
expect(field.remove(0)).toBeUndefined()
|
|
102
|
-
expect(field.shift()).toBeUndefined()
|
|
103
|
-
expect(field.move(0, 1)).toBeUndefined()
|
|
104
|
-
expect(field.moveUp(0)).toBeUndefined()
|
|
105
|
-
expect(field.moveDown(0)).toBeUndefined()
|
|
106
|
-
expect(form.values.array).toEqual('not-array')
|
|
107
|
-
|
|
108
|
-
// push/unshift 会把非数组值重置为空数组后再写入
|
|
109
|
-
await field.push(3)
|
|
110
|
-
expect(field.value).toEqual([3])
|
|
111
|
-
|
|
112
|
-
form.setValuesIn('array', 'not-array')
|
|
113
|
-
await field.unshift(0)
|
|
114
|
-
expect(field.value).toEqual([0])
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
test('insert 空元素与同索引移动不生效', async () => {
|
|
118
|
-
const form = attach(createForm())
|
|
119
|
-
const field = attach(form.createArrayField({ name: 'array', value: [1, 2] })!)
|
|
120
|
-
|
|
121
|
-
expect(field.insert(0)).toBeUndefined()
|
|
122
|
-
expect(field.value).toEqual([1, 2])
|
|
123
|
-
|
|
124
|
-
expect(field.move(1, 1)).toBeUndefined()
|
|
125
|
-
expect(field.value).toEqual([1, 2])
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
test('moveUp/moveDown 越界时环绕移动', async () => {
|
|
129
|
-
const form = attach(createForm())
|
|
130
|
-
const field = attach(form.createArrayField({ name: 'array', value: [1, 2, 3] })!)
|
|
131
|
-
|
|
132
|
-
await field.moveUp(0)
|
|
133
|
-
expect(field.value).toEqual([2, 3, 1])
|
|
134
|
-
|
|
135
|
-
await field.moveDown(2)
|
|
136
|
-
expect(field.value).toEqual([1, 2, 3])
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
test('move 会迁移子字段状态', async () => {
|
|
140
|
-
const form = attach(createForm())
|
|
141
|
-
const field = attach(form.createArrayField({ name: 'array', value: [{}, {}] })!)
|
|
142
|
-
const first = attach(form.createField({ name: '0', basePath: 'array' })!)
|
|
143
|
-
const second = attach(form.createField({ name: '1', basePath: 'array' })!)
|
|
144
|
-
first.setTitle('first')
|
|
145
|
-
second.setTitle('second')
|
|
146
|
-
|
|
147
|
-
await field.move(0, 1)
|
|
148
|
-
expect(form.fields['array.0']).toBe(second)
|
|
149
|
-
expect(form.fields['array.1']).toBe(first)
|
|
150
|
-
expect(form.fields['array.0'].title).toEqual('second')
|
|
151
|
-
expect(form.fields['array.1'].title).toEqual('first')
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
test('长度收缩自动清理子字段', () => {
|
|
155
|
-
const form = attach(createForm())
|
|
156
|
-
const field = attach(form.createArrayField({ name: 'array', value: [1, 2, 3] })!)
|
|
157
|
-
attach(form.createField({ name: '0', basePath: 'array' }))
|
|
158
|
-
attach(form.createField({ name: '1', basePath: 'array' }))
|
|
159
|
-
attach(form.createField({ name: '2', basePath: 'array' }))
|
|
160
|
-
|
|
161
|
-
field.value = [1]
|
|
162
|
-
expect(form.fields['array.0']).not.toBeUndefined()
|
|
163
|
-
expect(form.fields['array.1']).toBeUndefined()
|
|
164
|
-
expect(form.fields['array.2']).toBeUndefined()
|
|
165
|
-
|
|
166
|
-
field.value = []
|
|
167
|
-
expect(form.fields['array.0']).toBeUndefined()
|
|
168
|
-
})
|