Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: break out of all loops on first focus #11827

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/__tests__/logic/iterateFieldsByAction.test.ts
Expand Up @@ -64,4 +64,145 @@ describe('focusFieldBy', () => {
);
}).not.toThrow();
});

it('should focus on the first error it encounter and not the second', () => {
const focus = jest.fn();
iterateFieldsByAction(
{
first: {
_f: {
name: 'first',
ref: {
name: 'first',
focus,
},
},
},
second: {
_f: {
name: 'second',
ref: {
name: 'second',
focus,
},
},
},
},
(ref) => {
// @ts-ignore
ref.focus && ref.focus(ref.name);
return 1;
},
);
expect(focus).toBeCalledWith('first');
expect(focus).not.toBeCalledWith('second');
});
DPflasterer marked this conversation as resolved.
Show resolved Hide resolved

it('should recursively drill into objects', () => {
const focus = jest.fn();
iterateFieldsByAction(
{
test: {
// @ts-ignore
name: {
first: {
_f: {
name: 'name.first',
ref: {
name: 'first',
focus,
},
},
},
last: {
_f: {
name: 'name.last',
ref: {
name: 'last',
focus,
},
},
},
},
},
},
(ref, key) => {
if (key === 'name.last') {
// @ts-ignore
ref.focus && ref.focus(ref.name);
return 1;
}
return;
},
);
expect(focus).not.toBeCalledWith('first');
expect(focus).toBeCalledWith('last');
});
DPflasterer marked this conversation as resolved.
Show resolved Hide resolved

it('should should recursively drill into objects and break out of all loops on first focus', () => {
const focus = jest.fn();
const notFocus = jest.fn();
iterateFieldsByAction(
{
personal: {
// @ts-ignore
name: {
first: {
_f: {
name: 'name.first',
ref: {
name: 'first',
focus: notFocus,
},
},
},
last: {
_f: {
name: 'name.last',
ref: {
name: 'last',
focus,
},
},
},
},
phone: {
_f: {
name: 'phone',
ref: {
name: 'phone',
focus: notFocus,
},
},
},
address: {
line1: {
_f: {
name: 'address.line1',
ref: {
name: 'line1',
focus: notFocus,
},
},
},
},
},
},
(ref, key) => {
// @ts-ignore
ref.focus && ref.focus(ref.name);
return key === 'name.last' ? 1 : undefined;
},
);
// 'focus' should be called on 'last' and never again
expect(focus).not.toBeCalledWith('first'); // not valid
expect(focus).toBeCalledWith('last'); // valid
expect(focus).not.toBeCalledWith('phone'); // stopped
expect(focus).not.toBeCalledWith('line1');
// 'notFocus' should be called on the first, then never again
expect(notFocus).toBeCalledWith('first'); // not valid
expect(notFocus).not.toBeCalledWith('last'); // valid
expect(notFocus).not.toBeCalledWith('phone'); // stopped
expect(notFocus).not.toBeCalledWith('line1');
});
});
14 changes: 9 additions & 5 deletions src/logic/iterateFieldsByAction.ts
Expand Up @@ -16,17 +16,21 @@ const iterateFieldsByAction = (

if (_f) {
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
break;
return true;
} else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
break;
return true;
} else {
iterateFieldsByAction(currentField, action);
if (iterateFieldsByAction(currentField, action)) {
break;
}
}
} else if (isObject(currentField)) {
iterateFieldsByAction(currentField, action);
if (iterateFieldsByAction(currentField, action)) {
break;
}
}
}
}
return;
};

export default iterateFieldsByAction;