react-native-material-kit-fg
Version:
Bringing Material Design to React Native
49 lines (40 loc) • 1.1 kB
text/typescript
import { Prediction } from '../types';
import RadioButton from './RadioButton';
type Pred = Prediction<RadioButton>;
/**
* Managing a group of radio buttons.
*/
export default class RadioButtonGroup {
private readonly onAdd?: Pred;
private readonly onRemove?: Pred;
private buttons: RadioButton[];
constructor(onAdd?: Pred, onRemove?: Pred) {
this.buttons = [];
this.onAdd = onAdd;
this.onRemove = onRemove;
}
add(btn: RadioButton) {
if (this.canAdd(btn) && this.buttons.indexOf(btn) < 0) {
this.buttons.push(btn);
}
}
remove(btn: RadioButton) {
if (this.canRemove(btn)) {
const index = this.buttons.indexOf(btn);
if (index >= 0) {
this.buttons.splice(index, 1);
}
}
}
onChecked(btn: RadioButton, checked: boolean) {
if (checked) {
this.buttons.forEach(it => it !== btn && it.confirmUncheck());
}
}
private canAdd(btn: RadioButton): boolean {
return !this.onAdd || this.onAdd(btn);
}
private canRemove(btn: RadioButton): boolean {
return !this.onRemove || this.onRemove(btn);
}
}