To achieve conditional visibility of fields within your data model in Builder.io, you can use field dependencies. Specifically, you can configure the modalOptions
fields to appear only when the hasModal
boolean is set to true. This enhances the user experience by only showing relevant fields when needed.
Step-by-Step Guide
Here’s a step-by-step guide to set this up in Builder.io:
-
Go to Your Data Model:
- Navigate to the Models section in your Builder.io dashboard.
- Select the data model you want to edit, or create a new one if necessary.
-
Add the hasModal
Field:
- Add a boolean field named
hasModal
.
-
Add the modalOptions
Fields:
- Add the fields you want within the
modalOptions
object. Each of these fields will be configured to only show if hasModal
is true.
-
Set Field Dependencies:
- Use the
showIf
property to conditionally display the modalOptions
fields based on the value of hasModal
.
Example Configuration
Here’s an example configuration that you can use to set up your data model:
Step 1: Add the hasModal
Field:
{
name: 'hasModal',
type: 'boolean',
defaultValue: false,
helperText: 'Toggle this to set modal options',
}
Step 2: Add modalOptions
Fields with Dependencies:
Each field within the modalOptions
object should have a showIf
property configured to check the value of hasModal
.
{
name: 'modalOptionsTitle',
type: 'string',
helperText: 'Title of the modal',
showIf: {
'hasModal': true
}
},
{
name: 'modalOptionsDescription',
type: 'string',
helperText: 'Description of the modal',
showIf: {
'hasModal': true
}
}
Full Example Registration
You can register your component with these fields using Builder.registerComponent
and ensuring the conditional visibility logic is applied:
import { Builder } from '@builder.io/react';
const MyComponent = (props) => {
// Your component logic here
return (
<div>
<h1>My Component</h1>
{props.hasModal && (
<div>
<h2>{props.modalOptionsTitle}</h2>
<p>{props.modalOptionsDescription}</p>
</div>
)}
</div>
);
};
Builder.registerComponent(MyComponent, {
name: 'MyComponent',
inputs: [
{
name: 'hasModal',
type: 'boolean',
defaultValue: false,
helperText: 'Toggle this to set modal options',
},
{
name: 'modalOptionsTitle',
type: 'string',
helperText: 'Title of the modal',
showIf: {
'hasModal': true,
},
},
{
name: 'modalOptionsDescription',
type: 'string',
helperText: 'Description of the modal',
showIf: {
'hasModal': true,
},
},
],
});
Verify in the Visual Editor
-
Open the Content Editor:
- Navigate to the content entry for your data model in Builder.io.
-
Toggle the hasModal
Field:
- Toggle the
hasModal
field to true
and verify that the modalOptions
fields (modalOptionsTitle
and modalOptionsDescription
) appear.
- Toggle the
hasModal
field to false
and verify that the modalOptions
fields disappear.
Summary
By configuring conditional field visibility using the showIf
property, you can ensure that modalOptions
fields are only displayed when hasModal
is set to true. This provides a cleaner and more intuitive user experience for those interacting with the data model in Builder.io.