Created
March 27, 2023 01:51
-
-
Save ethereumdegen/4da51ec85d357ee040d6540a566095c1 to your computer and use it in GitHub Desktop.
react-tailwind-autoform
This file contains 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
import { useState } from 'react'; | |
import { observer } from "mobx-react"; | |
function AutoForm({ architecture, onSubmit }) { | |
const [formData, setFormData] = useState({}); | |
const handleChange = (event) => { | |
setFormData({ | |
...formData, | |
[event.target.name]: event.target.value, | |
}); | |
}; | |
const handleSubmit = (event) => { | |
event.preventDefault(); | |
onSubmit(formData); | |
}; | |
return ( | |
<form onSubmit={handleSubmit}> | |
{architecture.fields.map((field) => ( | |
<div key={field.name} className="mb-4"> | |
<label htmlFor={field.name} className="block text-gray-700 font-bold mb-2"> | |
{field.label} | |
</label> | |
{field.type === 'text' && ( | |
<input | |
type="text" | |
name={field.name} | |
id={field.name} | |
placeholder={field.placeholder} | |
required={field.required} | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
value={formData[field.name] || ''} | |
onChange={handleChange} | |
/> | |
)} | |
{field.type === 'number' && ( | |
<input | |
type="number" | |
name={field.name} | |
id={field.name} | |
placeholder={field.placeholder} | |
required={field.required} | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
value={formData[field.name] || ''} | |
onChange={handleChange} | |
/> | |
)} | |
{field.type === 'color' && ( | |
<input | |
type="color" | |
name={field.name} | |
id={field.name} | |
placeholder={field.placeholder} | |
required={field.required} | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
value={formData[field.name] || ''} | |
onChange={handleChange} | |
/> | |
)} | |
{field.type === 'file' && ( | |
<input | |
type="file" | |
name={field.name} | |
id={field.name} | |
placeholder={field.placeholder} | |
required={field.required} | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
value={formData[field.name] || ''} | |
onChange={handleChange} | |
/> | |
)} | |
{field.type === 'date' && ( | |
<input | |
type="date" | |
name={field.name} | |
id={field.name} | |
placeholder={field.placeholder} | |
required={field.required} | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
value={formData[field.name] || ''} | |
onChange={handleChange} | |
/> | |
)} | |
{field.type === 'textarea' && ( | |
<textarea | |
name={field.name} | |
id={field.name} | |
placeholder={field.placeholder} | |
required={field.required} | |
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | |
value={formData[field.name] || ''} | |
onChange={handleChange} | |
/> | |
)} | |
</div> | |
))} | |
<button | |
type="submit" | |
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" | |
> | |
Submit | |
</button> | |
</form> | |
); | |
} | |
export default observer(AutoForm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//use like this