Skip to main content

Conditional render

Conditional render utility is a useful tool when you want to show additional parts of the form based on specific value in specific field or even more fields.

type FieldStructure = {
// ... rest of the every field props,
conditionalRender?: {
conditionalFields: ConditionalType[];
conditionalRulesChainOption?: 'AND' | 'OR';
};
}
interface ConditionalType extends FieldTrackerInterface {
comparisonMethod: (value: any) => boolean;
}
interface FieldTrackerInterface {
fieldName: string;
useSamePath?: boolean;
}

Conditional render based on one field value

Live Editor
Result

Conditional render with useSamePath property

useSamePath property is allowing to only put the field name inside the fieldName property without replicating the whole path to that field. You can spot the difference inside the schema.

It is super useful inside nested sections or array fields, since you can easily point to the nearest sibling field.

Live Editor
Result

Conditional render based on more fields value

By default all the comparison method result are check with 'AND' option based on array method .every. Therefor to handle more fields in conditional render, all of them have to match the values to show the field to the user.

Live Editor
Result

Conditional render based on more fields value with 'OR' logical check

There is an option to change default behaviour presented in the previous example. Additional parameter called conditionalRulesChainOption can change the testing method behind the scene from .every to .some so if one of the values match, the field would be rendered.

Live Editor
Result