import React, { useState } from 'react'; import './SwitchGroup.css'; export default function SwitchGroup({ options = [], value = [], title = '', selectAll = false, onChange }) { const allOn = options.length > 0 && options.every(o => value.indexOf(o.value) >= 0); function toggle(v) { const arr = value.slice(); const idx = arr.indexOf(v); if (idx >= 0) arr.splice(idx, 1); else arr.push(v); onChange && onChange(arr); } function toggleAll() { onChange && onChange(allOn ? [] : options.map(o => o.value)); } return (
{(title || selectAll) ? (
{title} {selectAll ? (
) : null}
) : null} {options.map((o, i) => (
{o.label} {o.desc ? {o.desc} : null}
= 0 ? ' is-on' : '')} onClick={() => toggle(o.value)} />
))}
); }