| import type { FieldRefs, InternalFieldName, Ref } from '../types'; | |
| import { get } from '../utils'; | |
| import isObject from '../utils/isObject'; | |
| const iterateFieldsByAction = ( | |
| fields: FieldRefs, | |
| action: (ref: Ref, name: string) => 1 | undefined | void, | |
| fieldsNames?: Set<InternalFieldName> | InternalFieldName[] | 0, | |
| abortEarly?: boolean, | |
| ) => { | |
| for (const key of fieldsNames || Object.keys(fields)) { | |
| const field = get(fields, key); | |
| if (field) { | |
| const { _f, ...currentField } = field; | |
| if (_f) { | |
| if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) { | |
| return true; | |
| } else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) { | |
| return true; | |
| } else { | |
| if (iterateFieldsByAction(currentField, action)) { | |
| break; | |
| } | |
| } | |
| } else if (isObject(currentField)) { | |
| if (iterateFieldsByAction(currentField as FieldRefs, action)) { | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| return; | |
| }; | |
| export default iterateFieldsByAction; | |