66
77import { assign , isArray , isEqual , isNil , toArray } from 'skyroc-utils' ;
88
9- import type { FieldEntity } from '../types/field' ;
10- import type { Callbacks , Store , StoreValue } from '../types/formStore' ;
11- import type { Meta } from '../types/shared-types' ;
129import { get } from '../utils/get' ;
1310import { set , unset } from '../utils/set' ;
1411import type { NamePath , PathTuple } from '../utils/util' ;
@@ -17,7 +14,8 @@ import { anyOn, collectDeepKeys, isOn, isUnderPrefix, keyOfName, microtask } fro
1714import { type ChangeMask , ChangeTag } from './event' ;
1815import type { Action , ArrayOpArgs , Middleware , ValidateFieldsOptions } from './middleware' ;
1916import { compose } from './middleware' ;
20- import type { ValidateMessages } from './types' ;
17+ import type { Callbacks , FieldEntity , Meta , Store , StoreValue } from './types' ;
18+ import type { ValidateMessages } from './validate' ;
2119import { runRulesWithMode } from './validation' ;
2220import type { Rule , ValidateOptions } from './validation' ;
2321
@@ -34,6 +32,9 @@ export type ArrayField = {
3432 keys : number [ ] ;
3533} ;
3634
35+ /**
36+ * Checks if a validation rule should be triggered based on the provided trigger
37+ */
3738const matchTrigger = ( rule : Rule , trig ?: string | string [ ] ) => {
3839 const list = toArray ( rule . validateTrigger ) ;
3940
@@ -44,12 +45,18 @@ const matchTrigger = (rule: Rule, trig?: string | string[]) => {
4445 return trigList . some ( t => list . includes ( t ) ) ;
4546} ;
4647
48+ /**
49+ * Extracts values from a Map based on provided field names
50+ */
4751function getValueByNames < T > ( source : Map < string , T > , names ?: NamePath [ ] ) : Record < string , T > {
4852 const keys = names ?. length ? names . map ( keyOfName ) : Array . from ( source . keys ( ) ) ;
4953
5054 return Object . fromEntries ( keys . map ( k => [ k , source . get ( k ) ! ] ) ) ;
5155}
5256
57+ /**
58+ * Checks if any field in the provided names has a flag set in the bucket
59+ */
5360function getFlag ( bucket : Set < string > , names ?: NamePath [ ] ) {
5461 if ( ! names || names . length === 0 ) {
5562 return bucket . size > 0 ;
@@ -58,6 +65,9 @@ function getFlag(bucket: Set<string>, names?: NamePath[]) {
5865 return anyOn ( bucket , names ) ;
5966}
6067
68+ /**
69+ * Moves an array element from one index to another
70+ */
6171function move < T > ( arr : T [ ] , from : number , to : number ) : T [ ] {
6272 const clone = arr . slice ( ) ;
6373 const item = clone . splice ( from , 1 ) [ 0 ] ;
@@ -210,9 +220,7 @@ class FormStore {
210220 private rebindMiddlewares = ( ) => {
211221 const api = {
212222 dispatch : ( a : Action ) => this . dispatch ( a ) ,
213- // eslint-disable-next-line sort/object-properties
214- getState : ( ) => this . _store ,
215- getFields : this . getFields
223+ getState : ( ) => this . _store
216224 } ;
217225
218226 const chain = this . _middlewares . map ( m => m ( api ) ) ;
@@ -245,11 +253,11 @@ class FormStore {
245253 this . arrayOp ( a . name , a . args ) ;
246254 break ;
247255 case 'setExternalErrors' : {
248- const { entries } = a as any ;
256+ const { entries } = a ;
249257
250258 this . transaction ( ( ) => {
251259 if ( entries . length === 0 ) {
252- // ✅ 空数组代表全通过,清空所有错误
260+ // ✅ Empty array means all validation passed, clear all errors
253261 this . _errors . clear ( ) ;
254262 this . enqueueNotify (
255263 Array . from ( this . _fieldEntities , e => e . name as string ) ,
@@ -983,6 +991,9 @@ class FormStore {
983991
984992 // ===== Array Operation =====
985993
994+ /**
995+ * Gets or creates an array key manager for tracking array field keys
996+ */
986997 private getArrayKeyManager = ( name : string ) => {
987998 let mgr = this . arrayKeyMap . get ( name ) ;
988999 if ( ! mgr ) {
@@ -992,6 +1003,9 @@ class FormStore {
9921003 return mgr ;
9931004 } ;
9941005
1006+ /**
1007+ * Performs array operations on form fields (insert, remove, move, swap, replace)
1008+ */
9951009 private arrayOp ( name : NamePath , args : ArrayOpArgs ) : void {
9961010 const arr = this . getFieldValue ( name ) ;
9971011 if ( ! Array . isArray ( arr ) ) return ;
@@ -1077,6 +1091,10 @@ class FormStore {
10771091 } ;
10781092
10791093 // ===== FieldChange =====
1094+ /**
1095+ * Triggers the onFieldsChange callback for specified fields
1096+ * @param nameList - Array of field names that have changed
1097+ */
10801098 triggerOnFieldsChange = ( nameList : NamePath [ ] ) => {
10811099 if ( this . _callbacks ?. onFieldsChange ) {
10821100 const fields = this . getFields ( ) ;
@@ -1091,10 +1109,16 @@ class FormStore {
10911109
10921110 // ===== Submit =====
10931111
1112+ /**
1113+ * Registers a pre-submit transform function
1114+ */
10941115 usePreSubmit ( fn : ( values : Store ) => Store ) {
10951116 this . _preSubmit . push ( fn ) ;
10961117 }
10971118
1119+ /**
1120+ * Removes disabled and hidden fields from values before submission
1121+ */
10981122 private _pruneForSubmit ( values : Store ) : Store {
10991123 const disabled = Array . from ( this . _disabledKeys ) ;
11001124 const hidden = Array . from ( this . _hiddenKeys ) ;
@@ -1119,6 +1143,10 @@ class FormStore {
11191143 return walk ( values , [ ] ) ?? { } ;
11201144 }
11211145
1146+ /**
1147+ * Builds the payload for failed form submission
1148+ * @returns Object containing error information and form state
1149+ */
11221150 private buildFailedPayload = ( ) => {
11231151 const errorMap = Object . fromEntries ( this . _errors ) ;
11241152 const warningMap = Object . fromEntries ( this . _warnings ) ;
@@ -1148,13 +1176,20 @@ class FormStore {
11481176 } ;
11491177 } ;
11501178
1179+ /**
1180+ * Submits the form after validation
1181+ * Calls onFinish if validation passes, onFinishFailed if it fails
1182+ */
11511183 private submit = ( ) => {
11521184 this . validateFields ( ) . then ( ok => {
11531185 if ( ok ) this . _callbacks . onFinish ?.( this . _store ) ;
11541186 else this . _callbacks . onFinishFailed ?.( this . buildFailedPayload ( ) ) ;
11551187 } ) ;
11561188 } ;
11571189
1190+ /**
1191+ * Destroys the form and cleans up all state
1192+ */
11581193 private destroyForm = ( clearOnDestroy ?: boolean ) => {
11591194 if ( clearOnDestroy ) {
11601195 // destroy form reset store
0 commit comments