@ilokesto/form 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/README.ko.md +1276 -0
- package/README.md +1277 -0
- package/dist/FormStateSummary-YyGT9nfY.js +124 -0
- package/dist/adapters/FormStateSummary.d.ts +12 -0
- package/dist/adapters/dom/FieldValue.d.ts +2 -0
- package/dist/adapters/dom/RegisterBinding.d.ts +14 -0
- package/dist/adapters/dom/index.d.ts +3 -0
- package/dist/adapters/dom/types.d.ts +22 -0
- package/dist/core/array/ArrayItemReorder.d.ts +6 -0
- package/dist/core/array/ArrayKeyGenerator.d.ts +4 -0
- package/dist/core/array/FormArrayController.d.ts +21 -0
- package/dist/core/array/FormArrayFactory.d.ts +8 -0
- package/dist/core/array/FormArrayMutationPlanner.d.ts +14 -0
- package/dist/core/array/FormArrayPath.d.ts +6 -0
- package/dist/core/array/FormArrayRebaser.d.ts +7 -0
- package/dist/core/array/index.d.ts +1 -0
- package/dist/core/form/CreateForm.d.ts +23 -0
- package/dist/core/form/FormFieldCommands.d.ts +15 -0
- package/dist/core/form/FormSubmitter.d.ts +9 -0
- package/dist/core/form/index.d.ts +1 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/path/FormPath.d.ts +9 -0
- package/dist/core/path/index.d.ts +1 -0
- package/dist/core/state/FieldStateFactory.d.ts +6 -0
- package/dist/core/state/FormStateInitializer.d.ts +7 -0
- package/dist/core/state/FormStateReader.d.ts +11 -0
- package/dist/core/state/FormStateStore.d.ts +23 -0
- package/dist/core/state/FormStateWriter.d.ts +16 -0
- package/dist/core/state/index.d.ts +1 -0
- package/dist/core/types.d.ts +96 -0
- package/dist/core/validation/StandardSchemaValidator.d.ts +14 -0
- package/dist/core/validation/ValidationEngine.d.ts +25 -0
- package/dist/core/validation/index.d.ts +1 -0
- package/dist/core/value/ValueHelper.d.ts +7 -0
- package/dist/core/value/index.d.ts +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1245 -0
- package/dist/react/RegisterBinding.d.ts +5 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +83 -0
- package/dist/react/types.d.ts +64 -0
- package/dist/react/useField.d.ts +3 -0
- package/dist/react/useFieldSchemaRegistration.d.ts +4 -0
- package/dist/react/useForm.d.ts +3 -0
- package/dist/react/useFormSnapshot.d.ts +2 -0
- package/dist/react/useFormState.d.ts +3 -0
- package/dist/react/useRegister.d.ts +6 -0
- package/dist/solid/RegisterBinding.d.ts +5 -0
- package/dist/solid/index.d.ts +2 -0
- package/dist/solid/index.js +110 -0
- package/dist/solid/types.d.ts +68 -0
- package/dist/solid/useField.d.ts +3 -0
- package/dist/solid/useFieldSchemaRegistration.d.ts +3 -0
- package/dist/solid/useForm.d.ts +3 -0
- package/dist/solid/useFormSnapshot.d.ts +3 -0
- package/dist/solid/useFormState.d.ts +3 -0
- package/dist/solid/useRegister.d.ts +6 -0
- package/dist/svelte/RegisterAction.d.ts +4 -0
- package/dist/svelte/index.d.ts +2 -0
- package/dist/svelte/index.js +107 -0
- package/dist/svelte/types.d.ts +12 -0
- package/dist/svelte/useForm.d.ts +3 -0
- package/dist/svelte/useFormState.d.ts +4 -0
- package/dist/vue/RegisterBinding.d.ts +5 -0
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.js +110 -0
- package/dist/vue/types.d.ts +68 -0
- package/dist/vue/useField.d.ts +3 -0
- package/dist/vue/useFieldSchemaRegistration.d.ts +3 -0
- package/dist/vue/useForm.d.ts +3 -0
- package/dist/vue/useFormSnapshot.d.ts +3 -0
- package/dist/vue/useFormState.d.ts +3 -0
- package/dist/vue/useRegister.d.ts +6 -0
- package/package.json +93 -0
package/README.ko.md
ADDED
|
@@ -0,0 +1,1276 @@
|
|
|
1
|
+
# @ilokesto/form
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | 한국어
|
|
4
|
+
|
|
5
|
+
`@ilokesto/form`은 프레임워크에 의존하지 않는 form state core다. 렌더링과 이벤트 바인딩은 framework adapter에 맡기고, core는 form values, field metadata, validation errors, submit attempts, array item keys를 하나의 정규화된 store 안에서 관리한다.
|
|
6
|
+
|
|
7
|
+
이 패키지는 다섯 가지 아이디어를 중심으로 설계되어 있다.
|
|
8
|
+
|
|
9
|
+
1. **프레임워크 독립성**: core는 plain TypeScript class인 `CreateForm`을 노출하고 React, Vue, Svelte, Solid, DOM API를 import하지 않는다.
|
|
10
|
+
2. **dot path가 아닌 tuple path**: `"user.name"` 같은 문자열은 literal field name이고, nested path는 `["user", "name"]` 같은 tuple로 표현한다.
|
|
11
|
+
3. **정규화된 field state**: nested values는 leaf `FieldState` record로 분리되어 저장되고, `getValues()`를 호출할 때 다시 복원된다.
|
|
12
|
+
4. **Standard Schema validation**: core는 특정 schema library가 아니라 Standard Schema v1의 `~standard.validate` 계약에만 의존한다.
|
|
13
|
+
5. **Array rebasing**: array item이 move, swap, insert, remove될 때 `errors`, `touched`, `dirty`, `modified` 같은 child field metadata가 item과 함께 이동한다.
|
|
14
|
+
|
|
15
|
+
## Table of contents
|
|
16
|
+
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Quick start](#quick-start)
|
|
19
|
+
- [React adapter](#react-adapter)
|
|
20
|
+
- [Vue adapter](#vue-adapter)
|
|
21
|
+
- [Solid adapter](#solid-adapter)
|
|
22
|
+
- [Svelte adapter](#svelte-adapter)
|
|
23
|
+
- [Core concepts](#core-concepts)
|
|
24
|
+
- [API guide](#api-guide)
|
|
25
|
+
- [Runtime flows](#runtime-flows)
|
|
26
|
+
- [Internal architecture](#internal-architecture)
|
|
27
|
+
- [Core walkthrough](#core-walkthrough)
|
|
28
|
+
- [Design decisions](#design-decisions)
|
|
29
|
+
- [Testing and development](#testing-and-development)
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
현재 이 repository의 `package.json`은 package를 private으로 표시한다. workspace에서는 package manager 또는 workspace protocol을 통해 추가하면 된다. package가 publish되거나 link된 상태에서 import surface는 다음과 같다.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { CreateForm } from '@ilokesto/form';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Local development commands:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
pnpm install
|
|
43
|
+
pnpm build
|
|
44
|
+
pnpm typecheck
|
|
45
|
+
pnpm test
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
이 package는 ESM JavaScript와 TypeScript declaration을 `dist/`로 emit한다.
|
|
49
|
+
|
|
50
|
+
## Quick start
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { CreateForm } from '@ilokesto/form';
|
|
54
|
+
|
|
55
|
+
type LoginValues = {
|
|
56
|
+
email: string;
|
|
57
|
+
profile: {
|
|
58
|
+
name: string;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const form = new CreateForm<LoginValues>({
|
|
63
|
+
initialValues: {
|
|
64
|
+
email: '',
|
|
65
|
+
profile: {
|
|
66
|
+
name: 'Ada',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
form.setValue('email', 'ada@example.com', { source: 'user' });
|
|
72
|
+
form.setValue(['profile', 'name'], 'Grace', { source: 'user' });
|
|
73
|
+
|
|
74
|
+
console.log(form.getFieldState('email'));
|
|
75
|
+
console.log(form.getValues());
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
중요한 path rule:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
form.setValue(['profile', 'name'], 'Grace');
|
|
82
|
+
form.setValue('profile.name', 'literal field');
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
이 둘은 서로 다른 field다. `['profile', 'name']`은 nested `profile.name` value를 의미한다. `'profile.name'`은 실제 key에 dot이 들어간 top-level field를 의미한다.
|
|
86
|
+
|
|
87
|
+
## React adapter
|
|
88
|
+
|
|
89
|
+
React binding은 `./react` subpath로 노출된다. 그래서 package root는 framework-agnostic core만 유지한다.
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import { CreateForm } from '@ilokesto/form';
|
|
93
|
+
import { useForm } from '@ilokesto/form/react';
|
|
94
|
+
|
|
95
|
+
const form = new CreateForm({
|
|
96
|
+
initialValues: {
|
|
97
|
+
email: '',
|
|
98
|
+
remember: false,
|
|
99
|
+
},
|
|
100
|
+
validateOn: ['blur', 'submit'],
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
function LoginForm() {
|
|
104
|
+
const {
|
|
105
|
+
useRegister,
|
|
106
|
+
useField,
|
|
107
|
+
useFormState,
|
|
108
|
+
} = useForm(form);
|
|
109
|
+
|
|
110
|
+
const email = useField({ name: 'email', schema: emailSchema });
|
|
111
|
+
const remember = useRegister({ name: 'remember', type: 'checkbox' });
|
|
112
|
+
const [role] = useRegister<HTMLSelectElement>([{ name: 'role' }]);
|
|
113
|
+
const state = useFormState();
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<form>
|
|
117
|
+
<input {...email.props} />
|
|
118
|
+
{email.errors.map(error => <p key={error.message}>{error.message}</p>)}
|
|
119
|
+
|
|
120
|
+
<label>
|
|
121
|
+
<input type="checkbox" {...remember} />
|
|
122
|
+
Remember me
|
|
123
|
+
</label>
|
|
124
|
+
|
|
125
|
+
<select {...role}>
|
|
126
|
+
<option value="user">User</option>
|
|
127
|
+
<option value="admin">Admin</option>
|
|
128
|
+
</select>
|
|
129
|
+
|
|
130
|
+
<button disabled={!state.isDirty || !state.isValid}>
|
|
131
|
+
Submit
|
|
132
|
+
</button>
|
|
133
|
+
</form>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
React adapter의 첫 버전 hook은 세 가지다. `useRegister`는 단일, 배열, rest-argument 등록을 모두 처리하도록 overload되어 있다.
|
|
139
|
+
|
|
140
|
+
| Hook | Purpose |
|
|
141
|
+
| --- | --- |
|
|
142
|
+
| `useRegister(options)` | 단일 field의 input binding props를 반환한다: `name`, `type`, `value`, `checked`, `onChange`, `onBlur`, `onFocus`. 기본 input `type`은 `text`다. |
|
|
143
|
+
| `useRegister<TElement>(options[])` | map-friendly rendering을 위해 여러 binding props를 입력 순서대로 반환한다. select/textarea에 spread할 때는 `HTMLSelectElement` 또는 `HTMLTextAreaElement` generic으로 좁힌다. |
|
|
144
|
+
| `useRegister(optionA, optionB)` | 여러 binding을 rest arguments로 받는다. 내부적으로 options array처럼 처리한다. |
|
|
145
|
+
| `useField(options)` | 한 field에 대해 `{ props, value, setValue, errors, dirty, touched }`를 반환한다. `field.register`는 의도적으로 노출하지 않는다. |
|
|
146
|
+
| `useFormState()` | `errors`, `dirtyFields`, `touchedFields`, `isDirty`, `isValid`, `submitCount` 같은 form 전체 aggregate state를 반환한다. |
|
|
147
|
+
|
|
148
|
+
Field-local schema는 `useRegister` 또는 `useField`에 전달할 수 있다. 해당 field에 대해서는 field-local schema가 form-level schema보다 우선한다.
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
const email = useField({
|
|
152
|
+
name: 'email',
|
|
153
|
+
schema: emailSchema,
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Event model은 DOM event 중심이다. Custom component도 DOM-compatible `value`, `checked`, `onChange`, `onBlur`, `onFocus` props를 그대로 전달한다면 `useRegister`를 사용할 수 있다.
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
## Vue adapter
|
|
161
|
+
|
|
162
|
+
Vue binding은 `./vue` subpath로 노출된다. React adapter와 같은 `useForm(form)` 형태를 쓰지만, Vue template의 `v-bind`에 바로 전달할 수 있도록 `onInput`, `onChange`, `onBlur`, `onFocus` handler를 가진 props를 반환한다.
|
|
163
|
+
|
|
164
|
+
```vue
|
|
165
|
+
<script setup lang="ts">
|
|
166
|
+
import { CreateForm } from '@ilokesto/form';
|
|
167
|
+
import { useForm } from '@ilokesto/form/vue';
|
|
168
|
+
|
|
169
|
+
const form = new CreateForm({
|
|
170
|
+
initialValues: {
|
|
171
|
+
email: '',
|
|
172
|
+
remember: false,
|
|
173
|
+
role: 'user',
|
|
174
|
+
},
|
|
175
|
+
validateOn: ['blur', 'submit'],
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const {
|
|
179
|
+
useRegister,
|
|
180
|
+
useField,
|
|
181
|
+
useFormState,
|
|
182
|
+
} = useForm(form);
|
|
183
|
+
|
|
184
|
+
const email = useField({ name: 'email', schema: emailSchema });
|
|
185
|
+
const remember = useRegister({ name: 'remember', type: 'checkbox' });
|
|
186
|
+
const [role] = useRegister<HTMLSelectElement>([{ name: 'role' }]);
|
|
187
|
+
const state = useFormState();
|
|
188
|
+
</script>
|
|
189
|
+
|
|
190
|
+
<template>
|
|
191
|
+
<form>
|
|
192
|
+
<input v-bind="email.props" />
|
|
193
|
+
<p v-for="error in email.errors" :key="error.message">
|
|
194
|
+
{{ error.message }}
|
|
195
|
+
</p>
|
|
196
|
+
|
|
197
|
+
<label>
|
|
198
|
+
<input type="checkbox" v-bind="remember" />
|
|
199
|
+
Remember me
|
|
200
|
+
</label>
|
|
201
|
+
|
|
202
|
+
<select v-bind="role">
|
|
203
|
+
<option value="user">User</option>
|
|
204
|
+
<option value="admin">Admin</option>
|
|
205
|
+
</select>
|
|
206
|
+
|
|
207
|
+
<button :disabled="!state.isDirty || !state.isValid">
|
|
208
|
+
Submit
|
|
209
|
+
</button>
|
|
210
|
+
</form>
|
|
211
|
+
</template>
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Vue adapter도 같은 세 가지 개념을 노출한다.
|
|
215
|
+
|
|
216
|
+
| Composable | Purpose |
|
|
217
|
+
| --- | --- |
|
|
218
|
+
| `useRegister(options)` | 하나의 input 중심 `v-bind` binding object를 반환한다. `type`이 포함되고 기본값은 `text`다. Text input은 `input` event에서, checkbox/radio는 `change` event에서 값을 갱신한다. select/textarea 타입은 generic으로 좁힌다. |
|
|
219
|
+
| `useRegister(options[])` / `useRegister(optionA, optionB)` | map-friendly rendering을 위해 여러 binding object를 반환한다. |
|
|
220
|
+
| `useField(options)` | getter 기반 reactive read를 가진 `{ props, value, setValue, errors, dirty, touched }`를 반환한다. |
|
|
221
|
+
| `useFormState()` | `errors`, `dirtyFields`, `touchedFields`, `isDirty`, `isValid`, `submitCount` 같은 form 전체 aggregate getter를 반환한다. |
|
|
222
|
+
|
|
223
|
+
Field-local schema는 React와 같은 방식으로 동작하고 현재 Vue effect scope가 정리될 때 함께 cleanup된다.
|
|
224
|
+
|
|
225
|
+
## Solid adapter
|
|
226
|
+
|
|
227
|
+
Solid binding은 `./solid` subpath로 노출된다. React/Vue와 같은 `useForm(form)` 형태를 유지하되, Solid owner cleanup과 getter 기반 props를 사용한다.
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import { CreateForm } from '@ilokesto/form';
|
|
231
|
+
import { useForm } from '@ilokesto/form/solid';
|
|
232
|
+
|
|
233
|
+
const form = new CreateForm({
|
|
234
|
+
initialValues: {
|
|
235
|
+
email: '',
|
|
236
|
+
remember: false,
|
|
237
|
+
role: 'user',
|
|
238
|
+
},
|
|
239
|
+
validateOn: ['blur', 'submit'],
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
function LoginForm() {
|
|
243
|
+
const { useRegister, useField, useFormState } = useForm(form);
|
|
244
|
+
const email = useField({ name: 'email', schema: emailSchema });
|
|
245
|
+
const remember = useRegister({ name: 'remember', type: 'checkbox' });
|
|
246
|
+
const role = useRegister<HTMLSelectElement>({ name: 'role' });
|
|
247
|
+
const state = useFormState();
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
<form>
|
|
251
|
+
<input {...email.props} />
|
|
252
|
+
{email.errors.map(error => <p>{error.message}</p>)}
|
|
253
|
+
|
|
254
|
+
<input type="checkbox" {...remember} />
|
|
255
|
+
|
|
256
|
+
<select {...role}>
|
|
257
|
+
<option value="user">User</option>
|
|
258
|
+
<option value="admin">Admin</option>
|
|
259
|
+
</select>
|
|
260
|
+
|
|
261
|
+
<button disabled={!state.isDirty || !state.isValid}>Submit</button>
|
|
262
|
+
</form>
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Solid adapter는 Vue와 같은 의미의 `useRegister`, `useField`, `useFormState`를 제공한다. Text input은 `input` event에서, checkbox/radio/multiple select는 `change` event에서 갱신하고, field-local schema는 현재 Solid owner와 함께 dispose된다.
|
|
268
|
+
|
|
269
|
+
## Svelte adapter
|
|
270
|
+
|
|
271
|
+
Svelte binding은 `./svelte` subpath로 노출된다. Svelte는 hook-style rendering을 쓰지 않으므로 adapter는 `register` action과 Svelte readable form-state store를 제공한다.
|
|
272
|
+
|
|
273
|
+
```svelte
|
|
274
|
+
<script lang="ts">
|
|
275
|
+
import { CreateForm } from '@ilokesto/form';
|
|
276
|
+
import { useForm } from '@ilokesto/form/svelte';
|
|
277
|
+
|
|
278
|
+
const form = new CreateForm({
|
|
279
|
+
initialValues: {
|
|
280
|
+
email: '',
|
|
281
|
+
remember: false,
|
|
282
|
+
role: 'user',
|
|
283
|
+
},
|
|
284
|
+
validateOn: ['blur', 'submit'],
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const { register, useFormState } = useForm(form);
|
|
288
|
+
const state = useFormState();
|
|
289
|
+
</script>
|
|
290
|
+
|
|
291
|
+
<form>
|
|
292
|
+
<input use:register={{ name: 'email', schema: emailSchema }} />
|
|
293
|
+
<input type="checkbox" use:register={{ name: 'remember', type: 'checkbox' }} />
|
|
294
|
+
|
|
295
|
+
<select use:register={{ name: 'role' }}>
|
|
296
|
+
<option value="user">User</option>
|
|
297
|
+
<option value="admin">Admin</option>
|
|
298
|
+
</select>
|
|
299
|
+
|
|
300
|
+
<button disabled={!$state.isDirty || !$state.isValid}>Submit</button>
|
|
301
|
+
</form>
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Svelte action은 DOM synchronization을 직접 담당한다. DOM `name`을 설정하고, form state에서 `value`/`checked`를 sync하고, user change를 core form에 쓰며, action이 destroy될 때 field-local schema를 cleanup한다.
|
|
305
|
+
|
|
306
|
+
## Core concepts
|
|
307
|
+
|
|
308
|
+
### `FieldPathSegment`
|
|
309
|
+
|
|
310
|
+
하나의 path segment는 string object key 또는 numeric array index다.
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
type FieldPathSegment = string | number;
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
Examples:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
'user'
|
|
320
|
+
'name'
|
|
321
|
+
0
|
|
322
|
+
1
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### `FieldPath`
|
|
326
|
+
|
|
327
|
+
`FieldPath`는 form field path의 내부 tuple 표현이다.
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
type FieldPath = readonly FieldPathSegment[];
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Examples:
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
['email']
|
|
337
|
+
['user', 'name']
|
|
338
|
+
['items', 0, 'title']
|
|
339
|
+
[] // root value
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Root path는 primitive root value나 root-level schema error에 유용하다.
|
|
343
|
+
|
|
344
|
+
### `FieldPathInput`
|
|
345
|
+
|
|
346
|
+
Public API는 string field name 또는 tuple path를 받는다.
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
type FieldPathInput = string | FieldPath;
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
String은 dot path로 파싱되지 않는다. 그래서 nested path와 `.` 문자를 실제로 포함한 object key가 충돌하지 않는다.
|
|
353
|
+
|
|
354
|
+
### `PathKey`
|
|
355
|
+
|
|
356
|
+
`FormState.fields`와 `FormState.arrayKeys`는 string key가 필요하다. `FormPath`는 tuple path를 안정적인 string key로 변환한다.
|
|
357
|
+
|
|
358
|
+
| FieldPath | PathKey |
|
|
359
|
+
| --- | --- |
|
|
360
|
+
| `[]` | `$` |
|
|
361
|
+
| `['email']` | `["email"]` |
|
|
362
|
+
| `['user', 'name']` | `["user","name"]` |
|
|
363
|
+
| `['items', 0, 'title']` | `["items",0,"title"]` |
|
|
364
|
+
|
|
365
|
+
구현은 separator 기반 문자열 대신 JSON array string을 사용한다. 그래서 `"user.name"` 같은 literal field name도 안전하다.
|
|
366
|
+
|
|
367
|
+
### `FieldState`
|
|
368
|
+
|
|
369
|
+
`FieldState`는 하나의 leaf field가 가진 value와 metadata를 저장한다.
|
|
370
|
+
|
|
371
|
+
```ts
|
|
372
|
+
type FieldState<TValue = unknown> = {
|
|
373
|
+
value: TValue;
|
|
374
|
+
errors: FormError[];
|
|
375
|
+
touched: boolean;
|
|
376
|
+
dirty: boolean;
|
|
377
|
+
modified: boolean;
|
|
378
|
+
};
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
Flag의 의미:
|
|
382
|
+
|
|
383
|
+
- `touched`: field가 한 번 이상 blur되었다.
|
|
384
|
+
- `dirty`: 현재 value가 같은 path의 initial value와 `Object.is` 기준으로 다르다.
|
|
385
|
+
- `modified`: `source: 'user'` write로 field가 변경되었다.
|
|
386
|
+
- `errors`: validation 또는 manual assignment로 붙은 field errors.
|
|
387
|
+
|
|
388
|
+
### `FormState`
|
|
389
|
+
|
|
390
|
+
Internal snapshot은 정규화되어 있다.
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
type FormState<TValues> = {
|
|
394
|
+
initialValues: TValues;
|
|
395
|
+
fields: Record<PathKey, FieldState>;
|
|
396
|
+
submitCount: number;
|
|
397
|
+
arrayKeys: Record<PathKey, string[]>;
|
|
398
|
+
};
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
다음 input이 있을 때:
|
|
402
|
+
|
|
403
|
+
```ts
|
|
404
|
+
const form = new CreateForm({
|
|
405
|
+
initialValues: {
|
|
406
|
+
user: { name: 'Ada' },
|
|
407
|
+
items: [{ title: 'A' }, { title: 'B' }],
|
|
408
|
+
},
|
|
409
|
+
});
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
Core는 leaf field states와 array container keys를 따로 저장한다.
|
|
413
|
+
|
|
414
|
+
```ts
|
|
415
|
+
{
|
|
416
|
+
initialValues: {
|
|
417
|
+
user: { name: 'Ada' },
|
|
418
|
+
items: [{ title: 'A' }, { title: 'B' }],
|
|
419
|
+
},
|
|
420
|
+
fields: {
|
|
421
|
+
'["user","name"]': { value: 'Ada', errors: [], touched: false, dirty: false, modified: false },
|
|
422
|
+
'["items",0,"title"]': { value: 'A', errors: [], touched: false, dirty: false, modified: false },
|
|
423
|
+
'["items",1,"title"]': { value: 'B', errors: [], touched: false, dirty: false, modified: false },
|
|
424
|
+
},
|
|
425
|
+
submitCount: 0,
|
|
426
|
+
arrayKeys: {
|
|
427
|
+
'["items"]': ['initial-0', 'initial-1'],
|
|
428
|
+
},
|
|
429
|
+
}
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
`getValues()`는 `fields`와 `arrayKeys`에서 nested object를 복원한다.
|
|
433
|
+
|
|
434
|
+
### `FormError`
|
|
435
|
+
|
|
436
|
+
```ts
|
|
437
|
+
type FormError = {
|
|
438
|
+
type?: string;
|
|
439
|
+
message: string;
|
|
440
|
+
};
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
Schema error는 `type: 'standard_schema'`를 사용한다. `setErrors()`로 error를 직접 설정할 수도 있다.
|
|
444
|
+
|
|
445
|
+
### `ValidationTrigger`
|
|
446
|
+
|
|
447
|
+
```ts
|
|
448
|
+
type ValidationTrigger = 'change' | 'blur' | 'submit' | 'manual';
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
`validateOn`은 automatic validation을 제어한다. 생략하면 기본값은 `['submit']`이다. Manual validation은 `trigger()`로 언제든 실행할 수 있다.
|
|
452
|
+
|
|
453
|
+
### `StandardSchemaV1`
|
|
454
|
+
|
|
455
|
+
Core는 Standard Schema compatible object를 받는다.
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
const schema = {
|
|
459
|
+
'~standard': {
|
|
460
|
+
version: 1,
|
|
461
|
+
vendor: 'example',
|
|
462
|
+
validate(value) {
|
|
463
|
+
return { value };
|
|
464
|
+
},
|
|
465
|
+
},
|
|
466
|
+
};
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
실패 시 `validate()`는 `issues`를 반환한다. 각 issue path는 `FieldPath`로 변환되고, 다시 `PathKey`로 변환된 뒤 해당 `FieldState.errors`에 기록된다.
|
|
470
|
+
|
|
471
|
+
### Field-local schemas
|
|
472
|
+
|
|
473
|
+
Framework adapter는 single field에 대한 schema를 등록할 수 있다.
|
|
474
|
+
|
|
475
|
+
```ts
|
|
476
|
+
const cleanup = form.registerFieldSchema('email', {
|
|
477
|
+
schema: emailSchema,
|
|
478
|
+
});
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
Field-local schema가 있으면 해당 field는 form-level schema 대신 그 schema를 사용한다.
|
|
482
|
+
|
|
483
|
+
```txt
|
|
484
|
+
field-local schema > form-level schema
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
Cleanup function은 자신이 등록한 schema가 여전히 해당 field의 최신 registration일 때만 제거한다.
|
|
488
|
+
|
|
489
|
+
### `ArrayKeys`
|
|
490
|
+
|
|
491
|
+
Array item identity는 array values와 별도로 저장된다.
|
|
492
|
+
|
|
493
|
+
- `initialValues`에서 온 item은 deterministic key를 받는다: `initial-0`, `initial-1`, ...
|
|
494
|
+
- Runtime insertion은 generated key를 받는다: `item-1`, `item-2`, ...
|
|
495
|
+
- `move()`와 `swap()`은 value와 함께 key를 이동시킨다.
|
|
496
|
+
- `replace()`는 모든 새 item에 새 key를 만들고 old child metadata를 보존하지 않는다.
|
|
497
|
+
|
|
498
|
+
이 key들은 framework list rendering을 위한 것이다.
|
|
499
|
+
|
|
500
|
+
## API guide
|
|
501
|
+
|
|
502
|
+
### `new CreateForm(options)`
|
|
503
|
+
|
|
504
|
+
```ts
|
|
505
|
+
const form = new CreateForm({
|
|
506
|
+
initialValues,
|
|
507
|
+
schema,
|
|
508
|
+
schemaOptions,
|
|
509
|
+
validateOn,
|
|
510
|
+
});
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
Options:
|
|
514
|
+
|
|
515
|
+
- `initialValues`: required initial value tree.
|
|
516
|
+
- `schema`: optional Standard Schema v1 compatible schema.
|
|
517
|
+
- `schemaOptions`: optional Standard Schema validation options.
|
|
518
|
+
- `validateOn`: optional automatic validation triggers. Defaults to `['submit']`.
|
|
519
|
+
|
|
520
|
+
### `getState()`
|
|
521
|
+
|
|
522
|
+
현재 `FormState` snapshot을 반환한다.
|
|
523
|
+
|
|
524
|
+
```ts
|
|
525
|
+
const state = form.getState();
|
|
526
|
+
console.log(state.submitCount);
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
반환된 object는 read-only로 취급해야 한다.
|
|
530
|
+
|
|
531
|
+
### `subscribe(listener)`
|
|
532
|
+
|
|
533
|
+
Store change를 구독하고 unsubscribe function을 반환한다.
|
|
534
|
+
|
|
535
|
+
```ts
|
|
536
|
+
const unsubscribe = form.subscribe(() => {
|
|
537
|
+
console.log(form.getValues());
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
unsubscribe();
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
Framework adapter는 이 method를 사용해 core store와 reactive rendering을 연결한다.
|
|
544
|
+
|
|
545
|
+
### `registerFieldSchema(path, options)`
|
|
546
|
+
|
|
547
|
+
Field-local schema를 등록하고 cleanup function을 반환한다.
|
|
548
|
+
|
|
549
|
+
```ts
|
|
550
|
+
const cleanup = form.registerFieldSchema('email', {
|
|
551
|
+
schema: emailSchema,
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
cleanup();
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
이 API는 주로 framework adapter를 위한 것이다. Field-local schema는 `blur()`, `trigger()`, `submit()` 동안 해당 field에서 form-level schema보다 우선한다.
|
|
558
|
+
|
|
559
|
+
### `getFieldState(path)`
|
|
560
|
+
|
|
561
|
+
Path에 해당하는 `FieldState`를 반환한다. Field가 아직 존재하지 않아도 `undefined` 대신 default field state를 반환한다.
|
|
562
|
+
|
|
563
|
+
```ts
|
|
564
|
+
const email = form.getFieldState('email');
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
### `getValue(path)`
|
|
568
|
+
|
|
569
|
+
현재 field value만 반환한다.
|
|
570
|
+
|
|
571
|
+
```ts
|
|
572
|
+
const email = form.getValue('email');
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
### `getValues()`
|
|
576
|
+
|
|
577
|
+
전체 nested values object를 복원해 반환한다.
|
|
578
|
+
|
|
579
|
+
```ts
|
|
580
|
+
const values = form.getValues();
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
### `setValue(path, value, options?)`
|
|
584
|
+
|
|
585
|
+
하나의 field value를 쓴다.
|
|
586
|
+
|
|
587
|
+
```ts
|
|
588
|
+
form.setValue(['user', 'name'], 'Grace', {
|
|
589
|
+
source: 'user',
|
|
590
|
+
validate: true,
|
|
591
|
+
});
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
Effects:
|
|
595
|
+
|
|
596
|
+
1. Public path input을 internal tuple path로 변환한다.
|
|
597
|
+
2. 새 value를 해당 `FieldState`에 쓴다.
|
|
598
|
+
3. 같은 path의 `initialValues`와 비교해 `dirty`를 다시 계산한다.
|
|
599
|
+
4. `source === 'user'`일 때만 `modified`를 `true`로 만든다.
|
|
600
|
+
5. `options.validate`가 true이거나 `validateOn`에 `'change'`가 있으면 change validation을 시작한다.
|
|
601
|
+
|
|
602
|
+
`setValue()`는 `void`를 반환한다. Change validation은 async로 시작되며 method가 await하지 않는다.
|
|
603
|
+
|
|
604
|
+
### `blur(path)`
|
|
605
|
+
|
|
606
|
+
Field를 touched 처리하고 필요하면 validate한다.
|
|
607
|
+
|
|
608
|
+
```ts
|
|
609
|
+
const valid = await form.blur('email');
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
`validateOn`에 `'blur'`가 없으면 field를 touch한 뒤 `true`를 반환한다.
|
|
613
|
+
|
|
614
|
+
### `focus(path)`
|
|
615
|
+
|
|
616
|
+
현재는 no-op이다. Minimal core state가 focused field를 저장하지 않더라도 framework adapter가 stable command surface를 제공할 수 있도록 존재한다.
|
|
617
|
+
|
|
618
|
+
### `setErrors(path, errors)`
|
|
619
|
+
|
|
620
|
+
하나의 field error list를 교체한다.
|
|
621
|
+
|
|
622
|
+
```ts
|
|
623
|
+
form.setErrors('email', [{ message: 'Email is required' }]);
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
### `clearErrors(...paths)`
|
|
627
|
+
|
|
628
|
+
특정 field들의 errors를 비우거나, path를 넘기지 않으면 existing field 전체 errors를 비운다.
|
|
629
|
+
|
|
630
|
+
```ts
|
|
631
|
+
form.clearErrors('email');
|
|
632
|
+
form.clearErrors();
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
### `trigger(...paths)`
|
|
636
|
+
|
|
637
|
+
Manual validation을 실행한다.
|
|
638
|
+
|
|
639
|
+
```ts
|
|
640
|
+
await form.trigger('email');
|
|
641
|
+
await form.trigger();
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
Path가 있으면 full schema result에서 해당 field errors만 update한다. Path가 없으면 registered fields와 schema error keys 전체를 update한다.
|
|
645
|
+
|
|
646
|
+
### `array(path)`
|
|
647
|
+
|
|
648
|
+
Array field를 위한 `FormArray` controller를 반환한다.
|
|
649
|
+
|
|
650
|
+
```ts
|
|
651
|
+
const items = form.array('items');
|
|
652
|
+
|
|
653
|
+
items.push({ title: 'C' });
|
|
654
|
+
items.move(2, 0);
|
|
655
|
+
console.log(items.keys());
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
Array controller는 command가 실행될 때마다 store에서 최신 values와 keys를 읽는다.
|
|
659
|
+
|
|
660
|
+
### `reset(values?)`
|
|
661
|
+
|
|
662
|
+
Form을 initial state로 되돌린다.
|
|
663
|
+
|
|
664
|
+
```ts
|
|
665
|
+
form.reset();
|
|
666
|
+
form.reset({ email: 'new@example.com' });
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
Argument가 없으면 현재 `initialValues`를 재사용한다. Argument가 있으면 그 값이 새로운 `initialValues`가 된다.
|
|
670
|
+
|
|
671
|
+
### `submit(onValid, onInvalid?)`
|
|
672
|
+
|
|
673
|
+
`submitCount`를 증가시키고 form을 validate한 뒤 알맞은 callback을 호출한다.
|
|
674
|
+
|
|
675
|
+
```ts
|
|
676
|
+
const result = await form.submit(
|
|
677
|
+
values => values,
|
|
678
|
+
fields => {
|
|
679
|
+
console.log(fields);
|
|
680
|
+
},
|
|
681
|
+
);
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
Validation이 실패하면 `onInvalid`는 현재 `fields` object를 받고 `submit()`은 `undefined`를 반환한다. Validation이 성공하면 `onValid`는 복원된 values를 받는다.
|
|
685
|
+
|
|
686
|
+
### `FormArray.keys()`
|
|
687
|
+
|
|
688
|
+
현재 array items를 rendering하기 위한 stable keys를 반환한다.
|
|
689
|
+
|
|
690
|
+
```ts
|
|
691
|
+
const keys = form.array('items').keys();
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
### `FormArray.insert(index, value)`
|
|
695
|
+
|
|
696
|
+
Item을 삽입한다. Out-of-range index는 `0...length` 범위로 보정된다.
|
|
697
|
+
|
|
698
|
+
### `FormArray.push(value)`
|
|
699
|
+
|
|
700
|
+
Item을 끝에 추가한다.
|
|
701
|
+
|
|
702
|
+
### `FormArray.remove(index)`
|
|
703
|
+
|
|
704
|
+
Item을 제거한다. Invalid index는 무시된다.
|
|
705
|
+
|
|
706
|
+
### `FormArray.move(fromIndex, toIndex)`
|
|
707
|
+
|
|
708
|
+
Item 하나를 이동한다. Invalid index 또는 no-op move는 무시된다.
|
|
709
|
+
|
|
710
|
+
### `FormArray.swap(leftIndex, rightIndex)`
|
|
711
|
+
|
|
712
|
+
두 item을 교환한다. Invalid index 또는 같은 index는 무시된다.
|
|
713
|
+
|
|
714
|
+
### `FormArray.replace(values)`
|
|
715
|
+
|
|
716
|
+
Array 전체를 교체한다. 기존 item link를 의도적으로 끊기 때문에 child field metadata는 보존되지 않는다.
|
|
717
|
+
|
|
718
|
+
## Runtime flows
|
|
719
|
+
|
|
720
|
+
### Form creation flow
|
|
721
|
+
|
|
722
|
+
```txt
|
|
723
|
+
new CreateForm(options)
|
|
724
|
+
-> new FormStateStore(options.initialValues)
|
|
725
|
+
-> FormStateInitializer.initialize(initialValues)
|
|
726
|
+
-> fields for leaf values
|
|
727
|
+
-> arrayKeys for array containers
|
|
728
|
+
-> new ValidationEngine(store, options)
|
|
729
|
+
-> new FormFieldCommands(store, validation)
|
|
730
|
+
-> new FormArrayFactory(store)
|
|
731
|
+
-> new FormSubmitter(store, validation)
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
핵심은 모든 collaborator가 같은 `FormStateStore`를 공유한다는 점이다.
|
|
735
|
+
|
|
736
|
+
### Value write flow
|
|
737
|
+
|
|
738
|
+
```txt
|
|
739
|
+
form.setValue(path, value, options)
|
|
740
|
+
-> FormFieldCommands.setValue()
|
|
741
|
+
-> FormPath.toFieldPath(path)
|
|
742
|
+
-> FormStateStore.setValue()
|
|
743
|
+
-> FormStateWriter.setValue()
|
|
744
|
+
-> compute dirty from initialValues
|
|
745
|
+
-> optionally mark modified
|
|
746
|
+
-> optionally start change validation
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
`dirty`와 `modified`는 의도적으로 분리되어 있다. Programmatic write는 field를 dirty하게 만들 수 있지만 user-modified로 표시하지 않을 수 있다.
|
|
750
|
+
|
|
751
|
+
### Blur flow
|
|
752
|
+
|
|
753
|
+
```txt
|
|
754
|
+
form.blur(path)
|
|
755
|
+
-> FormFieldCommands.blur()
|
|
756
|
+
-> FormPath.pathInputToKey(path)
|
|
757
|
+
-> FormStateStore.touchField(key)
|
|
758
|
+
-> if validateOn includes 'blur': ValidationEngine.validateField(key, 'blur')
|
|
759
|
+
-> otherwise return true
|
|
760
|
+
```
|
|
761
|
+
|
|
762
|
+
### Manual validation flow
|
|
763
|
+
|
|
764
|
+
```txt
|
|
765
|
+
form.trigger(...paths)
|
|
766
|
+
-> no paths: validateRegisteredFields('manual')
|
|
767
|
+
-> with paths: validateFields(keys, 'manual')
|
|
768
|
+
-> StandardSchemaValidator.validate(values)
|
|
769
|
+
-> issue paths become PathKeys
|
|
770
|
+
-> selected field errors are replaced
|
|
771
|
+
```
|
|
772
|
+
|
|
773
|
+
Field-level validation도 full schema를 실행한 뒤 target field errors만 적용한다. 이렇게 하면 schema adapter가 단순하고 schema-library independent하게 유지된다.
|
|
774
|
+
|
|
775
|
+
### Submit flow
|
|
776
|
+
|
|
777
|
+
```txt
|
|
778
|
+
form.submit(onValid, onInvalid)
|
|
779
|
+
-> increment submitCount
|
|
780
|
+
-> validateRegisteredFields('submit')
|
|
781
|
+
-> invalid: onInvalid(fields), return undefined
|
|
782
|
+
-> valid: onValid(getValues())
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
`submitCount`는 successful submissions가 아니라 submit attempts를 기록한다.
|
|
786
|
+
|
|
787
|
+
### Array mutation flow
|
|
788
|
+
|
|
789
|
+
```txt
|
|
790
|
+
form.array('items').move(1, 0)
|
|
791
|
+
-> FormArrayController.move()
|
|
792
|
+
-> read current array values and keys
|
|
793
|
+
-> FormArrayMutationPlanner.move()
|
|
794
|
+
-> next values
|
|
795
|
+
-> next keys
|
|
796
|
+
-> previous-index to next-index mapper
|
|
797
|
+
-> FormArrayRebaser.rebase()
|
|
798
|
+
-> write next array into values
|
|
799
|
+
-> initialize a fresh FormState from next values
|
|
800
|
+
-> preserve non-array fields
|
|
801
|
+
-> move child metadata through the index mapper
|
|
802
|
+
-> write next array keys
|
|
803
|
+
```
|
|
804
|
+
|
|
805
|
+
이 흐름이 `items[1].name` metadata를 동일한 logical item에 붙인 채 그 item이 `items[0].name`으로 이동하게 만드는 핵심 기능이다.
|
|
806
|
+
|
|
807
|
+
## Internal architecture
|
|
808
|
+
|
|
809
|
+
```txt
|
|
810
|
+
src/index.ts
|
|
811
|
+
-> src/core/index.ts
|
|
812
|
+
-> form/CreateForm.ts
|
|
813
|
+
-> state/FormStateStore.ts
|
|
814
|
+
-> state/FormStateInitializer.ts
|
|
815
|
+
-> state/FormStateReader.ts
|
|
816
|
+
-> state/FormStateWriter.ts
|
|
817
|
+
-> form/FormFieldCommands.ts
|
|
818
|
+
-> validation/ValidationEngine.ts
|
|
819
|
+
-> validation/StandardSchemaValidator.ts
|
|
820
|
+
-> form/FormSubmitter.ts
|
|
821
|
+
-> array/FormArrayFactory.ts
|
|
822
|
+
-> array/FormArrayController.ts
|
|
823
|
+
-> array/FormArrayMutationPlanner.ts
|
|
824
|
+
-> array/FormArrayRebaser.ts
|
|
825
|
+
-> array/FormArrayPath.ts
|
|
826
|
+
-> array/ArrayItemReorder.ts
|
|
827
|
+
-> array/ArrayKeyGenerator.ts
|
|
828
|
+
-> path/FormPath.ts
|
|
829
|
+
-> value/ValueHelper.ts
|
|
830
|
+
-> types.ts
|
|
831
|
+
-> src/adapters/
|
|
832
|
+
-> dom/FieldValue.ts
|
|
833
|
+
-> dom/RegisterBinding.ts
|
|
834
|
+
-> FormStateSummary.ts
|
|
835
|
+
-> src/react/index.ts
|
|
836
|
+
-> shared adapter logic 위의 React hook adapter
|
|
837
|
+
-> src/vue/index.ts
|
|
838
|
+
-> shared adapter logic 위의 Vue composable adapter
|
|
839
|
+
-> src/solid/index.ts
|
|
840
|
+
-> shared adapter logic 위의 Solid helper adapter
|
|
841
|
+
-> src/svelte/index.ts
|
|
842
|
+
-> shared adapter logic 위의 Svelte action adapter
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
Responsibility summary:
|
|
846
|
+
|
|
847
|
+
| Area | Responsibility |
|
|
848
|
+
| --- | --- |
|
|
849
|
+
| `form/` | Public orchestration: field commands, submit flow, array controller access. |
|
|
850
|
+
| `state/` | Normalized state initialization, reading, writing, and store facade. |
|
|
851
|
+
| `path/` | Conversion between public path input, tuple paths, and string path keys. |
|
|
852
|
+
| `value/` | Immutable nested get/set and reconstruction of values from field states. |
|
|
853
|
+
| `validation/` | Standard Schema execution and error normalization. |
|
|
854
|
+
| `array/` | Array item key management, mutation planning, and child field metadata rebasing. |
|
|
855
|
+
| `adapters/` | 내부 shared adapter logic: DOM value extraction/binding, form-state aggregation. |
|
|
856
|
+
| `react/` | Public `Form` interface를 감싸는 React hook adapter. |
|
|
857
|
+
| `vue/` | Public `Form` interface를 감싸는 Vue composable adapter. |
|
|
858
|
+
| `solid/` | Public `Form` interface를 감싸는 Solid helper adapter. |
|
|
859
|
+
| `svelte/` | Public `Form` interface를 감싸는 Svelte action adapter. |
|
|
860
|
+
| `types.ts` | Public and internal TypeScript contracts. |
|
|
861
|
+
|
|
862
|
+
## Core walkthrough
|
|
863
|
+
|
|
864
|
+
### `src/index.ts`
|
|
865
|
+
|
|
866
|
+
Package root는 안정적인 framework-agnostic surface만 export한다.
|
|
867
|
+
|
|
868
|
+
```ts
|
|
869
|
+
export { CreateForm } from './core/index';
|
|
870
|
+
export type {
|
|
871
|
+
CreateFormOptions,
|
|
872
|
+
FieldPathInput,
|
|
873
|
+
Form,
|
|
874
|
+
FormError,
|
|
875
|
+
StandardSchemaV1,
|
|
876
|
+
} from './core/index';
|
|
877
|
+
```
|
|
878
|
+
|
|
879
|
+
Internal state와 command helper 타입은 package root에서 내보내지 않는다. 이 문서에서는 구현 설명을 위해 다루지만, consumer는 `Form`, `CreateForm`, field path, errors, Standard Schema contract를 통해 사용해야 한다.
|
|
880
|
+
|
|
881
|
+
### `src/core/index.ts`
|
|
882
|
+
|
|
883
|
+
이 파일은 `types.ts`의 public types와 `CreateForm`을 re-export한다. Package root와 implementation folders 사이의 boundary다.
|
|
884
|
+
|
|
885
|
+
### `src/core/types.ts`
|
|
886
|
+
|
|
887
|
+
`types.ts`는 core vocabulary를 정의한다.
|
|
888
|
+
|
|
889
|
+
- public path type: `FieldPathInput`; internal path concepts: `FieldPathSegment`, `FieldPath`, `PathKey`
|
|
890
|
+
- exported validation types: `FormError`, `StandardSchemaV1`; internal validation trigger type: `ValidationTrigger`
|
|
891
|
+
- internal state types: `FieldState`, `ArrayKeys`, `FormState`
|
|
892
|
+
- public API types: `CreateFormOptions`, `Form`; internal command helper types: `FormArray`, `SetValueOptions`
|
|
893
|
+
|
|
894
|
+
이 파일에서 가장 중요한 설계는 public string path가 literal field name이라는 점이다. Nested field에는 tuple path가 필요하다.
|
|
895
|
+
|
|
896
|
+
### `src/core/form/CreateForm.ts`
|
|
897
|
+
|
|
898
|
+
`CreateForm`은 top-level controller이자 main public class다.
|
|
899
|
+
|
|
900
|
+
네 collaborator를 소유한다.
|
|
901
|
+
|
|
902
|
+
```txt
|
|
903
|
+
store -> state storage and state operations
|
|
904
|
+
fields -> setValue, blur, errors, trigger
|
|
905
|
+
arrays -> array(path) controller creation
|
|
906
|
+
submitter -> submitCount, validation, callbacks
|
|
907
|
+
```
|
|
908
|
+
|
|
909
|
+
Constructor는 하나의 `FormStateStore`를 만들고 모든 collaborator에 전달한다. 그래서 모든 command는 같은 snapshot 위에서 동작한다.
|
|
910
|
+
|
|
911
|
+
대부분의 method는 얇은 delegation이다.
|
|
912
|
+
|
|
913
|
+
- `getState()`, `getFieldState()`, `getValue()`, `getValues()`, `reset()`은 `FormStateStore`에 위임한다.
|
|
914
|
+
- `setValue()`, `blur()`, `focus()`, `setErrors()`, `clearErrors()`, `trigger()`는 `FormFieldCommands`에 위임한다.
|
|
915
|
+
- `array()`는 path를 tuple로 변환하고 `FormArrayFactory`에 controller를 요청한다.
|
|
916
|
+
- `submit()`은 `FormSubmitter`에 위임한다.
|
|
917
|
+
|
|
918
|
+
이 구조는 public API를 stable하게 유지하면서 implementation responsibilities를 분리한다.
|
|
919
|
+
|
|
920
|
+
### `src/core/form/FormFieldCommands.ts`
|
|
921
|
+
|
|
922
|
+
`FormFieldCommands`는 field-level operations를 묶는다.
|
|
923
|
+
|
|
924
|
+
`setValue()`:
|
|
925
|
+
|
|
926
|
+
1. `FormPath.toFieldPath()`로 path를 tuple로 변환한다.
|
|
927
|
+
2. `store.setValue()`로 value를 쓴다.
|
|
928
|
+
3. 결과 `PathKey`를 받는다.
|
|
929
|
+
4. `options.validate`가 true이거나 `validateOn`에 `'change'`가 있으면 change validation을 시작한다.
|
|
930
|
+
|
|
931
|
+
`blur()`:
|
|
932
|
+
|
|
933
|
+
1. Public path를 `PathKey`로 변환한다.
|
|
934
|
+
2. Field를 touched 처리한다.
|
|
935
|
+
3. 설정된 경우에만 blur validation을 실행한다.
|
|
936
|
+
|
|
937
|
+
`focus()`는 현재 no-op이다. Minimal core state가 focus를 추적하지 않기 때문이다. Adapter-level consistency를 위해 method는 유지한다.
|
|
938
|
+
|
|
939
|
+
`setErrors()`, `clearErrors()`, `trigger()`는 path를 key로 normalize하고 store 또는 validation engine에 위임한다.
|
|
940
|
+
|
|
941
|
+
### `src/core/form/FormSubmitter.ts`
|
|
942
|
+
|
|
943
|
+
`FormSubmitter`는 submit-specific sequencing을 분리한다.
|
|
944
|
+
|
|
945
|
+
```txt
|
|
946
|
+
increment submitCount
|
|
947
|
+
validate all registered fields
|
|
948
|
+
if invalid -> call onInvalid(fields) and return undefined
|
|
949
|
+
if valid -> call onValid(getValues())
|
|
950
|
+
```
|
|
951
|
+
|
|
952
|
+
Submit은 multi-step command이므로 `CreateForm`이 validation branching까지 책임지지 않게 별도 class로 분리되어 있다.
|
|
953
|
+
|
|
954
|
+
### `src/core/state/FieldStateFactory.ts`
|
|
955
|
+
|
|
956
|
+
`FieldStateFactory`는 default field state shape을 중앙화한다.
|
|
957
|
+
|
|
958
|
+
```ts
|
|
959
|
+
{
|
|
960
|
+
value: undefined,
|
|
961
|
+
errors: [],
|
|
962
|
+
touched: false,
|
|
963
|
+
dirty: false,
|
|
964
|
+
modified: false,
|
|
965
|
+
}
|
|
966
|
+
```
|
|
967
|
+
|
|
968
|
+
Shared mutable reference를 피하기 위해 매번 새 `errors` array를 만든다. 같은 factory는 initial leaf fields와 missing-field fallback에 모두 사용된다.
|
|
969
|
+
|
|
970
|
+
### `src/core/state/FormStateInitializer.ts`
|
|
971
|
+
|
|
972
|
+
`FormStateInitializer.initialize(initialValues)`는 nested values를 normalized `FormState`로 변환한다.
|
|
973
|
+
|
|
974
|
+
Rules:
|
|
975
|
+
|
|
976
|
+
1. Arrays는 leaf field가 아니라 containers다.
|
|
977
|
+
2. Array containers는 `initial-0`, `initial-1` 같은 `arrayKeys`를 받는다.
|
|
978
|
+
3. Array items는 recursive하게 방문된다.
|
|
979
|
+
4. Plain objects는 traverse된다.
|
|
980
|
+
5. Empty plain object는 더 내려갈 field가 없으므로 leaf value로 저장된다.
|
|
981
|
+
6. `Date`, class instance 같은 non-plain objects는 leaf value로 취급된다.
|
|
982
|
+
7. Primitive와 `null`은 leaf value다.
|
|
983
|
+
|
|
984
|
+
그래서 `getState().fields`는 원래 nested object가 아니라 leaf paths를 가진다.
|
|
985
|
+
|
|
986
|
+
### `src/core/state/FormStateReader.ts`
|
|
987
|
+
|
|
988
|
+
`FormStateReader`는 read-only derived operations를 담당한다.
|
|
989
|
+
|
|
990
|
+
- `getKnownFieldPaths()`는 저장된 모든 `PathKey`를 tuple path로 되돌린다.
|
|
991
|
+
- `getFieldStateByKey()`는 existing field 또는 default `FieldState`를 반환한다.
|
|
992
|
+
- `getFieldState()`와 `getValue()`는 path-input convenience methods다.
|
|
993
|
+
- `getValues()`는 `ValueHelper.getValuesFromFields()`로 full nested value tree를 복원한다.
|
|
994
|
+
- `getValueAtPath()`는 먼저 values를 복원한 뒤 nested path를 읽는다.
|
|
995
|
+
|
|
996
|
+
Reader는 store를 직접 소유하지 않고 snapshot getter를 받기 때문에 항상 최신 state를 읽는다.
|
|
997
|
+
|
|
998
|
+
### `src/core/state/FormStateWriter.ts`
|
|
999
|
+
|
|
1000
|
+
`FormStateWriter`는 모든 state mutation을 `immer`로 수행한다.
|
|
1001
|
+
|
|
1002
|
+
`setValue()`:
|
|
1003
|
+
|
|
1004
|
+
- Tuple path를 `PathKey`로 변환한다.
|
|
1005
|
+
- 해당 tuple path의 initial value를 읽는다.
|
|
1006
|
+
- 가능하면 existing field metadata를 보존한다.
|
|
1007
|
+
- `value`를 교체한다.
|
|
1008
|
+
- `Object.is`로 `dirty`를 계산한다.
|
|
1009
|
+
- `source: 'user'`일 때만 `modified`를 설정한다.
|
|
1010
|
+
- Validation이 재사용할 수 있도록 `PathKey`를 반환한다.
|
|
1011
|
+
|
|
1012
|
+
Other methods:
|
|
1013
|
+
|
|
1014
|
+
- `touchField()`는 `touched: true`를 설정한다.
|
|
1015
|
+
- `setErrorsByKey()`는 error array를 교체한다.
|
|
1016
|
+
- `clearErrors()`는 target fields 또는 existing fields 전체 errors를 비운다.
|
|
1017
|
+
- `reset()`은 현재 또는 replacement initial values로 state를 다시 초기화한다.
|
|
1018
|
+
- `incrementSubmitCount()`는 attempts를 증가시킨다.
|
|
1019
|
+
- `replaceState()`는 array rebasing 같은 whole-state operation을 위한 escape hatch다.
|
|
1020
|
+
|
|
1021
|
+
### `src/core/state/FormStateStore.ts`
|
|
1022
|
+
|
|
1023
|
+
`FormStateStore`는 core의 다른 부분들이 사용하는 facade다.
|
|
1024
|
+
|
|
1025
|
+
소유하는 것:
|
|
1026
|
+
|
|
1027
|
+
- `@ilokesto/store`의 `Store<FormState<TValues>>`
|
|
1028
|
+
- `FormStateReader`
|
|
1029
|
+
- `FormStateWriter`
|
|
1030
|
+
|
|
1031
|
+
Collaborator들은 operation이 reader, writer, underlying store 중 어디에서 구현되는지 알 필요가 없다. Facade만 호출하면 된다.
|
|
1032
|
+
|
|
1033
|
+
### `src/core/path/FormPath.ts`
|
|
1034
|
+
|
|
1035
|
+
`FormPath`는 path conversion rules를 정의한다.
|
|
1036
|
+
|
|
1037
|
+
Key methods:
|
|
1038
|
+
|
|
1039
|
+
- `path(...segments)`: tuple path helper.
|
|
1040
|
+
- `toFieldPath(input)`: string은 `[input]`이 되고 tuple path는 그대로 통과한다.
|
|
1041
|
+
- `pathInputToKey(input)`: public path input을 `PathKey`로 변환한다.
|
|
1042
|
+
- `pathToKey(path)`: `[]`는 `$`, 나머지는 tuple을 JSON stringify한다.
|
|
1043
|
+
- `keyToPath(key)`: key를 tuple segments로 parse하고 validate한다.
|
|
1044
|
+
|
|
1045
|
+
JSON encoding이 path collision을 방지한다.
|
|
1046
|
+
|
|
1047
|
+
### `src/core/value/ValueHelper.ts`
|
|
1048
|
+
|
|
1049
|
+
`ValueHelper`는 immutable nested value operations를 제공한다.
|
|
1050
|
+
|
|
1051
|
+
`getValueAtPath(source, path)`는 object/array를 안전하게 walk한다. 중간 value가 nullish이거나 primitive이면 `undefined`를 반환한다.
|
|
1052
|
+
|
|
1053
|
+
`setValueAtPath(source, path, value)`는 new root value를 반환하고 path를 따라 container를 shallow-clone한다. Missing container는 next segment로 추론한다. Number면 array를 만들고 string이면 object를 만든다.
|
|
1054
|
+
|
|
1055
|
+
`getValuesFromFields(state, fieldPaths)`는 public values를 두 단계로 복원한다.
|
|
1056
|
+
|
|
1057
|
+
1. `arrayKeys`에서 empty array containers를 만든다.
|
|
1058
|
+
2. 모든 leaf field value를 tuple path 위치에 쓴다.
|
|
1059
|
+
|
|
1060
|
+
이 과정을 통해 normalized `fields`가 다시 nested object가 된다.
|
|
1061
|
+
|
|
1062
|
+
### `src/core/validation/ValidationEngine.ts`
|
|
1063
|
+
|
|
1064
|
+
`ValidationEngine`은 validation triggers와 store writes를 조율한다.
|
|
1065
|
+
|
|
1066
|
+
- Default `validateOn`은 `['submit']`이다.
|
|
1067
|
+
- Schema가 없으면 validation은 error 없이 성공한다.
|
|
1068
|
+
- `validateField(key, trigger)`는 full schema를 실행하고 해당 field errors만 적용한다.
|
|
1069
|
+
- `validateFields(keys, trigger)`는 full schema를 실행하고 selected fields errors만 적용한다.
|
|
1070
|
+
- `validateRegisteredFields(trigger)`는 full schema를 실행하고 current fields와 schema error keys 전체를 update한다.
|
|
1071
|
+
|
|
1072
|
+
Engine은 schema를 호출할 때 trigger value를 전달하지 않는다. Trigger는 engine이 언제 실행될지를 제어하지 schema API를 바꾸지 않는다.
|
|
1073
|
+
|
|
1074
|
+
### `src/core/validation/StandardSchemaValidator.ts`
|
|
1075
|
+
|
|
1076
|
+
`StandardSchemaValidator`는 Standard Schema result를 core errors로 adapt한다.
|
|
1077
|
+
|
|
1078
|
+
Success:
|
|
1079
|
+
|
|
1080
|
+
```ts
|
|
1081
|
+
{ valid: true, errorsByKey: {} }
|
|
1082
|
+
```
|
|
1083
|
+
|
|
1084
|
+
Failure:
|
|
1085
|
+
|
|
1086
|
+
```ts
|
|
1087
|
+
{
|
|
1088
|
+
valid: false,
|
|
1089
|
+
errorsByKey: {
|
|
1090
|
+
'["email"]': [{ type: 'standard_schema', message: 'Email is invalid' }],
|
|
1091
|
+
},
|
|
1092
|
+
}
|
|
1093
|
+
```
|
|
1094
|
+
|
|
1095
|
+
Issue path conversion rules:
|
|
1096
|
+
|
|
1097
|
+
- Missing path 또는 empty path는 root path `[]`와 key `$`가 된다.
|
|
1098
|
+
- String과 number path segment는 그대로 보존된다.
|
|
1099
|
+
- Object path segment는 `.key` property를 사용한다.
|
|
1100
|
+
- Symbol 같은 unsupported key는 root error로 fallback된다.
|
|
1101
|
+
|
|
1102
|
+
### `src/core/array/ArrayKeyGenerator.ts`
|
|
1103
|
+
|
|
1104
|
+
`ArrayKeyGenerator`는 runtime item keys를 만든다.
|
|
1105
|
+
|
|
1106
|
+
```txt
|
|
1107
|
+
item-1
|
|
1108
|
+
item-2
|
|
1109
|
+
item-3
|
|
1110
|
+
```
|
|
1111
|
+
|
|
1112
|
+
하나의 `FormArrayFactory`는 form instance마다 하나의 generator를 공유한다. 이렇게 하면 여러 controller 사이에서 accidental key collision 가능성을 줄인다.
|
|
1113
|
+
|
|
1114
|
+
### `src/core/array/ArrayItemReorder.ts`
|
|
1115
|
+
|
|
1116
|
+
`ArrayItemReorder`는 array order operations를 위한 pure helper다.
|
|
1117
|
+
|
|
1118
|
+
- `moveItem(items, from, to)`는 reordered copy를 반환한다.
|
|
1119
|
+
- `swapItems(items, left, right)`는 swapped copy를 반환한다.
|
|
1120
|
+
- `createIndexMapper(previousLength, nextOrder)`는 previous index를 next index로 mapping하는 function을 반환한다.
|
|
1121
|
+
|
|
1122
|
+
`nextOrder`는 "각 next position에는 어떤 previous index가 있는가?"로 표현된다. New item은 `-1`을 사용하고 removed item은 아예 등장하지 않는다.
|
|
1123
|
+
|
|
1124
|
+
### `src/core/array/FormArrayMutationPlanner.ts`
|
|
1125
|
+
|
|
1126
|
+
`FormArrayMutationPlanner`는 store를 몰라도 array mutations를 계산한다.
|
|
1127
|
+
|
|
1128
|
+
각 mutation은 다음 형태를 반환한다.
|
|
1129
|
+
|
|
1130
|
+
```ts
|
|
1131
|
+
type FormArrayMutation = {
|
|
1132
|
+
values: readonly unknown[];
|
|
1133
|
+
keys: readonly string[];
|
|
1134
|
+
mapPreviousIndex: (index: number) => number | undefined;
|
|
1135
|
+
};
|
|
1136
|
+
```
|
|
1137
|
+
|
|
1138
|
+
Command behavior:
|
|
1139
|
+
|
|
1140
|
+
- `insert()`는 target index를 보정하고 새 value/key를 삽입한다. New item은 `-1`에서 온 것으로 처리되어 previous metadata가 붙지 않는다.
|
|
1141
|
+
- `push()`는 value/key를 끝에 추가한다.
|
|
1142
|
+
- `remove()`는 value/key를 제거하고 removed index는 `undefined`로 map한다.
|
|
1143
|
+
- `move()`는 같은 rule로 values와 keys를 이동한다.
|
|
1144
|
+
- `swap()`은 같은 rule로 values와 keys를 교환한다.
|
|
1145
|
+
- `replace()`는 모두 새 values/keys를 반환하고 모든 previous index를 `undefined`로 map한다.
|
|
1146
|
+
|
|
1147
|
+
이 planner는 pure하기 때문에 store와 독립적으로 test하기 쉽다.
|
|
1148
|
+
|
|
1149
|
+
### `src/core/array/FormArrayPath.ts`
|
|
1150
|
+
|
|
1151
|
+
`FormArrayPath`는 rebasing 중 필요한 path helpers를 담는다.
|
|
1152
|
+
|
|
1153
|
+
- `hasPathPrefix(fieldPath, prefix)`는 tuple prefix equality를 확인한다.
|
|
1154
|
+
- `isArrayChildPath(fieldPath, arrayPath)`는 `['items']` 아래의 `['items', 0, 'name']`처럼 field가 array item 아래에 있는지 확인한다.
|
|
1155
|
+
- `replaceArrayIndex(fieldPath, arrayPath, nextIndex)`는 item index가 교체된 새 child path를 만든다.
|
|
1156
|
+
|
|
1157
|
+
### `src/core/array/FormArrayController.ts`
|
|
1158
|
+
|
|
1159
|
+
`FormArrayController`는 `form.array(path)`가 반환하는 public command object다.
|
|
1160
|
+
|
|
1161
|
+
보유하는 것:
|
|
1162
|
+
|
|
1163
|
+
- shared `FormStateStore`
|
|
1164
|
+
- shared `ArrayKeyGenerator`
|
|
1165
|
+
- controlled array `FieldPath`
|
|
1166
|
+
- `FormArrayMutationPlanner`
|
|
1167
|
+
|
|
1168
|
+
각 command는 같은 pattern을 따른다.
|
|
1169
|
+
|
|
1170
|
+
```txt
|
|
1171
|
+
read current array value
|
|
1172
|
+
read current keys
|
|
1173
|
+
ask planner for next values, keys, and index mapper
|
|
1174
|
+
if mutation exists, rebase the whole FormState
|
|
1175
|
+
```
|
|
1176
|
+
|
|
1177
|
+
`getArray()`는 array path의 현재 value를 읽는다. Value가 array가 아니면 empty array를 사용한다.
|
|
1178
|
+
|
|
1179
|
+
`getKeys()`는 stored `arrayKeys`를 읽는다. 없으면 current array length만큼 key를 만든다.
|
|
1180
|
+
|
|
1181
|
+
### `src/core/array/FormArrayFactory.ts`
|
|
1182
|
+
|
|
1183
|
+
`FormArrayFactory`는 `FormArrayController` instances를 만들고 form instance를 위한 하나의 `ArrayKeyGenerator`를 공유한다.
|
|
1184
|
+
|
|
1185
|
+
이 덕분에 `form.array('items')`를 반복 호출해서 fresh controller를 만들어도 key sequence가 reset되지 않는다.
|
|
1186
|
+
|
|
1187
|
+
### `src/core/array/FormArrayRebaser.ts`
|
|
1188
|
+
|
|
1189
|
+
`FormArrayRebaser`는 가장 중요한 array component다. Array mutation 뒤 values, field states, array keys, submit count를 정렬한다.
|
|
1190
|
+
|
|
1191
|
+
Detailed sequence:
|
|
1192
|
+
|
|
1193
|
+
1. Current store에서 known field paths를 읽는다.
|
|
1194
|
+
2. `store.getValues()`로 current values를 복원한다.
|
|
1195
|
+
3. `ValueHelper.setValueAtPath()`로 next array를 values에 쓴다.
|
|
1196
|
+
4. Next values에서 fresh `FormState`를 initialize한다.
|
|
1197
|
+
5. Freshly initialized fields에서 시작한다.
|
|
1198
|
+
6. Previous fields를 순회한다.
|
|
1199
|
+
7. Changed array 밖의 fields는 보존한다.
|
|
1200
|
+
8. Changed array 안의 child fields는 previous item index를 next item index로 mapping한다.
|
|
1201
|
+
9. Item이 아직 존재하면 child path의 index를 교체하고 metadata를 new field로 copy한다.
|
|
1202
|
+
10. Array path에 next `arrayKeys`를 쓴다.
|
|
1203
|
+
11. `submitCount`를 보존한다.
|
|
1204
|
+
|
|
1205
|
+
Rebased child field에 copy되는 것은 metadata뿐이다.
|
|
1206
|
+
|
|
1207
|
+
```txt
|
|
1208
|
+
errors
|
|
1209
|
+
touched
|
|
1210
|
+
dirty
|
|
1211
|
+
modified
|
|
1212
|
+
```
|
|
1213
|
+
|
|
1214
|
+
Value 자체는 next values로부터 freshly initialized state에서 온다. 그래서 stale value를 방지하면서 user interaction metadata는 보존한다.
|
|
1215
|
+
|
|
1216
|
+
## Design decisions
|
|
1217
|
+
|
|
1218
|
+
### Strings are literal paths
|
|
1219
|
+
|
|
1220
|
+
Dot-path parsing은 편하지만 모호하다. 이 core는 unambiguous tuple paths를 선택한다.
|
|
1221
|
+
|
|
1222
|
+
```ts
|
|
1223
|
+
'user.name' // one field named "user.name"
|
|
1224
|
+
['user', 'name'] // nested user.name field
|
|
1225
|
+
```
|
|
1226
|
+
|
|
1227
|
+
### State is normalized
|
|
1228
|
+
|
|
1229
|
+
Normalized fields는 whole nested value object를 mutate하지 않고도 개별 metadata를 update하기 쉽게 만든다. `ValueHelper`는 필요할 때 public value shape을 복원한다.
|
|
1230
|
+
|
|
1231
|
+
### Validation is schema-library independent
|
|
1232
|
+
|
|
1233
|
+
Core는 Standard Schema만 안다. 그래서 여러 validation library와 호환되고 특정 validator를 import하지 않는다.
|
|
1234
|
+
|
|
1235
|
+
### Array mutation is split into planning and rebasing
|
|
1236
|
+
|
|
1237
|
+
`FormArrayMutationPlanner`는 next array가 어떤 모습인지 결정한다. `FormArrayRebaser`는 전체 form state가 어떻게 따라가야 하는지 결정한다. 이 분리는 mutation math를 pure하게 만들고 store updates를 중앙화한다.
|
|
1238
|
+
|
|
1239
|
+
### `dirty` and `modified` are different
|
|
1240
|
+
|
|
1241
|
+
`dirty`는 current value와 initial value를 비교한다. `modified`는 user-originated writes를 추적한다. Programmatic update는 dirty일 수 있지만 user modified는 아닐 수 있다.
|
|
1242
|
+
|
|
1243
|
+
## Testing and development
|
|
1244
|
+
|
|
1245
|
+
### Commands
|
|
1246
|
+
|
|
1247
|
+
```sh
|
|
1248
|
+
pnpm build
|
|
1249
|
+
pnpm typecheck
|
|
1250
|
+
pnpm test
|
|
1251
|
+
```
|
|
1252
|
+
|
|
1253
|
+
`pnpm build`는 TypeScript로 declaration files를 emit하고, NodeNext 호환성을 위해 declaration-file relative specifier만 보정한 뒤, Vite로 ESM JavaScript를 bundle한다. 그래서 source import는 extensionless로 유지하면서도 `dist/index.js`, `dist/react/index.js`, `dist/vue/index.js`, `dist/solid/index.js`, `dist/svelte/index.js` 같은 adapter subpath는 ESM runtime에서 직접 import할 수 있다.
|
|
1254
|
+
|
|
1255
|
+
`pnpm test`는 Vitest suite를 실행한다.
|
|
1256
|
+
|
|
1257
|
+
### Current test coverage themes
|
|
1258
|
+
|
|
1259
|
+
Existing tests는 다음을 cover한다.
|
|
1260
|
+
|
|
1261
|
+
1. Tuple paths와 literal string names의 차이.
|
|
1262
|
+
2. Blur, manual trigger, submit에서의 Standard Schema validation.
|
|
1263
|
+
3. Move와 remove 시 array value/key/metadata rebasing.
|
|
1264
|
+
4. React adapter의 text input, textarea, checkbox, radio, select, `useField`, overloaded `useRegister`, `useFormState`, field-local schema precedence.
|
|
1265
|
+
5. Vue adapter의 text input, textarea, checkbox, radio, select, `useField`, overloaded `useRegister`, `useFormState`, field-local schema cleanup.
|
|
1266
|
+
6. Solid adapter의 text input, textarea, checkbox, radio, select, `useField`, overloaded `useRegister`, `useFormState`, field-local schema cleanup.
|
|
1267
|
+
7. Svelte register action의 text input, checkbox, radio, select, multiple select, readable `useFormState`, field-local schema cleanup.
|
|
1268
|
+
|
|
1269
|
+
### Suggested future documentation/tests
|
|
1270
|
+
|
|
1271
|
+
좋은 추가 항목:
|
|
1272
|
+
|
|
1273
|
+
- `insert()` index bounding test.
|
|
1274
|
+
- `replace()`가 child metadata를 의도적으로 drop하는지에 대한 test.
|
|
1275
|
+
- Root-level schema errors test.
|
|
1276
|
+
- Custom DOM-event-compatible component를 위한 adapter examples.
|