Skip to content

fix(renderer): fix undefined meta in condition #1234

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

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/react-form-renderer/src/condition/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ const Condition = React.memo(
* We have to get the meta in the timetout to wait for state initialization
*/
const meta = formOptions.getFieldState(field.name);
const isFormModified = Object.values(formOptions.getState().modified).some(Boolean);
/**
* Apply setter only on modfied fields or on fields with no initial value.
* Apply setter only
* - field has no initial value
* - form is modified
* - when meta is false = field was unmounted before timeout, we finish the condition
*/
if (typeof meta.initial === 'undefined' || meta.modified) {
if (!meta || isFormModified || typeof meta.initial === 'undefined') {
formOptions.batch(() => {
Object.entries(setter).forEach(([name, value]) => {
formOptions.change(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ describe('condition test', () => {
expect(() => screen.getByLabelText('field-2')).toThrow();
});

it('should handle nested complex coniditions', async () => {
it('should handle nested complex conditions', async () => {
const schema = {
fields: [
{
Expand Down Expand Up @@ -521,6 +521,45 @@ describe('condition test', () => {
await waitFor(() => expect(screen.getByLabelText('info.occupation')).toHaveValue('SPY'));
});

it('should change field with initial value only when form is modified', async () => {
const schema = {
fields: [
{
component: 'text-field',
name: 'field1',
initialValue: 'B',
},
{
component: 'text-field',
name: 'field2',
initialValue: 'schema initial value',
clearOnUnmount: true,
condition: {
when: 'field1',
is: 'B',
then: { set: { field2: 'set with then' } },
else: { set: { field2: 'set with else' } },
},
},
],
};

render(<FormRenderer {...initialProps} schema={schema} />);

expect(screen.getByLabelText('field2')).toHaveValue('schema initial value');

userEvent.type(screen.getByLabelText('field2'), '+++');

expect(screen.getByLabelText('field2')).toHaveValue('schema initial value+++');

userEvent.clear(screen.getByLabelText('field1'));
userEvent.type(screen.getByLabelText('field1'), 'A');
userEvent.clear(screen.getByLabelText('field1'));
userEvent.type(screen.getByLabelText('field1'), 'B');

await waitFor(() => expect(screen.getByLabelText('field2')).toHaveValue('set with then'));
});

describe('reducer', () => {
it('returns default', () => {
const initialState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ condition: {

Set is a object consists of field names as keys and values as values. You can change any form field value from any conditional action.

When the field containing a condition has some defined initial value, the setter is not triggered until the setter is retriggered with a different value.

</DocPage>