Open chaesthetics opened 12 months ago
Radio.js
import * as React from "react"; export const RadioGroup = ({ onChange, selected, children }) => { const RadioOptions = React.Children.map(children, (child) => { return React.cloneElement(child, { onChange, checked: child.props.value === selected, }); }); return <div className="RadioGroup">{RadioOptions}</div>; }; export const RadioOption = ({ value, checked, onChange, children }) => { return ( <div className="RadioOption"> <input id={value} type="radio" name={value} value={value} checked={checked} onChange={(e) => { onChange(e.target.value); }} /> <label htmlFor={value}>{children}</label> </div> ); };
App.js
import "./index.css"; import { RadioGroup, RadioOption } from "./Radyo"; import { useState } from "react"; function App() { const [selected, setSelected] = useState(""); return ( <div className="App"> <h2>How did you hear about Little Lemon?</h2> <RadioGroup onChange={setSelected} selected={selected}> <RadioOption value="social_media">Social Media</RadioOption> <RadioOption value="friends">Friends</RadioOption> <RadioOption value="advertising">Advertising</RadioOption> <RadioOption value="other">Other</RadioOption> </RadioGroup> <button disabled={!selected}>Submit</button> </div> ); } export default App;
Radio.js
App.js