RadioList.jsx
50 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import clx from 'classnames'; |
| 5 | |
| 6 | export default function RadioList({ |
| 7 | id, |
| 8 | onChange, |
| 9 | options, |
| 10 | value = '', |
| 11 | isButton = false, |
| 12 | className = '', |
| 13 | }) { |
| 14 | const wrapClassName = clx( |
| 15 | 'advads-radio-list', |
| 16 | { 'is-button': isButton }, |
| 17 | className |
| 18 | ); |
| 19 | |
| 20 | return ( |
| 21 | <div className={wrapClassName}> |
| 22 | {options.map((option) => { |
| 23 | const radioId = `radio-${option.value}-${id}`; |
| 24 | const props = { |
| 25 | type: 'radio', |
| 26 | id: radioId, |
| 27 | name: id, |
| 28 | value: option.value, |
| 29 | }; |
| 30 | |
| 31 | if (value) { |
| 32 | props.checked = value === option.value; |
| 33 | } |
| 34 | |
| 35 | return ( |
| 36 | <div className="advads-radio-list--item" key={option.value}> |
| 37 | <input |
| 38 | {...props} |
| 39 | onChange={() => onChange(option.value)} |
| 40 | /> |
| 41 | <label htmlFor={radioId}> |
| 42 | <span>{option.label}</span> |
| 43 | </label> |
| 44 | </div> |
| 45 | ); |
| 46 | })} |
| 47 | </div> |
| 48 | ); |
| 49 | } |
| 50 |