import { useState, ChangeEvent, FormEvent } from 'react';
function App() {
const [formData, setFormData] = useState({
username: "",
email: "",
age: 0
});
const [submitted, setSubmitted] = useState(null);
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setFormData({
...formData,
[name]: name === 'age' ? Number(value) : value
});
};
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setSubmitted(formData);
};
return (
<div style={{ padding: '20px', maxWidth: '500px', margin: '0 auto' }}>
<h2>ユーザー登録フォーム</h2>
<form onSubmit={handleSubmit} style={{ marginTop: '20px' }}>
<div style={{ marginBottom: '15px' }}>
<label style={{ display: 'block', marginBottom: '5px', fontWeight: 'bold' }}>
ユーザー名
</label>
<input
type="text"
name="username"
value={formData.username}
onChange={handleChange}
placeholder="山田太郎"
required
style={{
width: '100%',
padding: '10px',
fontSize: '16px',
border: '1px solid #ccc',
borderRadius: '4px'
}}
/>
</div>
<div style={{ marginBottom: '15px' }}>
<label style={{ display: 'block', marginBottom: '5px', fontWeight: 'bold' }}>
メールアドレス
</label>
<input
type="email"
name="email"
value={formData.email}
onChange={handleChange}
placeholder="[email protected]"
required
style={{
width: '100%',
padding: '10px',
fontSize: '16px',
border: '1px solid #ccc',
borderRadius: '4px'
}}
/>
</div>
<div style={{ marginBottom: '15px' }}>
<label style={{ display: 'block', marginBottom: '5px', fontWeight: 'bold' }}>
年齢
</label>
<input
type="number"
name="age"
value={formData.age || ""}
onChange={handleChange}
placeholder="25"
required
min="0"
style={{
width: '100%',
padding: '10px',
fontSize: '16px',
border: '1px solid #ccc',
borderRadius: '4px'
}}
/>
</div>
<button
type="submit"
style={{
width: '100%',
padding: '12px',
fontSize: '16px',
backgroundColor: '#007bff',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
fontWeight: 'bold'
}}
>
登録
</button>
</form>
{submitted && (
<div style={{
marginTop: '30px',
padding: '20px',
backgroundColor: '#d4edda',
border: '2px solid #c3e6cb',
borderRadius: '8px'
}}>
<h3 style={{ marginTop: 0, color: '#155724' }}>登録情報</h3>
<p><strong>ユーザー名:</strong> {submitted.username}</p>
<p><strong>メール:</strong> {submitted.email}</p>
<p><strong>年齢:</strong> {submitted.age}歳</p>
</div>
)}
</div>
);
}
export default App;