@ilokesto/form 1.0.0 → 1.0.2

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 CHANGED
@@ -60,7 +60,7 @@ type LoginValues = {
60
60
  };
61
61
 
62
62
  const form = new CreateForm<LoginValues>({
63
- initialValues: {
63
+ defaultValues: {
64
64
  email: '',
65
65
  profile: {
66
66
  name: 'Ada',
@@ -93,7 +93,7 @@ import { CreateForm } from '@ilokesto/form';
93
93
  import { useForm } from '@ilokesto/form/react';
94
94
 
95
95
  const form = new CreateForm({
96
- initialValues: {
96
+ defaultValues: {
97
97
  email: '',
98
98
  remember: false,
99
99
  },
@@ -135,6 +135,30 @@ function LoginForm() {
135
135
  }
136
136
  ```
137
137
 
138
+ Component가 form을 소유하는 경우 React adapter는 options로 form을 직접 만들 수도 있다.
139
+
140
+ ```tsx
141
+ const { form, useRegister, handleSubmit } = useForm({
142
+ defaultValues: {
143
+ email: '',
144
+ remember: false,
145
+ },
146
+ });
147
+ ```
148
+
149
+ `useForm(options)`에 전달한 options는 해당 component lifetime 동안 form instance를 만들 때 사용된다. React adapter에서는 외부 `values`도 받을 수 있다. `values` reference가 바뀌면 adapter가 `form.reset(values, resetOptions)`를 대신 호출한다. `useQuery` 같은 async data로 값을 hydrate할 때 사용자 편집값을 덮어쓰고 싶지 않다면 `keepDirtyValues`를 사용한다.
150
+
151
+ ```tsx
152
+ const { useRegister } = useForm({
153
+ defaultValues: emptyUser,
154
+ values: query.data,
155
+ resetOptions: {
156
+ keepDirtyValues: true,
157
+ keepErrors: true,
158
+ },
159
+ });
160
+ ```
161
+
138
162
  React adapter의 첫 버전 hook은 세 가지다. `useRegister`는 단일, 배열, rest-argument 등록을 모두 처리하도록 overload되어 있다.
139
163
 
140
164
  | Hook | Purpose |
@@ -167,7 +191,7 @@ import { CreateForm } from '@ilokesto/form';
167
191
  import { useForm } from '@ilokesto/form/vue';
168
192
 
169
193
  const form = new CreateForm({
170
- initialValues: {
194
+ defaultValues: {
171
195
  email: '',
172
196
  remember: false,
173
197
  role: 'user',
@@ -231,7 +255,7 @@ import { CreateForm } from '@ilokesto/form';
231
255
  import { useForm } from '@ilokesto/form/solid';
232
256
 
233
257
  const form = new CreateForm({
234
- initialValues: {
258
+ defaultValues: {
235
259
  email: '',
236
260
  remember: false,
237
261
  role: 'user',
@@ -276,7 +300,7 @@ Svelte binding은 `./svelte` subpath로 노출된다. Svelte는 hook-style rende
276
300
  import { useForm } from '@ilokesto/form/svelte';
277
301
 
278
302
  const form = new CreateForm({
279
- initialValues: {
303
+ defaultValues: {
280
304
  email: '',
281
305
  remember: false,
282
306
  role: 'user',
@@ -391,7 +415,7 @@ Internal snapshot은 정규화되어 있다.
391
415
 
392
416
  ```ts
393
417
  type FormState<TValues> = {
394
- initialValues: TValues;
418
+ defaultValues: TValues;
395
419
  fields: Record<PathKey, FieldState>;
396
420
  submitCount: number;
397
421
  arrayKeys: Record<PathKey, string[]>;
@@ -402,7 +426,7 @@ type FormState<TValues> = {
402
426
 
403
427
  ```ts
404
428
  const form = new CreateForm({
405
- initialValues: {
429
+ defaultValues: {
406
430
  user: { name: 'Ada' },
407
431
  items: [{ title: 'A' }, { title: 'B' }],
408
432
  },
@@ -413,7 +437,7 @@ Core는 leaf field states와 array container keys를 따로 저장한다.
413
437
 
414
438
  ```ts
415
439
  {
416
- initialValues: {
440
+ defaultValues: {
417
441
  user: { name: 'Ada' },
418
442
  items: [{ title: 'A' }, { title: 'B' }],
419
443
  },
@@ -490,7 +514,7 @@ Cleanup function은 자신이 등록한 schema가 여전히 해당 field의 최
490
514
 
491
515
  Array item identity는 array values와 별도로 저장된다.
492
516
 
493
- - `initialValues`에서 온 item은 deterministic key를 받는다: `initial-0`, `initial-1`, ...
517
+ - `defaultValues`에서 온 item은 deterministic key를 받는다: `initial-0`, `initial-1`, ...
494
518
  - Runtime insertion은 generated key를 받는다: `item-1`, `item-2`, ...
495
519
  - `move()`와 `swap()`은 value와 함께 key를 이동시킨다.
496
520
  - `replace()`는 모든 새 item에 새 key를 만들고 old child metadata를 보존하지 않는다.
@@ -503,7 +527,7 @@ Array item identity는 array values와 별도로 저장된다.
503
527
 
504
528
  ```ts
505
529
  const form = new CreateForm({
506
- initialValues,
530
+ defaultValues,
507
531
  schema,
508
532
  schemaOptions,
509
533
  validateOn,
@@ -512,7 +536,7 @@ const form = new CreateForm({
512
536
 
513
537
  Options:
514
538
 
515
- - `initialValues`: required initial value tree.
539
+ - `defaultValues`: required default value tree. reset/dirty 기준값으로도 사용된다.
516
540
  - `schema`: optional Standard Schema v1 compatible schema.
517
541
  - `schemaOptions`: optional Standard Schema validation options.
518
542
  - `validateOn`: optional automatic validation triggers. Defaults to `['submit']`.
@@ -595,7 +619,7 @@ Effects:
595
619
 
596
620
  1. Public path input을 internal tuple path로 변환한다.
597
621
  2. 새 value를 해당 `FieldState`에 쓴다.
598
- 3. 같은 path의 `initialValues`와 비교해 `dirty`를 다시 계산한다.
622
+ 3. 같은 path의 `defaultValues`와 비교해 `dirty`를 다시 계산한다.
599
623
  4. `source === 'user'`일 때만 `modified`를 `true`로 만든다.
600
624
  5. `options.validate`가 true이거나 `validateOn`에 `'change'`가 있으면 change validation을 시작한다.
601
625
 
@@ -657,16 +681,26 @@ console.log(items.keys());
657
681
 
658
682
  Array controller는 command가 실행될 때마다 store에서 최신 values와 keys를 읽는다.
659
683
 
660
- ### `reset(values?)`
684
+ ### `reset(values?, options?)`
661
685
 
662
686
  Form을 initial state로 되돌린다.
663
687
 
664
688
  ```ts
665
689
  form.reset();
666
690
  form.reset({ email: 'new@example.com' });
691
+ form.reset({ email: 'new@example.com' }, { keepDirtyValues: true });
667
692
  ```
668
693
 
669
- Argument가 없으면 현재 `initialValues`를 재사용한다. Argument가 있으면 그 값이 새로운 `initialValues`가 된다.
694
+ Argument가 없으면 현재 `defaultValues`를 재사용한다. Argument가 있으면 그 값이 새로운 `defaultValues`가 된다.
695
+
696
+ Reset options는 새 normalized value shape에도 살아남은 field path에 대해 일부 상태를 보존한다.
697
+
698
+ | Option | Behavior |
699
+ | --- | --- |
700
+ | `keepDirtyValues` | dirty field의 현재 value를 유지하고 새 `defaultValues` 기준으로 dirty를 다시 계산한다. |
701
+ | `keepErrors` | 살아남은 field path의 errors를 유지한다. |
702
+ | `keepTouched` | 살아남은 field path의 touched flag를 유지한다. |
703
+ | `keepSubmitState` | `submitCount`, `isSubmitting`, `isSubmitted`, `isSubmitSuccessful`을 유지한다. |
670
704
 
671
705
  ### `submit(onValid, onInvalid?)`
672
706
 
@@ -721,8 +755,8 @@ Array 전체를 교체한다. 기존 item link를 의도적으로 끊기 때문
721
755
 
722
756
  ```txt
723
757
  new CreateForm(options)
724
- -> new FormStateStore(options.initialValues)
725
- -> FormStateInitializer.initialize(initialValues)
758
+ -> new FormStateStore(options.defaultValues)
759
+ -> FormStateInitializer.initialize(defaultValues)
726
760
  -> fields for leaf values
727
761
  -> arrayKeys for array containers
728
762
  -> new ValidationEngine(store, options)
@@ -741,7 +775,7 @@ form.setValue(path, value, options)
741
775
  -> FormPath.toFieldPath(path)
742
776
  -> FormStateStore.setValue()
743
777
  -> FormStateWriter.setValue()
744
- -> compute dirty from initialValues
778
+ -> compute dirty from defaultValues
745
779
  -> optionally mark modified
746
780
  -> optionally start change validation
747
781
  ```
@@ -969,7 +1003,7 @@ Shared mutable reference를 피하기 위해 매번 새 `errors` array를 만든
969
1003
 
970
1004
  ### `src/core/state/FormStateInitializer.ts`
971
1005
 
972
- `FormStateInitializer.initialize(initialValues)`는 nested values를 normalized `FormState`로 변환한다.
1006
+ `FormStateInitializer.initialize(defaultValues)`는 nested values를 normalized `FormState`로 변환한다.
973
1007
 
974
1008
  Rules:
975
1009
 
package/README.md CHANGED
@@ -60,7 +60,7 @@ type LoginValues = {
60
60
  };
61
61
 
62
62
  const form = new CreateForm<LoginValues>({
63
- initialValues: {
63
+ defaultValues: {
64
64
  email: '',
65
65
  profile: {
66
66
  name: 'Ada',
@@ -93,7 +93,7 @@ import { CreateForm } from '@ilokesto/form';
93
93
  import { useForm } from '@ilokesto/form/react';
94
94
 
95
95
  const form = new CreateForm({
96
- initialValues: {
96
+ defaultValues: {
97
97
  email: '',
98
98
  remember: false,
99
99
  },
@@ -135,6 +135,32 @@ function LoginForm() {
135
135
  }
136
136
  ```
137
137
 
138
+ For component-owned forms, the React adapter can also create the form from options directly:
139
+
140
+ ```tsx
141
+ const { form, useRegister, handleSubmit } = useForm({
142
+ defaultValues: {
143
+ email: '',
144
+ remember: false,
145
+ },
146
+ });
147
+ ```
148
+
149
+ Options passed to `useForm(options)` are used only to create the form instance for that component lifetime. They are not reactive. When hydrating values from async data such as a query result, initialize with safe defaults and call `form.reset(nextValues)` explicitly when you want that data to become the new default baseline.
150
+
151
+ The React adapter also accepts reactive external `values`. When the `values` reference changes, the adapter calls `form.reset(values, resetOptions)` for you. This is useful for server/query data; use `keepDirtyValues` when refetches should not overwrite fields the user already edited.
152
+
153
+ ```tsx
154
+ const { useRegister } = useForm({
155
+ defaultValues: emptyUser,
156
+ values: query.data,
157
+ resetOptions: {
158
+ keepDirtyValues: true,
159
+ keepErrors: true,
160
+ },
161
+ });
162
+ ```
163
+
138
164
  The React adapter has three first-version hooks. `useRegister` is overloaded for single, array, and rest-argument registration:
139
165
 
140
166
  | Hook | Purpose |
@@ -167,7 +193,7 @@ import { CreateForm } from '@ilokesto/form';
167
193
  import { useForm } from '@ilokesto/form/vue';
168
194
 
169
195
  const form = new CreateForm({
170
- initialValues: {
196
+ defaultValues: {
171
197
  email: '',
172
198
  remember: false,
173
199
  role: 'user',
@@ -231,7 +257,7 @@ import { CreateForm } from '@ilokesto/form';
231
257
  import { useForm } from '@ilokesto/form/solid';
232
258
 
233
259
  const form = new CreateForm({
234
- initialValues: {
260
+ defaultValues: {
235
261
  email: '',
236
262
  remember: false,
237
263
  role: 'user',
@@ -276,7 +302,7 @@ Svelte bindings are exposed through the `./svelte` subpath. Svelte does not use
276
302
  import { useForm } from '@ilokesto/form/svelte';
277
303
 
278
304
  const form = new CreateForm({
279
- initialValues: {
305
+ defaultValues: {
280
306
  email: '',
281
307
  remember: false,
282
308
  role: 'user',
@@ -391,7 +417,7 @@ The internal snapshot is normalized.
391
417
 
392
418
  ```ts
393
419
  type FormState<TValues> = {
394
- initialValues: TValues;
420
+ defaultValues: TValues;
395
421
  fields: Record<PathKey, FieldState>;
396
422
  submitCount: number;
397
423
  arrayKeys: Record<PathKey, string[]>;
@@ -402,7 +428,7 @@ For this input:
402
428
 
403
429
  ```ts
404
430
  const form = new CreateForm({
405
- initialValues: {
431
+ defaultValues: {
406
432
  user: { name: 'Ada' },
407
433
  items: [{ title: 'A' }, { title: 'B' }],
408
434
  },
@@ -413,7 +439,7 @@ The core stores leaf field states and array container keys separately:
413
439
 
414
440
  ```ts
415
441
  {
416
- initialValues: {
442
+ defaultValues: {
417
443
  user: { name: 'Ada' },
418
444
  items: [{ title: 'A' }, { title: 'B' }],
419
445
  },
@@ -490,7 +516,7 @@ The cleanup function removes the schema registration if it is still the latest r
490
516
 
491
517
  Array item identity is stored separately from array values.
492
518
 
493
- - Items from `initialValues` get deterministic keys: `initial-0`, `initial-1`, ...
519
+ - Items from `defaultValues` get deterministic keys: `initial-0`, `initial-1`, ...
494
520
  - Runtime insertions get generated keys: `item-1`, `item-2`, ...
495
521
  - `move()` and `swap()` move keys with values.
496
522
  - `replace()` creates a new key for every new item and does not preserve old child metadata.
@@ -503,7 +529,7 @@ These keys are intended for framework list rendering.
503
529
 
504
530
  ```ts
505
531
  const form = new CreateForm({
506
- initialValues,
532
+ defaultValues,
507
533
  schema,
508
534
  schemaOptions,
509
535
  validateOn,
@@ -512,7 +538,7 @@ const form = new CreateForm({
512
538
 
513
539
  Options:
514
540
 
515
- - `initialValues`: required initial value tree.
541
+ - `defaultValues`: required default value tree. This is also the reset/dirty baseline.
516
542
  - `schema`: optional Standard Schema v1 compatible schema.
517
543
  - `schemaOptions`: optional Standard Schema validation options.
518
544
  - `validateOn`: optional automatic validation triggers. Defaults to `['submit']`.
@@ -595,7 +621,7 @@ Effects:
595
621
 
596
622
  1. Converts the public path input to an internal tuple path.
597
623
  2. Writes the new value to the corresponding `FieldState`.
598
- 3. Recomputes `dirty` by comparing with `initialValues` at the same path.
624
+ 3. Recomputes `dirty` by comparing with `defaultValues` at the same path.
599
625
  4. Sets `modified` to `true` only when `source === 'user'`.
600
626
  5. Starts change validation if `options.validate` is true or `validateOn` contains `'change'`.
601
627
 
@@ -657,16 +683,26 @@ console.log(items.keys());
657
683
 
658
684
  The array controller reads the latest values and keys from the store each time a command runs.
659
685
 
660
- ### `reset(values?)`
686
+ ### `reset(values?, options?)`
661
687
 
662
688
  Resets the form to initial state.
663
689
 
664
690
  ```ts
665
691
  form.reset();
666
692
  form.reset({ email: 'new@example.com' });
693
+ form.reset({ email: 'new@example.com' }, { keepDirtyValues: true });
667
694
  ```
668
695
 
669
- Without an argument, it reuses the current `initialValues`. With an argument, the argument becomes the new `initialValues`.
696
+ Without an argument, it reuses the current `defaultValues`. With an argument, the argument becomes the new `defaultValues`.
697
+
698
+ Reset options can preserve selected state for fields that still exist in the new normalized value shape:
699
+
700
+ | Option | Behavior |
701
+ | --- | --- |
702
+ | `keepDirtyValues` | Keeps current values for dirty fields and recomputes dirty against the new `defaultValues`. |
703
+ | `keepErrors` | Keeps existing errors for surviving field paths. |
704
+ | `keepTouched` | Keeps touched flags for surviving field paths. |
705
+ | `keepSubmitState` | Keeps `submitCount`, `isSubmitting`, `isSubmitted`, and `isSubmitSuccessful`. |
670
706
 
671
707
  ### `submit(onValid, onInvalid?)`
672
708
 
@@ -721,8 +757,8 @@ Replaces the whole array. Existing item links are intentionally broken, so child
721
757
 
722
758
  ```txt
723
759
  new CreateForm(options)
724
- -> new FormStateStore(options.initialValues)
725
- -> FormStateInitializer.initialize(initialValues)
760
+ -> new FormStateStore(options.defaultValues)
761
+ -> FormStateInitializer.initialize(defaultValues)
726
762
  -> fields for leaf values
727
763
  -> arrayKeys for array containers
728
764
  -> new ValidationEngine(store, options)
@@ -741,7 +777,7 @@ form.setValue(path, value, options)
741
777
  -> FormPath.toFieldPath(path)
742
778
  -> FormStateStore.setValue()
743
779
  -> FormStateWriter.setValue()
744
- -> compute dirty from initialValues
780
+ -> compute dirty from defaultValues
745
781
  -> optionally mark modified
746
782
  -> optionally start change validation
747
783
  ```
@@ -970,7 +1006,7 @@ It creates a new `errors` array each time to avoid shared mutable references. Th
970
1006
 
971
1007
  ### `src/core/state/FormStateInitializer.ts`
972
1008
 
973
- `FormStateInitializer.initialize(initialValues)` converts nested values into normalized `FormState`.
1009
+ `FormStateInitializer.initialize(defaultValues)` converts nested values into normalized `FormState`.
974
1010
 
975
1011
  Rules:
976
1012