File size: 1,052 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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;
|