File size: 1,322 Bytes
cce8120 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import React, { useState } from 'react';
function TestForm({ onCreate }) {
const [formData, setFormData] = useState({ name: '', description: '' });
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState(null);
const handleChange = (e) => {
const { name, value } = e.target;
setFormData((prev) => ({ ...prev, [name]: value }));
};
const handleSubmit = async (e) => {
e.preventDefault();
setSubmitting(true);
setError(null);
try {
await onCreate(formData);
setFormData({ name: '', description: '' });
} catch (e) {
setError(e.message);
} finally {
setSubmitting(false);
}
};
return (
<form onSubmit={handleSubmit} style={{ marginBottom: '2rem' }}>
<h2>Create New Test</h2>
{error && <p style={{ color: 'red' }}>{error}</p>}
<input
name="name"
value={formData.name}
onChange={handleChange}
placeholder="Name"
required
disabled={submitting}
/>
<input
name="description"
value={formData.description}
onChange={handleChange}
placeholder="Description"
disabled={submitting}
/>
<button type="submit" disabled={submitting}>Create</button>
</form>
);
}
export default TestForm;
|