Created
November 22, 2021 11:29
-
-
Save omas-public/b8121b26bc4b2246b5d0bf93b430c80f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react' | |
import './App.css' | |
const VueForm = ({ checkedValue }) => ( | |
<p> | |
現在選択されている値:<b>{checkedValue}</b> | |
</p>) | |
const RadioBtnItems = ({ handleChange, checkedValue, values }) => ( | |
values.map(v => | |
<label key={v.id}> | |
<input | |
type='radio' | |
value={v.item} | |
onChange={handleChange} | |
checked={checkedValue === v.item} | |
/> | |
{v.item} | |
</label> | |
) | |
) | |
const InputRadio = ({ values }) => { | |
const INITIAL_VALUE = values[0].item | |
const [checkedValue, setCheckedValue] = useState(INITIAL_VALUE) | |
const handleChange = (e) => setCheckedValue(e.target.value) | |
return ( | |
<div className='App'> | |
<VueForm checkedValue={checkedValue} /> | |
<RadioBtnItems | |
handleChange={handleChange} | |
checkedValue={checkedValue} | |
values={values} | |
/> | |
</div> | |
) | |
} | |
export default function App () { | |
const values = [ | |
{ id: 1, item: 'red' }, | |
{ id: 2, item: 'blue' }, | |
{ id: 3, item: 'yellow' } | |
] | |
return <InputRadio values={values} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment