@ilokesto/form 2.0.0 → 2.0.1
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 +74 -481
- package/README.md +76 -482
- package/dist/{chunk-EPQC3LKB.js → chunk-4F4SAGQ5.js} +320 -504
- package/dist/chunk-4F4SAGQ5.js.map +1 -0
- package/dist/{chunk-OBMSHDV2.js → chunk-JK644RR7.js} +3 -3
- package/dist/{chunk-OBMSHDV2.js.map → chunk-JK644RR7.js.map} +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +1 -1
- package/dist/react/index.js +2 -2
- package/dist/solid/index.js +2 -2
- package/dist/svelte/index.js +2 -2
- package/dist/vue/index.js +2 -2
- package/package.json +2 -2
- package/dist/chunk-EPQC3LKB.js.map +0 -1
|
@@ -77,7 +77,7 @@ var FormArrayMutationPlanner = class {
|
|
|
77
77
|
* @param nextKey - 새 item에 부여할 key.
|
|
78
78
|
* @returns rebase에 필요한 mutation.
|
|
79
79
|
*/
|
|
80
|
-
insert(currentArray, currentKeys, index, value, nextKey) {
|
|
80
|
+
static insert(currentArray, currentKeys, index, value, nextKey) {
|
|
81
81
|
const boundedIndex = Math.max(0, Math.min(index, currentArray.length));
|
|
82
82
|
const nextArray = [...currentArray];
|
|
83
83
|
const nextKeys = [...currentKeys];
|
|
@@ -100,7 +100,7 @@ var FormArrayMutationPlanner = class {
|
|
|
100
100
|
* @param nextKey - 새 item key.
|
|
101
101
|
* @returns rebase에 필요한 mutation.
|
|
102
102
|
*/
|
|
103
|
-
push(currentArray, currentKeys, value, nextKey) {
|
|
103
|
+
static push(currentArray, currentKeys, value, nextKey) {
|
|
104
104
|
return {
|
|
105
105
|
values: [...currentArray, value],
|
|
106
106
|
keys: [...currentKeys, nextKey],
|
|
@@ -115,7 +115,7 @@ var FormArrayMutationPlanner = class {
|
|
|
115
115
|
* @param index - 제거할 index.
|
|
116
116
|
* @returns 유효하지 않은 index면 undefined, 아니면 mutation.
|
|
117
117
|
*/
|
|
118
|
-
remove(currentArray, currentKeys, index) {
|
|
118
|
+
static remove(currentArray, currentKeys, index) {
|
|
119
119
|
if (index < 0 || index >= currentArray.length) {
|
|
120
120
|
return void 0;
|
|
121
121
|
}
|
|
@@ -135,7 +135,7 @@ var FormArrayMutationPlanner = class {
|
|
|
135
135
|
* @param toIndex - 이동 후 index.
|
|
136
136
|
* @returns 유효하지 않은 index거나 같은 위치면 undefined, 아니면 mutation.
|
|
137
137
|
*/
|
|
138
|
-
move(currentArray, currentKeys, fromIndex, toIndex) {
|
|
138
|
+
static move(currentArray, currentKeys, fromIndex, toIndex) {
|
|
139
139
|
if (fromIndex < 0 || fromIndex >= currentArray.length || toIndex < 0 || toIndex >= currentArray.length || fromIndex === toIndex) {
|
|
140
140
|
return void 0;
|
|
141
141
|
}
|
|
@@ -155,7 +155,7 @@ var FormArrayMutationPlanner = class {
|
|
|
155
155
|
* @param rightIndex - 두 번째 index.
|
|
156
156
|
* @returns 유효하지 않은 index거나 같은 위치면 undefined, 아니면 mutation.
|
|
157
157
|
*/
|
|
158
|
-
swap(currentArray, currentKeys, leftIndex, rightIndex) {
|
|
158
|
+
static swap(currentArray, currentKeys, leftIndex, rightIndex) {
|
|
159
159
|
if (leftIndex < 0 || rightIndex < 0 || leftIndex >= currentArray.length || rightIndex >= currentArray.length || leftIndex === rightIndex) {
|
|
160
160
|
return void 0;
|
|
161
161
|
}
|
|
@@ -176,7 +176,7 @@ var FormArrayMutationPlanner = class {
|
|
|
176
176
|
* @param nextKeys - 새 item key 목록.
|
|
177
177
|
* @returns rebase에 필요한 mutation.
|
|
178
178
|
*/
|
|
179
|
-
replace(values, nextKeys) {
|
|
179
|
+
static replace(values, nextKeys) {
|
|
180
180
|
return {
|
|
181
181
|
values: [...values],
|
|
182
182
|
keys: [...nextKeys],
|
|
@@ -492,310 +492,38 @@ var ValueHelper = class {
|
|
|
492
492
|
};
|
|
493
493
|
|
|
494
494
|
// src/core/state/FormStateReader.ts
|
|
495
|
-
var FormStateReader = class {
|
|
496
|
-
/** getSnapshot은 호출 시점의 최신 FormState를 반환해야 한다. */
|
|
497
|
-
constructor(getSnapshot) {
|
|
498
|
-
this.getSnapshot = getSnapshot;
|
|
499
|
-
}
|
|
500
|
-
getSnapshot;
|
|
495
|
+
var FormStateReader = class _FormStateReader {
|
|
501
496
|
/**
|
|
502
497
|
* fields 객체의 key들을 FieldPath로 되돌린다.
|
|
503
498
|
*
|
|
504
499
|
* ValueHelper.getValuesFromFields는 field value를 nested object로 복원할 때
|
|
505
500
|
* 각 PathKey가 실제로 어떤 tuple path였는지 알아야 한다.
|
|
506
501
|
*/
|
|
507
|
-
getKnownFieldPaths() {
|
|
508
|
-
return Object.keys(
|
|
502
|
+
static getKnownFieldPaths(state) {
|
|
503
|
+
return Object.keys(state.fields).reduce((paths, key) => {
|
|
509
504
|
paths[key] = FormPath.keyToPath(key);
|
|
510
505
|
return paths;
|
|
511
506
|
}, {});
|
|
512
507
|
}
|
|
513
508
|
/** key로 FieldState를 읽고, 없는 key라면 default FieldState를 반환해 caller가 undefined 처리를 하지 않게 한다. */
|
|
514
|
-
getFieldStateByKey(fieldKey) {
|
|
515
|
-
return
|
|
509
|
+
static getFieldStateByKey(state, fieldKey) {
|
|
510
|
+
return state.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
516
511
|
}
|
|
517
512
|
/** public path input을 PathKey로 바꾼 뒤 FieldState를 읽는다. */
|
|
518
|
-
getFieldState(fieldPath) {
|
|
519
|
-
return
|
|
513
|
+
static getFieldState(state, fieldPath) {
|
|
514
|
+
return _FormStateReader.getFieldStateByKey(state, FormPath.pathInputToKey(fieldPath));
|
|
520
515
|
}
|
|
521
516
|
/** FieldState에서 value만 꺼내는 편의 메서드다. */
|
|
522
|
-
getValue(fieldPath) {
|
|
523
|
-
return
|
|
517
|
+
static getValue(state, fieldPath) {
|
|
518
|
+
return _FormStateReader.getFieldState(state, fieldPath).value;
|
|
524
519
|
}
|
|
525
520
|
/** 현재 FormState를 사용자가 넘긴 defaultValues와 같은 nested 구조로 복원한다. */
|
|
526
|
-
getValues() {
|
|
527
|
-
return ValueHelper.getValuesFromFields(
|
|
521
|
+
static getValues(state) {
|
|
522
|
+
return ValueHelper.getValuesFromFields(state, _FormStateReader.getKnownFieldPaths(state));
|
|
528
523
|
}
|
|
529
524
|
/** 복원된 values에서 FieldPath가 가리키는 nested 값을 읽는다. */
|
|
530
|
-
getValueAtPath(fieldPath) {
|
|
531
|
-
return ValueHelper.getValueAtPath(
|
|
532
|
-
}
|
|
533
|
-
};
|
|
534
|
-
var FormStateWriter = class _FormStateWriter {
|
|
535
|
-
/** getState와 setState만 있으면 충분하므로 Store 전체 API에 의존하지 않는다. */
|
|
536
|
-
constructor(store) {
|
|
537
|
-
this.store = store;
|
|
538
|
-
}
|
|
539
|
-
store;
|
|
540
|
-
/**
|
|
541
|
-
* 한 field value를 갱신한다.
|
|
542
|
-
*
|
|
543
|
-
* dirty는 defaultValues의 같은 path 값과 Object.is로 비교해 계산한다.
|
|
544
|
-
* modified는 사용자가 만든 변경(source: 'user')일 때만 true로 바꾼다.
|
|
545
|
-
* 반환한 PathKey는 이후 validation 실행에 재사용된다.
|
|
546
|
-
*/
|
|
547
|
-
setValue(fieldPath, value, options = {}) {
|
|
548
|
-
const fieldKey = FormPath.pathToKey(fieldPath);
|
|
549
|
-
const defaultValue = ValueHelper.getValueAtPath(this.store.getState().defaultValues, fieldPath);
|
|
550
|
-
this.store.setState(
|
|
551
|
-
(prevState) => produce(prevState, (draft) => {
|
|
552
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
553
|
-
draft.fields[fieldKey] = {
|
|
554
|
-
...previousField,
|
|
555
|
-
value,
|
|
556
|
-
dirty: !_FormStateWriter.isSameValue(value, defaultValue),
|
|
557
|
-
modified: options.source === "user" ? true : previousField.modified
|
|
558
|
-
};
|
|
559
|
-
})
|
|
560
|
-
);
|
|
561
|
-
return fieldKey;
|
|
562
|
-
}
|
|
563
|
-
/** blur된 field의 touched flag를 true로 바꾼다. 기존 field가 없으면 default state에서 시작한다. */
|
|
564
|
-
touchField(fieldKey) {
|
|
565
|
-
this.store.setState(
|
|
566
|
-
(prevState) => produce(prevState, (draft) => {
|
|
567
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
568
|
-
draft.fields[fieldKey] = {
|
|
569
|
-
...previousField,
|
|
570
|
-
touched: true
|
|
571
|
-
};
|
|
572
|
-
})
|
|
573
|
-
);
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* field의 isFocused를 true로 바꾼다. focus 이벤트에서 호출된다.
|
|
577
|
-
*
|
|
578
|
-
* fieldKey에 해당하는 field가 없으면 default state를 생성해 기록한다. 이는 touchField와 동일한 패턴이지만,
|
|
579
|
-
* 존재하지 않는 path에 대해 의도치 않게 field가 생성되는 부수 효과에 유의해야 한다.
|
|
580
|
-
*/
|
|
581
|
-
focusField(fieldKey) {
|
|
582
|
-
this.store.setState(
|
|
583
|
-
(prevState) => produce(prevState, (draft) => {
|
|
584
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
585
|
-
draft.fields[fieldKey] = {
|
|
586
|
-
...previousField,
|
|
587
|
-
isFocused: true
|
|
588
|
-
};
|
|
589
|
-
})
|
|
590
|
-
);
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* field의 isFocused를 false로 바꾼다. blur 이벤트에서 호출된다.
|
|
594
|
-
*
|
|
595
|
-
* fieldKey에 해당하는 field가 없으면 default state를 생성해 기록한다. focusField와 동일한 fallback 패턴이며,
|
|
596
|
-
* 존재하지 않는 path에 대해 의도치 않게 field가 생성되는 부수 효과에 유의해야 한다.
|
|
597
|
-
*/
|
|
598
|
-
unfocusField(fieldKey) {
|
|
599
|
-
this.store.setState(
|
|
600
|
-
(prevState) => produce(prevState, (draft) => {
|
|
601
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
602
|
-
draft.fields[fieldKey] = {
|
|
603
|
-
...previousField,
|
|
604
|
-
isFocused: false
|
|
605
|
-
};
|
|
606
|
-
})
|
|
607
|
-
);
|
|
608
|
-
}
|
|
609
|
-
/** validation 또는 외부 명령 결과로 field의 errors를 통째로 교체한다. */
|
|
610
|
-
setErrorsByKey(fieldKey, errors) {
|
|
611
|
-
this.store.setState(
|
|
612
|
-
(prevState) => produce(prevState, (draft) => {
|
|
613
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
614
|
-
draft.fields[fieldKey] = {
|
|
615
|
-
...previousField,
|
|
616
|
-
errors: [...errors]
|
|
617
|
-
};
|
|
618
|
-
})
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* errors를 비운다.
|
|
623
|
-
*
|
|
624
|
-
* fieldKeys가 있으면 해당 key들만 대상으로 하고, 없으면 현재 존재하는 모든 fields를 대상으로 한다.
|
|
625
|
-
* 존재하지 않는 key가 들어와도 default field를 만든 뒤 errors: [] 상태로 저장한다.
|
|
626
|
-
*/
|
|
627
|
-
clearErrors(fieldKeys) {
|
|
628
|
-
this.store.setState(
|
|
629
|
-
(prevState) => produce(prevState, (draft) => {
|
|
630
|
-
const keys = fieldKeys && fieldKeys.length > 0 ? fieldKeys : Object.keys(draft.fields);
|
|
631
|
-
keys.forEach((fieldKey) => {
|
|
632
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
633
|
-
draft.fields[fieldKey] = {
|
|
634
|
-
...previousField,
|
|
635
|
-
errors: []
|
|
636
|
-
};
|
|
637
|
-
});
|
|
638
|
-
})
|
|
639
|
-
);
|
|
640
|
-
}
|
|
641
|
-
/** 새 values가 있으면 그것을 새 defaultValues로 삼고, 없으면 기존 defaultValues로 FormState를 재생성한다. */
|
|
642
|
-
reset(values, options = {}) {
|
|
643
|
-
const previousState = this.store.getState();
|
|
644
|
-
const nextDefaultValues = values ?? previousState.defaultValues;
|
|
645
|
-
const initializedState = FormStateInitializer.initialize(nextDefaultValues);
|
|
646
|
-
this.store.setState(
|
|
647
|
-
produce(initializedState, (draft) => {
|
|
648
|
-
Object.entries(draft.fields).forEach(([fieldKey, nextField]) => {
|
|
649
|
-
const previousField = previousState.fields[fieldKey];
|
|
650
|
-
if (!previousField) {
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
if (options.keepDirtyValues && previousField.dirty) {
|
|
654
|
-
const fieldPath = FormPath.keyToPath(fieldKey);
|
|
655
|
-
const defaultValue = ValueHelper.getValueAtPath(nextDefaultValues, fieldPath);
|
|
656
|
-
nextField.value = previousField.value;
|
|
657
|
-
nextField.dirty = !_FormStateWriter.isSameValue(previousField.value, defaultValue);
|
|
658
|
-
nextField.modified = previousField.modified;
|
|
659
|
-
}
|
|
660
|
-
if (options.keepErrors) {
|
|
661
|
-
nextField.errors = [...previousField.errors];
|
|
662
|
-
}
|
|
663
|
-
if (options.keepTouched) {
|
|
664
|
-
nextField.touched = previousField.touched;
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
if (options.keepSubmitState) {
|
|
668
|
-
draft.submitCount = previousState.submitCount;
|
|
669
|
-
draft.isSubmitting = previousState.isSubmitting;
|
|
670
|
-
draft.isSubmitted = previousState.isSubmitted;
|
|
671
|
-
draft.isSubmitSuccessful = previousState.isSubmitSuccessful;
|
|
672
|
-
}
|
|
673
|
-
})
|
|
674
|
-
);
|
|
675
|
-
}
|
|
676
|
-
/** submit 시도 횟수를 증가시키고 진행 중 상태를 기록한다. */
|
|
677
|
-
beginSubmit() {
|
|
678
|
-
this.store.setState(
|
|
679
|
-
(prevState) => produce(prevState, (draft) => {
|
|
680
|
-
draft.submitCount += 1;
|
|
681
|
-
draft.isSubmitting = true;
|
|
682
|
-
draft.isSubmitSuccessful = false;
|
|
683
|
-
})
|
|
684
|
-
);
|
|
685
|
-
}
|
|
686
|
-
/** submit validation/callback 완료 후 성공 여부를 기록한다. */
|
|
687
|
-
completeSubmit(successful) {
|
|
688
|
-
this.store.setState(
|
|
689
|
-
(prevState) => produce(prevState, (draft) => {
|
|
690
|
-
draft.isSubmitting = false;
|
|
691
|
-
draft.isSubmitted = true;
|
|
692
|
-
draft.isSubmitSuccessful = successful;
|
|
693
|
-
})
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
/** 이미 완성된 FormState updater를 store에 전달한다. 배열 리베이스처럼 여러 조각을 한 번에 바꿀 때 사용한다. */
|
|
697
|
-
replaceState(updater) {
|
|
698
|
-
this.store.setState(updater);
|
|
699
|
-
}
|
|
700
|
-
/** dirty 계산에 쓰는 값 비교 규칙이다. NaN 같은 JS 특수값을 위해 Object.is를 사용한다. */
|
|
701
|
-
static isSameValue(left, right) {
|
|
702
|
-
return Object.is(left, right);
|
|
703
|
-
}
|
|
704
|
-
};
|
|
705
|
-
|
|
706
|
-
// src/core/state/FormStateStore.ts
|
|
707
|
-
var FormStateStore = class {
|
|
708
|
-
/** subscribe/getState/setState를 제공하는 외부 store 구현체. */
|
|
709
|
-
store;
|
|
710
|
-
/** field state와 values를 읽고 복원하는 전담 객체. */
|
|
711
|
-
reader;
|
|
712
|
-
/** immer 기반으로 FormState를 갱신하는 전담 객체. */
|
|
713
|
-
writer;
|
|
714
|
-
/** 존재하지 않는 필드를 읽을 때도 public API가 안정적인 FieldState shape을 반환하도록 만든다. */
|
|
715
|
-
static getDefaultFieldState() {
|
|
716
|
-
return FieldStateFactory.createDefault();
|
|
717
|
-
}
|
|
718
|
-
/**
|
|
719
|
-
* defaultValues를 FormState로 변환해 store를 만든다.
|
|
720
|
-
*
|
|
721
|
-
* FormStateInitializer는 nested object/array를 순회해 leaf field와 array key 정보를 만든다.
|
|
722
|
-
*/
|
|
723
|
-
constructor(defaultValues) {
|
|
724
|
-
this.store = createStore(FormStateInitializer.initialize(defaultValues));
|
|
725
|
-
this.reader = new FormStateReader(() => this.store.getState());
|
|
726
|
-
this.writer = new FormStateWriter(this.store);
|
|
727
|
-
}
|
|
728
|
-
/** 현재 FormState snapshot을 반환한다. 반환값은 읽기 전용으로 취급한다. */
|
|
729
|
-
getState() {
|
|
730
|
-
return this.store.getState();
|
|
731
|
-
}
|
|
732
|
-
/** 상태 변경 시 호출될 listener를 등록하고 unsubscribe 함수를 반환한다. */
|
|
733
|
-
subscribe(listener) {
|
|
734
|
-
return this.store.subscribe(listener);
|
|
735
|
-
}
|
|
736
|
-
/** 현재 fields에 존재하는 모든 PathKey를 FieldPath로 되돌린 map을 만든다. */
|
|
737
|
-
getKnownFieldPaths() {
|
|
738
|
-
return this.reader.getKnownFieldPaths();
|
|
739
|
-
}
|
|
740
|
-
/** 이미 정규화된 key로 FieldState를 읽는다. validation처럼 key 기반으로 동작하는 곳에서 사용한다. */
|
|
741
|
-
getFieldStateByKey(fieldKey) {
|
|
742
|
-
return this.reader.getFieldStateByKey(fieldKey);
|
|
743
|
-
}
|
|
744
|
-
/** public path input을 받아 해당 FieldState를 읽는다. */
|
|
745
|
-
getFieldState(fieldPath) {
|
|
746
|
-
return this.reader.getFieldState(fieldPath);
|
|
747
|
-
}
|
|
748
|
-
/** public path input을 받아 해당 value만 읽는다. */
|
|
749
|
-
getValue(fieldPath) {
|
|
750
|
-
return this.reader.getValue(fieldPath);
|
|
751
|
-
}
|
|
752
|
-
/** fields와 arrayKeys로부터 전체 values 객체를 복원한다. */
|
|
753
|
-
getValues() {
|
|
754
|
-
return this.reader.getValues();
|
|
755
|
-
}
|
|
756
|
-
/** 내부 FieldPath를 기준으로 최신 values에서 nested value를 읽는다. 배열 controller가 현재 배열 값을 얻을 때 사용한다. */
|
|
757
|
-
getValueAtPath(fieldPath) {
|
|
758
|
-
return this.reader.getValueAtPath(fieldPath);
|
|
759
|
-
}
|
|
760
|
-
/** 한 field value를 쓰고, dirty/modified 같은 메타데이터를 함께 갱신한다. */
|
|
761
|
-
setValue(fieldPath, value, options = {}) {
|
|
762
|
-
return this.writer.setValue(fieldPath, value, options);
|
|
763
|
-
}
|
|
764
|
-
/** field를 touched 상태로 바꾼다. 보통 blur 이벤트에서 호출된다. */
|
|
765
|
-
touchField(fieldKey) {
|
|
766
|
-
this.writer.touchField(fieldKey);
|
|
767
|
-
}
|
|
768
|
-
/** field의 isFocused를 true로 바꾼다. focus 이벤트에서 호출된다. */
|
|
769
|
-
focusField(fieldKey) {
|
|
770
|
-
this.writer.focusField(fieldKey);
|
|
771
|
-
}
|
|
772
|
-
/** field의 isFocused를 false로 바꾼다. blur 이벤트에서 호출된다. */
|
|
773
|
-
unfocusField(fieldKey) {
|
|
774
|
-
this.writer.unfocusField(fieldKey);
|
|
775
|
-
}
|
|
776
|
-
/** field의 errors를 새 배열로 교체한다. validation 결과와 외부 setErrors가 공통으로 사용한다. */
|
|
777
|
-
setErrorsByKey(fieldKey, errors) {
|
|
778
|
-
this.writer.setErrorsByKey(fieldKey, errors);
|
|
779
|
-
}
|
|
780
|
-
/** 특정 fields 또는 전체 fields의 errors를 비운다. */
|
|
781
|
-
clearErrors(fieldKeys) {
|
|
782
|
-
this.writer.clearErrors(fieldKeys);
|
|
783
|
-
}
|
|
784
|
-
/** form을 defaultValues 또는 새 values 기준으로 다시 초기화한다. */
|
|
785
|
-
reset(values, options) {
|
|
786
|
-
this.writer.reset(values, options);
|
|
787
|
-
}
|
|
788
|
-
/** submit 시도 시작 상태를 기록한다. */
|
|
789
|
-
beginSubmit() {
|
|
790
|
-
this.writer.beginSubmit();
|
|
791
|
-
}
|
|
792
|
-
/** submit 시도 완료 상태를 기록한다. */
|
|
793
|
-
completeSubmit(successful) {
|
|
794
|
-
this.writer.completeSubmit(successful);
|
|
795
|
-
}
|
|
796
|
-
/** 배열 rebasing처럼 전체 FormState를 계산해 교체해야 하는 작업에 쓰는 escape hatch다. */
|
|
797
|
-
replaceState(updater) {
|
|
798
|
-
this.writer.replaceState(updater);
|
|
525
|
+
static getValueAtPath(state, fieldPath) {
|
|
526
|
+
return ValueHelper.getValueAtPath(_FormStateReader.getValues(state), fieldPath);
|
|
799
527
|
}
|
|
800
528
|
};
|
|
801
529
|
|
|
@@ -813,17 +541,14 @@ var FormArrayRebaser = class {
|
|
|
813
541
|
* 4. 배열 child field는 mapPreviousIndex로 새 index를 찾아 메타데이터를 옮긴다.
|
|
814
542
|
* 5. 살아남은 arrayKeys를 보존하고 변경된 배열의 key와 submitCount를 새 state에 다시 반영한다.
|
|
815
543
|
*
|
|
816
|
-
* @param
|
|
544
|
+
* @param previousState - 변경 전 FormState snapshot.
|
|
817
545
|
* @param fieldPath - 변경된 배열 field path.
|
|
818
|
-
* @param
|
|
819
|
-
* @param nextKeys - 변경 후 배열 item key 목록.
|
|
820
|
-
* @param mapPreviousIndex - 이전 item index를 새 index로 바꾸는 mapper.
|
|
546
|
+
* @param mutation - 변경 후 배열 값, item key, 이전 index를 새 index로 바꾸는 mapper.
|
|
821
547
|
* @returns store에 넣을 새 FormState.
|
|
822
548
|
*/
|
|
823
|
-
static rebase(
|
|
824
|
-
const
|
|
825
|
-
const
|
|
826
|
-
const nextValues = ValueHelper.setValueAtPath(store.getValues(), fieldPath, [...nextArray]);
|
|
549
|
+
static rebase(previousState, fieldPath, mutation) {
|
|
550
|
+
const fieldPaths = FormStateReader.getKnownFieldPaths(previousState);
|
|
551
|
+
const nextValues = ValueHelper.setValueAtPath(FormStateReader.getValues(previousState), fieldPath, [...mutation.values]);
|
|
827
552
|
const initializedState = FormStateInitializer.initialize(nextValues);
|
|
828
553
|
const arrayKey = FormPath.pathToKey(fieldPath);
|
|
829
554
|
const rebasedFields = { ...initializedState.fields };
|
|
@@ -841,13 +566,13 @@ var FormArrayRebaser = class {
|
|
|
841
566
|
if (typeof previousIndex !== "number") {
|
|
842
567
|
return;
|
|
843
568
|
}
|
|
844
|
-
const nextIndex = mapPreviousIndex(previousIndex);
|
|
569
|
+
const nextIndex = mutation.mapPreviousIndex(previousIndex);
|
|
845
570
|
if (nextIndex === void 0) {
|
|
846
571
|
return;
|
|
847
572
|
}
|
|
848
573
|
const nextFieldPath = FormArrayPath.replaceArrayIndex(currentFieldPath, fieldPath, nextIndex);
|
|
849
574
|
const nextFieldKey = FormPath.pathToKey(nextFieldPath);
|
|
850
|
-
const nextField = initializedState.fields[nextFieldKey] ??
|
|
575
|
+
const nextField = initializedState.fields[nextFieldKey] ?? FieldStateFactory.createDefault();
|
|
851
576
|
rebasedFields[nextFieldKey] = {
|
|
852
577
|
...nextField,
|
|
853
578
|
errors: previousField.errors,
|
|
@@ -868,7 +593,7 @@ var FormArrayRebaser = class {
|
|
|
868
593
|
if (typeof previousIndex !== "number") {
|
|
869
594
|
return;
|
|
870
595
|
}
|
|
871
|
-
const nextIndex = mapPreviousIndex(previousIndex);
|
|
596
|
+
const nextIndex = mutation.mapPreviousIndex(previousIndex);
|
|
872
597
|
if (nextIndex === void 0) {
|
|
873
598
|
return;
|
|
874
599
|
}
|
|
@@ -880,7 +605,7 @@ var FormArrayRebaser = class {
|
|
|
880
605
|
rebasedArrayKeys[nextArrayKey] = [...previousKeys];
|
|
881
606
|
}
|
|
882
607
|
});
|
|
883
|
-
rebasedArrayKeys[arrayKey] = [...
|
|
608
|
+
rebasedArrayKeys[arrayKey] = [...mutation.keys];
|
|
884
609
|
return {
|
|
885
610
|
...initializedState,
|
|
886
611
|
fields: rebasedFields,
|
|
@@ -901,20 +626,17 @@ var FormArrayController = class {
|
|
|
901
626
|
keyGenerator;
|
|
902
627
|
/** 이 controller가 담당하는 배열 field의 내부 tuple path다. */
|
|
903
628
|
fieldPath;
|
|
904
|
-
/** insert/remove/move/swap/replace의 다음 배열 상태를 계산하는 planner다. */
|
|
905
|
-
mutations;
|
|
906
629
|
/**
|
|
907
630
|
* 배열 controller를 만든다.
|
|
908
631
|
*
|
|
909
632
|
* @param store - form state store.
|
|
910
|
-
* @param keys -
|
|
633
|
+
* @param keys - 같은 form의 controller들이 공유하는 item key 생성기.
|
|
911
634
|
* @param fieldPath - 제어할 배열 field path.
|
|
912
635
|
*/
|
|
913
636
|
constructor(store, keys, fieldPath) {
|
|
914
637
|
this.store = store;
|
|
915
638
|
this.keyGenerator = keys;
|
|
916
639
|
this.fieldPath = fieldPath;
|
|
917
|
-
this.mutations = new FormArrayMutationPlanner();
|
|
918
640
|
}
|
|
919
641
|
/**
|
|
920
642
|
* 현재 배열 item들의 안정적인 render key를 반환한다.
|
|
@@ -931,7 +653,7 @@ var FormArrayController = class {
|
|
|
931
653
|
* @param value - 삽입할 item value.
|
|
932
654
|
*/
|
|
933
655
|
insert(index, value) {
|
|
934
|
-
this.applyMutation(
|
|
656
|
+
this.applyMutation(FormArrayMutationPlanner.insert(this.getArray(), this.getKeys(), index, value, this.keyGenerator.create()));
|
|
935
657
|
}
|
|
936
658
|
/**
|
|
937
659
|
* 배열 끝에 item을 추가한다.
|
|
@@ -939,7 +661,7 @@ var FormArrayController = class {
|
|
|
939
661
|
* @param value - 추가할 item value.
|
|
940
662
|
*/
|
|
941
663
|
push(value) {
|
|
942
|
-
this.applyMutation(
|
|
664
|
+
this.applyMutation(FormArrayMutationPlanner.push(this.getArray(), this.getKeys(), value, this.keyGenerator.create()));
|
|
943
665
|
}
|
|
944
666
|
/**
|
|
945
667
|
* 지정 index의 item을 제거한다.
|
|
@@ -947,7 +669,7 @@ var FormArrayController = class {
|
|
|
947
669
|
* @param index - 제거할 item index. 유효하지 않으면 아무 일도 하지 않는다.
|
|
948
670
|
*/
|
|
949
671
|
remove(index) {
|
|
950
|
-
this.applyMutation(
|
|
672
|
+
this.applyMutation(FormArrayMutationPlanner.remove(this.getArray(), this.getKeys(), index));
|
|
951
673
|
}
|
|
952
674
|
/**
|
|
953
675
|
* item을 한 위치에서 다른 위치로 이동한다.
|
|
@@ -956,7 +678,7 @@ var FormArrayController = class {
|
|
|
956
678
|
* @param toIndex - 이동 후 index.
|
|
957
679
|
*/
|
|
958
680
|
move(fromIndex, toIndex) {
|
|
959
|
-
this.applyMutation(
|
|
681
|
+
this.applyMutation(FormArrayMutationPlanner.move(this.getArray(), this.getKeys(), fromIndex, toIndex));
|
|
960
682
|
}
|
|
961
683
|
/**
|
|
962
684
|
* 두 item의 위치를 맞바꾼다.
|
|
@@ -965,166 +687,50 @@ var FormArrayController = class {
|
|
|
965
687
|
* @param rightIndex - 두 번째 item index.
|
|
966
688
|
*/
|
|
967
689
|
swap(leftIndex, rightIndex) {
|
|
968
|
-
this.applyMutation(
|
|
690
|
+
this.applyMutation(FormArrayMutationPlanner.swap(this.getArray(), this.getKeys(), leftIndex, rightIndex));
|
|
969
691
|
}
|
|
970
692
|
/**
|
|
971
693
|
* 배열 전체를 새 values로 교체한다.
|
|
972
|
-
*
|
|
973
|
-
* @remarks
|
|
974
|
-
* 기존 child field 상태는 재사용하지 않는다. 모든 item key도 새로 만든다.
|
|
975
|
-
*
|
|
976
|
-
* @param values - 새 배열 값.
|
|
977
|
-
*/
|
|
978
|
-
replace(values) {
|
|
979
|
-
this.applyMutation(
|
|
980
|
-
}
|
|
981
|
-
/**
|
|
982
|
-
* store에서 현재 배열 값을 읽는다.
|
|
983
|
-
*
|
|
984
|
-
* @returns path의 값이 배열이면 그 배열, 아니면 빈 배열.
|
|
985
|
-
*/
|
|
986
|
-
getArray() {
|
|
987
|
-
const value = this.store.getValueAtPath(this.fieldPath);
|
|
988
|
-
return Array.isArray(value) ? value : [];
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* 현재 배열에 대응하는 render key 목록을 읽는다.
|
|
992
|
-
*
|
|
993
|
-
* @returns store에 저장된 arrayKeys가 있으면 그것을 사용하고, 없으면 현재 배열 길이만큼 새 key를 만든다.
|
|
994
|
-
*/
|
|
995
|
-
getKeys() {
|
|
996
|
-
const arrayKey = FormPath.pathToKey(this.fieldPath);
|
|
997
|
-
const currentArray = this.getArray();
|
|
998
|
-
return this.store.getState().arrayKeys[arrayKey] ?? currentArray.map(() => this.keyGenerator.create());
|
|
999
|
-
}
|
|
1000
|
-
/**
|
|
1001
|
-
* planner가 만든 mutation이 있으면 rebase를 실행한다.
|
|
1002
|
-
*
|
|
1003
|
-
* @param mutation - 유효하지 않은 조작이면 undefined일 수 있다.
|
|
1004
|
-
*/
|
|
1005
|
-
applyMutation(mutation) {
|
|
1006
|
-
if (!mutation) {
|
|
1007
|
-
return;
|
|
1008
|
-
}
|
|
1009
|
-
this.rebase(mutation.values, mutation.keys, mutation.mapPreviousIndex);
|
|
1010
|
-
}
|
|
1011
|
-
/**
|
|
1012
|
-
* 다음 배열 값과 key 목록을 FormState 전체에 반영한다.
|
|
1013
|
-
*
|
|
1014
|
-
* @param nextArray - 변경 후 배열 값.
|
|
1015
|
-
* @param nextKeys - 변경 후 item key 목록.
|
|
1016
|
-
* @param mapPreviousIndex - 기존 child field index를 새 index로 매핑하는 함수.
|
|
1017
|
-
*/
|
|
1018
|
-
rebase(nextArray, nextKeys, mapPreviousIndex) {
|
|
1019
|
-
this.store.replaceState(
|
|
1020
|
-
() => FormArrayRebaser.rebase(this.store, this.fieldPath, nextArray, nextKeys, mapPreviousIndex)
|
|
1021
|
-
);
|
|
1022
|
-
}
|
|
1023
|
-
};
|
|
1024
|
-
|
|
1025
|
-
// src/core/array/FormArrayFactory.ts
|
|
1026
|
-
var FormArrayFactory = class {
|
|
1027
|
-
/** 생성된 array controller들이 공유할 form state store다. */
|
|
1028
|
-
store;
|
|
1029
|
-
/** 새 array item key를 만드는 공유 generator다. */
|
|
1030
|
-
keys;
|
|
1031
|
-
/**
|
|
1032
|
-
* factory를 만든다.
|
|
1033
|
-
*
|
|
1034
|
-
* @param store - array controller가 읽고 쓸 form state store.
|
|
1035
|
-
*/
|
|
1036
|
-
constructor(store) {
|
|
1037
|
-
this.store = store;
|
|
1038
|
-
this.keys = new ArrayKeyGenerator();
|
|
1039
|
-
}
|
|
1040
|
-
/**
|
|
1041
|
-
* 특정 배열 field를 제어할 FormArray 구현체를 만든다.
|
|
1042
|
-
*
|
|
1043
|
-
* @param fieldPath - 배열 field의 내부 tuple path.
|
|
1044
|
-
* @returns push/remove/move/swap/replace 명령을 가진 controller.
|
|
1045
|
-
*/
|
|
1046
|
-
create(fieldPath) {
|
|
1047
|
-
return new FormArrayController(this.store, this.keys, fieldPath);
|
|
1048
|
-
}
|
|
1049
|
-
};
|
|
1050
|
-
|
|
1051
|
-
// src/core/form/FormFieldCommands.ts
|
|
1052
|
-
var FormFieldCommands = class {
|
|
1053
|
-
/**
|
|
1054
|
-
* store는 실제 상태 변경을, validation은 변경 이후 조건부 검증을 담당한다.
|
|
1055
|
-
* 두 객체를 함께 받아야 setValue나 blur 같은 복합 명령을 한 곳에서 처리할 수 있다.
|
|
1056
|
-
*/
|
|
1057
|
-
constructor(store, validation) {
|
|
1058
|
-
this.store = store;
|
|
1059
|
-
this.validation = validation;
|
|
1060
|
-
}
|
|
1061
|
-
store;
|
|
1062
|
-
validation;
|
|
1063
|
-
/**
|
|
1064
|
-
* path를 내부 FieldPath로 정규화한 뒤 값을 쓴다.
|
|
1065
|
-
*
|
|
1066
|
-
* options.validate가 true이거나 validateOn에 change가 있으면 저장 직후 비동기 검증을 시작한다.
|
|
1067
|
-
* setValue 자체는 void API이므로 change validation의 Promise는 기다리지 않는다.
|
|
1068
|
-
*/
|
|
1069
|
-
setValue(path, value, options = {}) {
|
|
1070
|
-
const fieldKey = this.store.setValue(FormPath.toFieldPath(path), value, options);
|
|
1071
|
-
if (options.validate || this.validation.shouldValidateOn("change")) {
|
|
1072
|
-
void this.validation.validateField(fieldKey, "change");
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
/** field-local schema를 등록한다. 등록된 schema는 해당 field에서 form-level schema보다 우선한다. */
|
|
1076
|
-
registerFieldSchema(path, options) {
|
|
1077
|
-
return this.validation.registerFieldSchema(FormPath.pathInputToKey(path), options);
|
|
1078
|
-
}
|
|
1079
|
-
/**
|
|
1080
|
-
* blur 이벤트를 처리한다.
|
|
1081
|
-
*
|
|
1082
|
-
* 먼저 isFocused를 false로 바꾸고 touched를 true로 설정한다.
|
|
1083
|
-
* validateOn에 blur가 있을 때만 schema validation을 실행한다.
|
|
1084
|
-
* blur validation이 꺼져 있어도 isFocused 해제와 touched 표시는 항상 수행한다.
|
|
1085
|
-
*/
|
|
1086
|
-
async blur(path) {
|
|
1087
|
-
const fieldKey = FormPath.pathInputToKey(path);
|
|
1088
|
-
this.store.unfocusField(fieldKey);
|
|
1089
|
-
this.store.touchField(fieldKey);
|
|
1090
|
-
if (!this.validation.shouldValidateOn("blur")) {
|
|
1091
|
-
return true;
|
|
1092
|
-
}
|
|
1093
|
-
return this.validation.validateField(fieldKey, "blur");
|
|
694
|
+
*
|
|
695
|
+
* @remarks
|
|
696
|
+
* 기존 child field 상태는 재사용하지 않는다. 모든 item key도 새로 만든다.
|
|
697
|
+
*
|
|
698
|
+
* @param values - 새 배열 값.
|
|
699
|
+
*/
|
|
700
|
+
replace(values) {
|
|
701
|
+
this.applyMutation(FormArrayMutationPlanner.replace(values, values.map(() => this.keyGenerator.create())));
|
|
1094
702
|
}
|
|
1095
703
|
/**
|
|
1096
|
-
*
|
|
704
|
+
* store에서 현재 배열 값을 읽는다.
|
|
1097
705
|
*
|
|
1098
|
-
*
|
|
1099
|
-
* DOM은 focus가 다른 element로 옮겨질 때 이전 element에 blur 이벤트를 자연스럽게 발생시키므로,
|
|
1100
|
-
* 이 메서드가 다른 field를 명시적으로 unfocus 할 필요는 없다.
|
|
706
|
+
* @returns path의 값이 배열이면 그 배열, 아니면 빈 배열.
|
|
1101
707
|
*/
|
|
1102
|
-
|
|
1103
|
-
this.store.
|
|
1104
|
-
|
|
1105
|
-
/** 특정 필드의 errors 배열을 외부에서 전달받은 값으로 교체한다. */
|
|
1106
|
-
setErrors(path, errors) {
|
|
1107
|
-
this.store.setErrorsByKey(FormPath.pathInputToKey(path), errors);
|
|
708
|
+
getArray() {
|
|
709
|
+
const value = this.store.getValueAtPath(this.fieldPath);
|
|
710
|
+
return Array.isArray(value) ? value : [];
|
|
1108
711
|
}
|
|
1109
712
|
/**
|
|
1110
|
-
*
|
|
713
|
+
* 현재 배열에 대응하는 render key 목록을 읽는다.
|
|
1111
714
|
*
|
|
1112
|
-
*
|
|
1113
|
-
* paths가 있으면 해당 path들만 key로 변환해 부분적으로 비운다.
|
|
715
|
+
* @returns store에 저장된 arrayKeys가 있으면 그것을 사용하고, 없으면 현재 배열 길이만큼 새 key를 만든다.
|
|
1114
716
|
*/
|
|
1115
|
-
|
|
1116
|
-
|
|
717
|
+
getKeys() {
|
|
718
|
+
const arrayKey = FormPath.pathToKey(this.fieldPath);
|
|
719
|
+
const currentArray = this.getArray();
|
|
720
|
+
return this.store.getState().arrayKeys[arrayKey] ?? currentArray.map(() => this.keyGenerator.create());
|
|
1117
721
|
}
|
|
1118
722
|
/**
|
|
1119
|
-
*
|
|
723
|
+
* planner가 만든 mutation이 있으면 rebase를 실행한다.
|
|
1120
724
|
*
|
|
1121
|
-
*
|
|
725
|
+
* @param mutation - 유효하지 않은 조작이면 undefined일 수 있다.
|
|
1122
726
|
*/
|
|
1123
|
-
|
|
1124
|
-
if (
|
|
1125
|
-
return
|
|
727
|
+
applyMutation(mutation) {
|
|
728
|
+
if (!mutation) {
|
|
729
|
+
return;
|
|
1126
730
|
}
|
|
1127
|
-
|
|
731
|
+
this.store.replaceState(
|
|
732
|
+
(previousState) => FormArrayRebaser.rebase(previousState, this.fieldPath, mutation)
|
|
733
|
+
);
|
|
1128
734
|
}
|
|
1129
735
|
};
|
|
1130
736
|
|
|
@@ -1183,6 +789,201 @@ var FormSubmitter = class {
|
|
|
1183
789
|
}
|
|
1184
790
|
}
|
|
1185
791
|
};
|
|
792
|
+
var FormStateStore = class {
|
|
793
|
+
store;
|
|
794
|
+
/** nested defaultValues를 정규화하여 단일 store를 만든다. */
|
|
795
|
+
constructor(defaultValues) {
|
|
796
|
+
this.store = createStore(FormStateInitializer.initialize(defaultValues));
|
|
797
|
+
}
|
|
798
|
+
/** 현재 FormState snapshot을 반환한다. 반환값은 읽기 전용으로 취급한다. */
|
|
799
|
+
getState() {
|
|
800
|
+
return this.store.getState();
|
|
801
|
+
}
|
|
802
|
+
/** 상태 변경 시 호출될 listener를 등록하고 unsubscribe 함수를 반환한다. */
|
|
803
|
+
subscribe(listener) {
|
|
804
|
+
return this.store.subscribe(listener);
|
|
805
|
+
}
|
|
806
|
+
/** 이미 정규화된 key로 FieldState를 읽는다. validation처럼 key 기반으로 동작하는 곳에서 사용한다. */
|
|
807
|
+
getFieldStateByKey(fieldKey) {
|
|
808
|
+
return FormStateReader.getFieldStateByKey(this.store.getState(), fieldKey);
|
|
809
|
+
}
|
|
810
|
+
/** public path input을 받아 해당 FieldState를 읽는다. */
|
|
811
|
+
getFieldState(fieldPath) {
|
|
812
|
+
return FormStateReader.getFieldState(this.store.getState(), fieldPath);
|
|
813
|
+
}
|
|
814
|
+
/** public path input을 받아 해당 value만 읽는다. */
|
|
815
|
+
getValue(fieldPath) {
|
|
816
|
+
return FormStateReader.getValue(this.store.getState(), fieldPath);
|
|
817
|
+
}
|
|
818
|
+
/** fields와 arrayKeys로부터 전체 values 객체를 복원한다. */
|
|
819
|
+
getValues() {
|
|
820
|
+
return FormStateReader.getValues(this.store.getState());
|
|
821
|
+
}
|
|
822
|
+
/** 내부 FieldPath를 기준으로 최신 values에서 nested value를 읽는다. 배열 controller가 현재 배열 값을 얻을 때 사용한다. */
|
|
823
|
+
getValueAtPath(fieldPath) {
|
|
824
|
+
return FormStateReader.getValueAtPath(this.store.getState(), fieldPath);
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* 한 field value를 갱신한다.
|
|
828
|
+
*
|
|
829
|
+
* dirty는 defaultValues의 같은 path 값과 Object.is로 비교해 계산한다.
|
|
830
|
+
* modified는 사용자가 만든 변경(source: 'user')일 때만 true로 바꾼다.
|
|
831
|
+
* 반환한 PathKey는 이후 validation 실행에 재사용된다.
|
|
832
|
+
*/
|
|
833
|
+
setValue(fieldPath, value, options = {}) {
|
|
834
|
+
const fieldKey = FormPath.pathToKey(fieldPath);
|
|
835
|
+
const defaultValue = ValueHelper.getValueAtPath(this.store.getState().defaultValues, fieldPath);
|
|
836
|
+
this.store.setState(
|
|
837
|
+
(prevState) => produce(prevState, (draft) => {
|
|
838
|
+
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
839
|
+
draft.fields[fieldKey] = {
|
|
840
|
+
...previousField,
|
|
841
|
+
value,
|
|
842
|
+
dirty: !Object.is(value, defaultValue),
|
|
843
|
+
modified: options.source === "user" ? true : previousField.modified
|
|
844
|
+
};
|
|
845
|
+
})
|
|
846
|
+
);
|
|
847
|
+
return fieldKey;
|
|
848
|
+
}
|
|
849
|
+
/** blur된 field의 touched flag를 true로 바꾼다. 기존 field가 없으면 default state에서 시작한다. */
|
|
850
|
+
touchField(fieldKey) {
|
|
851
|
+
this.store.setState(
|
|
852
|
+
(prevState) => produce(prevState, (draft) => {
|
|
853
|
+
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
854
|
+
draft.fields[fieldKey] = {
|
|
855
|
+
...previousField,
|
|
856
|
+
touched: true
|
|
857
|
+
};
|
|
858
|
+
})
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* field의 isFocused를 true로 바꾼다. focus 이벤트에서 호출된다.
|
|
863
|
+
*
|
|
864
|
+
* fieldKey에 해당하는 field가 없으면 default state를 생성해 기록한다. 이는 touchField와 동일한 패턴이지만,
|
|
865
|
+
* 존재하지 않는 path에 대해 의도치 않게 field가 생성되는 부수 효과에 유의해야 한다.
|
|
866
|
+
*/
|
|
867
|
+
focusField(fieldKey) {
|
|
868
|
+
this.store.setState(
|
|
869
|
+
(prevState) => produce(prevState, (draft) => {
|
|
870
|
+
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
871
|
+
draft.fields[fieldKey] = {
|
|
872
|
+
...previousField,
|
|
873
|
+
isFocused: true
|
|
874
|
+
};
|
|
875
|
+
})
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* field의 isFocused를 false로 바꾼다. blur 이벤트에서 호출된다.
|
|
880
|
+
*
|
|
881
|
+
* fieldKey에 해당하는 field가 없으면 default state를 생성해 기록한다. focusField와 동일한 fallback 패턴이며,
|
|
882
|
+
* 존재하지 않는 path에 대해 의도치 않게 field가 생성되는 부수 효과에 유의해야 한다.
|
|
883
|
+
*/
|
|
884
|
+
unfocusField(fieldKey) {
|
|
885
|
+
this.store.setState(
|
|
886
|
+
(prevState) => produce(prevState, (draft) => {
|
|
887
|
+
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
888
|
+
draft.fields[fieldKey] = {
|
|
889
|
+
...previousField,
|
|
890
|
+
isFocused: false
|
|
891
|
+
};
|
|
892
|
+
})
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
/** validation 또는 외부 명령 결과로 field의 errors를 통째로 교체한다. */
|
|
896
|
+
setErrorsByKey(fieldKey, errors) {
|
|
897
|
+
this.store.setState(
|
|
898
|
+
(prevState) => produce(prevState, (draft) => {
|
|
899
|
+
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
900
|
+
draft.fields[fieldKey] = {
|
|
901
|
+
...previousField,
|
|
902
|
+
errors: [...errors]
|
|
903
|
+
};
|
|
904
|
+
})
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* errors를 비운다.
|
|
909
|
+
*
|
|
910
|
+
* fieldKeys가 있으면 해당 key들만 대상으로 하고, 없으면 현재 존재하는 모든 fields를 대상으로 한다.
|
|
911
|
+
* 존재하지 않는 key가 들어와도 default field를 만든 뒤 errors: [] 상태로 저장한다.
|
|
912
|
+
*/
|
|
913
|
+
clearErrors(fieldKeys) {
|
|
914
|
+
this.store.setState(
|
|
915
|
+
(prevState) => produce(prevState, (draft) => {
|
|
916
|
+
const keys = fieldKeys && fieldKeys.length > 0 ? fieldKeys : Object.keys(draft.fields);
|
|
917
|
+
keys.forEach((fieldKey) => {
|
|
918
|
+
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
919
|
+
draft.fields[fieldKey] = {
|
|
920
|
+
...previousField,
|
|
921
|
+
errors: []
|
|
922
|
+
};
|
|
923
|
+
});
|
|
924
|
+
})
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
/** 새 values가 있으면 그것을 새 defaultValues로 삼고, 없으면 기존 defaultValues로 FormState를 재생성한다. */
|
|
928
|
+
reset(values, options = {}) {
|
|
929
|
+
const previousState = this.store.getState();
|
|
930
|
+
const nextDefaultValues = values ?? previousState.defaultValues;
|
|
931
|
+
const initializedState = FormStateInitializer.initialize(nextDefaultValues);
|
|
932
|
+
this.store.setState(
|
|
933
|
+
produce(initializedState, (draft) => {
|
|
934
|
+
Object.entries(draft.fields).forEach(([fieldKey, nextField]) => {
|
|
935
|
+
const previousField = previousState.fields[fieldKey];
|
|
936
|
+
if (!previousField) {
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
if (options.keepDirtyValues && previousField.dirty) {
|
|
940
|
+
const fieldPath = FormPath.keyToPath(fieldKey);
|
|
941
|
+
const defaultValue = ValueHelper.getValueAtPath(nextDefaultValues, fieldPath);
|
|
942
|
+
nextField.value = previousField.value;
|
|
943
|
+
nextField.dirty = !Object.is(previousField.value, defaultValue);
|
|
944
|
+
nextField.modified = previousField.modified;
|
|
945
|
+
}
|
|
946
|
+
if (options.keepErrors) {
|
|
947
|
+
nextField.errors = [...previousField.errors];
|
|
948
|
+
}
|
|
949
|
+
if (options.keepTouched) {
|
|
950
|
+
nextField.touched = previousField.touched;
|
|
951
|
+
}
|
|
952
|
+
});
|
|
953
|
+
if (options.keepSubmitState) {
|
|
954
|
+
draft.submitCount = previousState.submitCount;
|
|
955
|
+
draft.isSubmitting = previousState.isSubmitting;
|
|
956
|
+
draft.isSubmitted = previousState.isSubmitted;
|
|
957
|
+
draft.isSubmitSuccessful = previousState.isSubmitSuccessful;
|
|
958
|
+
}
|
|
959
|
+
})
|
|
960
|
+
);
|
|
961
|
+
}
|
|
962
|
+
/** submit 시도 횟수를 증가시키고 진행 중 상태를 기록한다. */
|
|
963
|
+
beginSubmit() {
|
|
964
|
+
this.store.setState(
|
|
965
|
+
(prevState) => produce(prevState, (draft) => {
|
|
966
|
+
draft.submitCount += 1;
|
|
967
|
+
draft.isSubmitting = true;
|
|
968
|
+
draft.isSubmitSuccessful = false;
|
|
969
|
+
})
|
|
970
|
+
);
|
|
971
|
+
}
|
|
972
|
+
/** submit validation/callback 완료 후 성공 여부를 기록한다. */
|
|
973
|
+
completeSubmit(successful) {
|
|
974
|
+
this.store.setState(
|
|
975
|
+
(prevState) => produce(prevState, (draft) => {
|
|
976
|
+
draft.isSubmitting = false;
|
|
977
|
+
draft.isSubmitted = true;
|
|
978
|
+
draft.isSubmitSuccessful = successful;
|
|
979
|
+
})
|
|
980
|
+
);
|
|
981
|
+
}
|
|
982
|
+
/** 이미 완성된 FormState updater를 store에 전달한다. 배열 리베이스처럼 여러 조각을 한 번에 바꿀 때 사용한다. */
|
|
983
|
+
replaceState(updater) {
|
|
984
|
+
this.store.setState(updater);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
1186
987
|
|
|
1187
988
|
// src/core/validation/StandardSchemaValidator.ts
|
|
1188
989
|
var StandardSchemaValidator = class _StandardSchemaValidator {
|
|
@@ -1326,6 +1127,34 @@ var ValidationSequencer = class {
|
|
|
1326
1127
|
}
|
|
1327
1128
|
};
|
|
1328
1129
|
|
|
1130
|
+
// src/core/validation/ValidationSnapshot.ts
|
|
1131
|
+
var ValidationSnapshot = class _ValidationSnapshot {
|
|
1132
|
+
constructor(fieldValues, arrayKeys) {
|
|
1133
|
+
this.fieldValues = fieldValues;
|
|
1134
|
+
this.arrayKeys = arrayKeys;
|
|
1135
|
+
}
|
|
1136
|
+
fieldValues;
|
|
1137
|
+
arrayKeys;
|
|
1138
|
+
static capture(state) {
|
|
1139
|
+
return new _ValidationSnapshot(
|
|
1140
|
+
new Map(Object.entries(state.fields).map(([fieldKey, field]) => [fieldKey, field.value])),
|
|
1141
|
+
new Map(Object.entries(state.arrayKeys))
|
|
1142
|
+
);
|
|
1143
|
+
}
|
|
1144
|
+
/** Metadata and error writes do not invalidate work against unchanged values. */
|
|
1145
|
+
matches(state) {
|
|
1146
|
+
if (this.fieldValues.size !== Object.keys(state.fields).length) return false;
|
|
1147
|
+
if (this.arrayKeys.size !== Object.keys(state.arrayKeys).length) return false;
|
|
1148
|
+
for (const [fieldKey, value] of this.fieldValues) {
|
|
1149
|
+
if (!Object.is(state.fields[fieldKey]?.value, value)) return false;
|
|
1150
|
+
}
|
|
1151
|
+
for (const [fieldKey, keys] of this.arrayKeys) {
|
|
1152
|
+
if (!Object.is(state.arrayKeys[fieldKey], keys)) return false;
|
|
1153
|
+
}
|
|
1154
|
+
return true;
|
|
1155
|
+
}
|
|
1156
|
+
};
|
|
1157
|
+
|
|
1329
1158
|
// src/core/validation/ValidationEngine.ts
|
|
1330
1159
|
var ValidationEngine = class _ValidationEngine {
|
|
1331
1160
|
/** validateOn이 지정되지 않았을 때 submit에서만 자동 검증하도록 하는 기본값이다. */
|
|
@@ -1405,13 +1234,13 @@ var ValidationEngine = class _ValidationEngine {
|
|
|
1405
1234
|
return this.validateRegisteredFields("manual");
|
|
1406
1235
|
}
|
|
1407
1236
|
const token = this.sequencer.startFields(targetFieldKeys);
|
|
1408
|
-
const snapshot = this.
|
|
1237
|
+
const snapshot = ValidationSnapshot.capture(this.store.getState());
|
|
1409
1238
|
const localErrorsByKey = await this.validateFieldSchemas(targetFieldKeys);
|
|
1410
1239
|
if (this.isStale(token, snapshot)) {
|
|
1411
1240
|
return false;
|
|
1412
1241
|
}
|
|
1413
1242
|
const formSchemaFieldKeys = targetFieldKeys.filter((fieldKey) => !this.fieldSchemas.has(fieldKey));
|
|
1414
|
-
const schemaResult = formSchemaFieldKeys.length > 0 ? await this.
|
|
1243
|
+
const schemaResult = formSchemaFieldKeys.length > 0 ? await this.validateFormSchema() : _ValidationEngine.createValidSchemaResult();
|
|
1415
1244
|
if (this.isStale(token, snapshot)) {
|
|
1416
1245
|
return false;
|
|
1417
1246
|
}
|
|
@@ -1431,12 +1260,12 @@ var ValidationEngine = class _ValidationEngine {
|
|
|
1431
1260
|
}
|
|
1432
1261
|
async validateRegisteredFieldsOutcome(_trigger) {
|
|
1433
1262
|
const token = this.sequencer.startFull();
|
|
1434
|
-
const snapshot = this.
|
|
1435
|
-
const schemaResult = await this.
|
|
1263
|
+
const snapshot = ValidationSnapshot.capture(this.store.getState());
|
|
1264
|
+
const schemaResult = await this.validateFormSchema();
|
|
1436
1265
|
if (this.isStale(token, snapshot)) {
|
|
1437
1266
|
return { kind: "stale" };
|
|
1438
1267
|
}
|
|
1439
|
-
const localErrorsByKey = await this.
|
|
1268
|
+
const localErrorsByKey = await this.validateFieldSchemas([...this.fieldSchemas.keys()]);
|
|
1440
1269
|
if (this.isStale(token, snapshot)) {
|
|
1441
1270
|
return { kind: "stale" };
|
|
1442
1271
|
}
|
|
@@ -1450,33 +1279,14 @@ var ValidationEngine = class _ValidationEngine {
|
|
|
1450
1279
|
*
|
|
1451
1280
|
* @returns schema가 없으면 성공 결과, 있으면 schema validation 결과.
|
|
1452
1281
|
*/
|
|
1453
|
-
|
|
1282
|
+
validateFormSchema() {
|
|
1454
1283
|
if (!this.schema) {
|
|
1455
1284
|
return Promise.resolve(_ValidationEngine.createValidSchemaResult());
|
|
1456
1285
|
}
|
|
1457
1286
|
return this.schema.validate(this.store.getValues());
|
|
1458
1287
|
}
|
|
1459
|
-
captureValueSnapshot() {
|
|
1460
|
-
const state = this.store.getState();
|
|
1461
|
-
return {
|
|
1462
|
-
arrayKeys: new Map(Object.entries(state.arrayKeys)),
|
|
1463
|
-
fieldValues: new Map(
|
|
1464
|
-
Object.entries(state.fields).map(([fieldKey, field]) => [fieldKey, field.value])
|
|
1465
|
-
)
|
|
1466
|
-
};
|
|
1467
|
-
}
|
|
1468
1288
|
isStale(token, snapshot) {
|
|
1469
|
-
|
|
1470
|
-
const state = this.store.getState();
|
|
1471
|
-
if (snapshot.fieldValues.size !== Object.keys(state.fields).length) return true;
|
|
1472
|
-
if (snapshot.arrayKeys.size !== Object.keys(state.arrayKeys).length) return true;
|
|
1473
|
-
for (const [fieldKey, value] of snapshot.fieldValues) {
|
|
1474
|
-
if (!Object.is(state.fields[fieldKey]?.value, value)) return true;
|
|
1475
|
-
}
|
|
1476
|
-
for (const [fieldKey, keys] of snapshot.arrayKeys) {
|
|
1477
|
-
if (!Object.is(state.arrayKeys[fieldKey], keys)) return true;
|
|
1478
|
-
}
|
|
1479
|
-
return false;
|
|
1289
|
+
return this.sequencer.isStale(token) || !snapshot.matches(this.store.getState());
|
|
1480
1290
|
}
|
|
1481
1291
|
/**
|
|
1482
1292
|
* validation 결과를 store에 기록한다.
|
|
@@ -1508,10 +1318,6 @@ var ValidationEngine = class _ValidationEngine {
|
|
|
1508
1318
|
}));
|
|
1509
1319
|
return errorsByKey;
|
|
1510
1320
|
}
|
|
1511
|
-
/** 등록된 모든 field-local schema를 검증한다. */
|
|
1512
|
-
validateRegisteredFieldSchemas() {
|
|
1513
|
-
return this.validateFieldSchemas([...this.fieldSchemas.keys()]);
|
|
1514
|
-
}
|
|
1515
1321
|
/**
|
|
1516
1322
|
* form-level schema 결과에 field-local schema 결과를 덮어쓴다.
|
|
1517
1323
|
*
|
|
@@ -1564,10 +1370,10 @@ var ValidationEngine = class _ValidationEngine {
|
|
|
1564
1370
|
var CreateForm = class {
|
|
1565
1371
|
/** form의 단일 상태 저장소. 모든 값/필드 메타데이터/배열 키는 이 store를 통해 읽고 쓴다. */
|
|
1566
1372
|
store;
|
|
1567
|
-
/**
|
|
1568
|
-
|
|
1569
|
-
/**
|
|
1570
|
-
|
|
1373
|
+
/** 필드 명령 뒤에 실행할 검증과 field-local schema 등록을 담당한다. */
|
|
1374
|
+
validation;
|
|
1375
|
+
/** 같은 form의 모든 배열 controller가 공유하는 item key 생성기. */
|
|
1376
|
+
arrayKeys = new ArrayKeyGenerator();
|
|
1571
1377
|
/** submitCount 증가, submit validation, onValid/onInvalid 호출을 담당한다. */
|
|
1572
1378
|
submitter;
|
|
1573
1379
|
/**
|
|
@@ -1577,10 +1383,8 @@ var CreateForm = class {
|
|
|
1577
1383
|
*/
|
|
1578
1384
|
constructor(options) {
|
|
1579
1385
|
this.store = new FormStateStore(options.defaultValues);
|
|
1580
|
-
|
|
1581
|
-
this.
|
|
1582
|
-
this.arrays = new FormArrayFactory(this.store);
|
|
1583
|
-
this.submitter = new FormSubmitter(this.store, validation);
|
|
1386
|
+
this.validation = new ValidationEngine(this.store, options);
|
|
1387
|
+
this.submitter = new FormSubmitter(this.store, this.validation);
|
|
1584
1388
|
}
|
|
1585
1389
|
/** 현재 form snapshot을 그대로 반환한다. 값, 필드 메타, submitCount, arrayKeys를 모두 포함한다. */
|
|
1586
1390
|
getState() {
|
|
@@ -1592,7 +1396,7 @@ var CreateForm = class {
|
|
|
1592
1396
|
}
|
|
1593
1397
|
/** field-local schema를 validation engine에 등록하고 cleanup 함수를 반환한다. */
|
|
1594
1398
|
registerFieldSchema(path, options) {
|
|
1595
|
-
return this.
|
|
1399
|
+
return this.validation.registerFieldSchema(FormPath.pathInputToKey(path), options);
|
|
1596
1400
|
}
|
|
1597
1401
|
/** path에 해당하는 FieldState를 반환한다. 없는 필드는 읽기용 기본 상태를 만들어 반환한다. */
|
|
1598
1402
|
getFieldState(path) {
|
|
@@ -1608,31 +1412,43 @@ var CreateForm = class {
|
|
|
1608
1412
|
}
|
|
1609
1413
|
/** 한 필드의 값을 변경하고, 옵션 또는 validateOn 설정에 따라 change validation을 실행한다. */
|
|
1610
1414
|
setValue(path, value, options = {}) {
|
|
1611
|
-
this.
|
|
1415
|
+
const fieldKey = this.store.setValue(FormPath.toFieldPath(path), value, options);
|
|
1416
|
+
if (options.validate || this.validation.shouldValidateOn("change")) {
|
|
1417
|
+
void this.validation.validateField(fieldKey, "change");
|
|
1418
|
+
}
|
|
1612
1419
|
}
|
|
1613
1420
|
/** 한 필드를 touched 처리하고, validateOn에 blur가 포함되어 있으면 schema validation을 실행한다. */
|
|
1614
|
-
blur(path) {
|
|
1615
|
-
|
|
1421
|
+
async blur(path) {
|
|
1422
|
+
const fieldKey = FormPath.pathInputToKey(path);
|
|
1423
|
+
this.store.unfocusField(fieldKey);
|
|
1424
|
+
this.store.touchField(fieldKey);
|
|
1425
|
+
if (!this.validation.shouldValidateOn("blur")) {
|
|
1426
|
+
return true;
|
|
1427
|
+
}
|
|
1428
|
+
return this.validation.validateField(fieldKey, "blur");
|
|
1616
1429
|
}
|
|
1617
1430
|
/** field의 isFocused를 true로 바꾼다. focus 이벤트에서 호출된다. */
|
|
1618
1431
|
focus(path) {
|
|
1619
|
-
this.
|
|
1432
|
+
this.store.focusField(FormPath.pathInputToKey(path));
|
|
1620
1433
|
}
|
|
1621
1434
|
/** 외부에서 직접 특정 필드의 error 목록을 교체할 때 사용한다. */
|
|
1622
1435
|
setErrors(path, errors) {
|
|
1623
|
-
this.
|
|
1436
|
+
this.store.setErrorsByKey(FormPath.pathInputToKey(path), errors);
|
|
1624
1437
|
}
|
|
1625
1438
|
/** 지정한 필드들의 errors를 비우거나, path가 없으면 모든 필드 errors를 비운다. */
|
|
1626
1439
|
clearErrors(...paths) {
|
|
1627
|
-
this.
|
|
1440
|
+
this.store.clearErrors(paths.length > 0 ? paths.map((path) => FormPath.pathInputToKey(path)) : void 0);
|
|
1628
1441
|
}
|
|
1629
1442
|
/** schema validation을 수동 실행한다. path가 있으면 해당 필드 errors만 갱신한다. */
|
|
1630
1443
|
trigger(...paths) {
|
|
1631
|
-
|
|
1444
|
+
if (paths.length === 0) {
|
|
1445
|
+
return this.validation.validateRegisteredFields("manual");
|
|
1446
|
+
}
|
|
1447
|
+
return this.validation.validateFields(paths.map((path) => FormPath.pathInputToKey(path)), "manual");
|
|
1632
1448
|
}
|
|
1633
1449
|
/** 배열 필드를 push/remove/move/swap/replace할 수 있는 전용 명령 객체를 반환한다. */
|
|
1634
1450
|
array(path) {
|
|
1635
|
-
return this.
|
|
1451
|
+
return new FormArrayController(this.store, this.arrayKeys, FormPath.toFieldPath(path));
|
|
1636
1452
|
}
|
|
1637
1453
|
/** form을 defaultValues 또는 새 values 기준으로 초기 상태로 되돌린다. */
|
|
1638
1454
|
reset(values, options) {
|
|
@@ -1645,5 +1461,5 @@ var CreateForm = class {
|
|
|
1645
1461
|
};
|
|
1646
1462
|
|
|
1647
1463
|
export { CreateForm };
|
|
1648
|
-
//# sourceMappingURL=chunk-
|
|
1649
|
-
//# sourceMappingURL=chunk-
|
|
1464
|
+
//# sourceMappingURL=chunk-4F4SAGQ5.js.map
|
|
1465
|
+
//# sourceMappingURL=chunk-4F4SAGQ5.js.map
|