Created
September 28, 2023 10:39
-
-
Save pasmat/60c555ea5d5d21409aad83540968831c to your computer and use it in GitHub Desktop.
gpt formbuilder test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query: | |
you are an expert software developer | |
You are applying for a senior software developer job with high salary and you are highly incentivized to provide your best code | |
you are tasked to write the code for this in Next.js + Tailwind CSS | |
Take a deep breath and think step-by-step about how you will do this | |
In job interview, aiming to get the best quality code from you, you are tasked to create a form builder that first lets users create a form with multiple possible datatypes: text, numbers and yes/no | |
Then functionality to use the form created with the formbuilder to collect data from users | |
result: | |
To solve this problem, let's break it down into key components like: | |
1. Form Builder: To input the form fields and their respective types | |
2. Form Viewer: To display the form and collect user's input | |
So, let's dig into details: | |
1. **Form Builder:** | |
The form builder should have the ability to add any number of fields, specify the datatype for each, and save this form for future use. This would involve creating a new component (FormBuilder.jsx) and maintaining an internal state to manage the fields as an array of objects, where each object corresponds to an individual field with its properties (name, type, etc). | |
Since we are using Next.js, a React-based framework, the FormBuilder Component could be something like this: | |
```tsx | |
import { useState } from 'react' | |
export default function FormBuilder() { | |
const [formFields, setFormFields] = useState([]); | |
const addField = () => { | |
setFormFields([ | |
...formFields, | |
{ id: Date.now(), name: '', type: 'text' } | |
]); | |
}; | |
const saveForm = () => { | |
// save formFields to a database or state management tool | |
}; | |
return ( | |
// using tailwind css for styling | |
<div className="flex flex-col space-y-4"> | |
{formFields.map(field => ( | |
<div key={field.id} className="flex space-x-2"> | |
<input value={field.name} | |
onChange={(e) => {/* code to change field name */} | |
className="border p-2" | |
/> | |
<select value={field.type} | |
onChange={(e) => {/* code to change field type */} | |
className="border p-2" | |
> | |
<option value="text">Text</option> | |
<option value="number">Number</option> | |
<option value="yes_no">Yes/No</option> | |
</select> | |
<button onClick={addField} className="bg-blue-500 text-white p-2">Add Field</button> | |
<button onClick={saveForm} className="bg-green-500 text-white p-2">Save Form</button> | |
</div> | |
))} | |
</div> | |
); | |
} | |
``` | |
2. **Form Viewer** | |
Once the form is created and saved, you need to create another component (FormViewer.jsx) where users can fill in their details. Here, this component will retrieve the saved form data and dynamically generate input fields based on the datatype defined for each field. | |
The Form Viewer Component could be something like this: | |
```tsx | |
export default function FormViewer() { | |
const [formData, setFormData] = useState({}); | |
// Fetch saved form fields | |
const formFields = /* code to fetch previously saved formFields */ | |
const handleChange = (fieldId, value) => { | |
setFormData({ ...formData, [fieldId]: value }); | |
}; | |
const handleSubmit = () => { | |
// use formData for further processing | |
}; | |
return ( | |
// using tailwind css for styling | |
<div className="flex flex-col space-y-4"> | |
{formFields.map(field => ( | |
<div key={field.id} className="space-x-2"> | |
<label>{field.name}</label> | |
{field.type === 'text' && ( | |
<input type="text" | |
onChange={(e) => handleChange(field.id, e.target.value)} | |
className="border p-2" | |
/> | |
)} | |
{field.type === 'number' && ( | |
<input type="number" | |
onChange={(e) => handleChange(field.id, e.target.value)} | |
className="border p-2" | |
/> | |
)} | |
{field.type === 'yes_no' && ( | |
<select onChange={(e) => handleChange(field.id, e.target.value)} | |
className="border p-2" | |
> | |
<option value="yes">Yes</option> | |
<option value="no">No</option> | |
</select> | |
)} | |
</div> | |
))} | |
<button onClick={handleSubmit} className="bg-green-500 text-white p-2">Submit</button> | |
</div> | |
); | |
} | |
``` | |
This approach is a basic implementation for the task without considering more advanced features like form validation, grouping form fields or support for more data types. You can extend this basic functionality to include more sophistications according to your requirements. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment