Compound Input With Label
ReactJS
Medium
5 views
Problem Description
Create a Field component that renders label + input and accepts id/name props.
Output Format
Render a React component.
Constraints
Use htmlFor and id properly.
Official Solution
import React, { useState } from 'react';
function Field({ label, id, value, onChange, type = 'text' }) {
return (
<div style={{ display: 'grid', gap: 6 }}>
<label htmlFor={id} style={{ fontWeight: 600 }}>{label}</label>
<input id={id} type={type} value={value} onChange={(e) => onChange(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid #bbb' }} />
</div>
);
}
export default function App() {
const [email, setEmail] = useState('');
return (
<div style={{ padding: 16, width: 420 }}>
<Field label='Email' id='email' type='email' value={email} onChange={setEmail} />
<div style={{ marginTop: 10, color: '#555' }}>Typing: {email}</div>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!