Contact Form Example
Building a contact form with React JSONR
This example demonstrates how to create a contact form using React JSONR. We'll build a form with various input types and proper event handling.
Complete Example
Here's the full implementation:
Result
The code above would render a fully functional contact form like this:
- A form titled "Contact Us"
- Input fields for name, email, and a textarea for the message
- A submit button that shows an alert when clicked
- A disclaimer text below the form
- Console logs when input values change
All input elements have automatically generated IDs thanks to our AutoIdPlugin
.
Key Points
- JSON Structure: The form is defined entirely in JSON, making it easy to modify or generate dynamically
- Event Handling: Form submission and input changes are handled via named event handlers
- Plugin Usage: The
AutoIdPlugin
adds IDs to input elements, demonstrating how plugins can enhance the JSON - HTML Elements: Native HTML elements like
div
andp
are registered alongside custom components
Extended Use Cases
This pattern is particularly useful for:
- CMS-driven forms: Store form definitions in a CMS and render them dynamically
- A/B testing: Easily swap between different form layouts without changing code
- Dynamic forms: Generate form fields based on user data or application state