38 lines
1.0 KiB
React
38 lines
1.0 KiB
React
import React from 'react';
|
|
import './ButtonGroup.css';
|
|
|
|
export default function ButtonGroup({ options = [], value, multiple = false, size = 'default', onChange }) {
|
|
function isSelected(v) {
|
|
return multiple ? (value || []).indexOf(v) >= 0 : value === v;
|
|
}
|
|
function toggle(o) {
|
|
if (o.disabled) return;
|
|
let next;
|
|
if (multiple) {
|
|
const arr = (value || []).slice();
|
|
const idx = arr.indexOf(o.value);
|
|
if (idx >= 0) arr.splice(idx, 1); else arr.push(o.value);
|
|
next = arr;
|
|
} else {
|
|
next = o.value;
|
|
}
|
|
onChange && onChange(next);
|
|
}
|
|
|
|
const sizeCls = size === 'small' ? ' is-sm' : size === 'large' ? ' is-lg' : '';
|
|
return (
|
|
<div className={'aa-btngroup' + sizeCls}>
|
|
{options.map((o, i) => (
|
|
<button
|
|
key={i}
|
|
className={'aa-btngroup-btn' + (isSelected(o.value) ? ' is-selected' : '') + (o.disabled ? ' is-disabled' : '')}
|
|
disabled={!!o.disabled}
|
|
onClick={() => toggle(o)}
|
|
>
|
|
{o.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|