Part 05 / Topic 3 of 7
Controlled <select>, checkbox, and radio inputs
A controlled <select> works exactly like a controlled text input - value plus onChange - the only new part is that its value comes from whichever <option>'s own value attribute is currently chosen:
// examples/03_select_checkbox_radio.jsx (trimmed - the real file also has a
// <label> and a <p>Showing restaurants in: ...</p> line)
export function DeliveryCityDropdown() {
const [city, setCity] = useState("bengaluru");
return (
<select value={city} onChange={(event) => setCity(event.target.value)}>
<option value="bengaluru">Bengaluru</option>
<option value="mumbai">Mumbai</option>
<option value="delhi">Delhi</option>
<option value="pune">Pune</option>
</select>
);
}
Choosing "Mumbai" from the dropdown really moves city from "bengaluru" to "mumbai" (examples/03_select_checkbox_radio.test.jsx) - no new concept over Part 3's text inputs, just a different element reading the same two props.
Checkboxes and radios are controlled through checked, not value, because what they hold is a yes/no state, not text:
// examples/03_select_checkbox_radio.jsx (trimmed - the real file wraps this
// input in a <label> with its own text, plus a <p>Subscribed: ...</p> line)
export function SubscribeToOffersCheckbox() {
const [subscribed, setSubscribed] = useState(false);
return (
<input
type="checkbox"
checked={subscribed}
onChange={(event) => setSubscribed(event.target.checked)}
/>
);
}
event.target.checked (a boolean), not event.target.value (which a checkbox's DOM node also has, fixed at "on", and which is never what this component wants). Clicking the checkbox once really flips subscribed from false to true, and the rendered text with it (examples/03_select_checkbox_radio.test.jsx).
A radio group shares one name across every option, but each radio computes its own checked by comparing the shared state to its own value:
// examples/03_select_checkbox_radio.jsx (trimmed - the real file has a
// <legend>, a <label> with its own text around each radio, and a
// <p>Selected plan: ...</p> line)
export function PlanTypeRadioGroup() {
const [plan, setPlan] = useState("basic");
return (
<fieldset>
<input type="radio" name="plan" value="basic" checked={plan === "basic"} onChange={(event) => setPlan(event.target.value)} />
<input type="radio" name="plan" value="one" checked={plan === "one"} onChange={(event) => setPlan(event.target.value)} />
</fieldset>
);
}
Only one plan value exists at a time, so only one radio's checked === true expression is ever true - clicking "One" really unchecks "Basic" in the same render that checks "One" (examples/03_select_checkbox_radio.test.jsx), with no code anywhere that explicitly unchecks the other button. name="plan" on both inputs is what makes them a group to the browser (keyboard arrow-key navigation between them depends on it); the checked comparison is what makes them a group to React.
Done here? The practice set for Part 5 — including the solutions — is on the part page.