Skip to content

Commit 6fdc329

Browse files
committed
fix(renderer): fix undefined meta in condition
1 parent bb71375 commit 6fdc329

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

packages/react-form-renderer/src/condition/condition.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ const Condition = React.memo(
5151
* We have to get the meta in the timetout to wait for state initialization
5252
*/
5353
const meta = formOptions.getFieldState(field.name);
54+
const isFormModified = Object.values(formOptions.getState().modified).some(Boolean);
5455
/**
55-
* Apply setter only on modfied fields or on fields with no initial value.
56+
* Apply setter only
57+
* - field has no initial value
58+
* - form is modified
59+
* - when meta is false = field was unmounted before timeout, we finish the condition
5660
*/
57-
if (typeof meta.initial === 'undefined' || meta.modified) {
61+
if (!meta || isFormModified || typeof meta.initial === 'undefined') {
5862
formOptions.batch(() => {
5963
Object.entries(setter).forEach(([name, value]) => {
6064
formOptions.change(name, value);

packages/react-form-renderer/src/tests/form-renderer/condition.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ describe('condition test', () => {
468468
expect(() => screen.getByLabelText('field-2')).toThrow();
469469
});
470470

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

524+
it('should change field with initial value only when form is modified', async () => {
525+
const schema = {
526+
fields: [
527+
{
528+
component: 'text-field',
529+
name: 'field1',
530+
initialValue: 'B',
531+
},
532+
{
533+
component: 'text-field',
534+
name: 'field2',
535+
initialValue: 'schema initial value',
536+
clearOnUnmount: true,
537+
condition: {
538+
when: 'field1',
539+
is: 'B',
540+
then: { set: { field2: 'set with then' } },
541+
else: { set: { field2: 'set with else' } },
542+
},
543+
},
544+
],
545+
};
546+
547+
render(<FormRenderer {...initialProps} schema={schema} />);
548+
549+
expect(screen.getByLabelText('field2')).toHaveValue('schema initial value');
550+
551+
userEvent.type(screen.getByLabelText('field2'), '+++');
552+
553+
expect(screen.getByLabelText('field2')).toHaveValue('schema initial value+++');
554+
555+
userEvent.clear(screen.getByLabelText('field1'));
556+
userEvent.type(screen.getByLabelText('field1'), 'A');
557+
userEvent.clear(screen.getByLabelText('field1'));
558+
userEvent.type(screen.getByLabelText('field1'), 'B');
559+
560+
await waitFor(() => expect(screen.getByLabelText('field2')).toHaveValue('set with then'));
561+
});
562+
524563
describe('reducer', () => {
525564
it('returns default', () => {
526565
const initialState = {

packages/react-renderer-demo/src/pages/schema/condition-set.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ condition: {
2020

2121
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.
2222

23+
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.
24+
2325
</DocPage>

0 commit comments

Comments
 (0)