-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRadioButton.tsx
42 lines (33 loc) · 1.36 KB
/
RadioButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as React from "react";
import { action } from "mobx";
import { observer } from "mobx-react";
import { BoxedValue } from "boxm";
import { FormElementProps, removeProps, StyleComponent } from "./FormElementProps";
export interface RadioButtonProps<T> extends FormElementProps {
value: BoxedValue<T>;
option: T;
component?: StyleComponent<HTMLInputElement>
}
@observer
export class TypedRadioButton<T> extends React.Component<RadioButtonProps<T>, {}> {
@action.bound
changed(ev: React.FormEvent<HTMLInputElement>) {
if (ev.currentTarget.checked) {
this.props.value.set(this.props.option);
}
}
render() {
const { component: InputComponent = "input" } = this.props;
return <InputComponent type="radio"
{...removeProps(this.props, "value", "option")}
checked={this.props.value.get() === this.props.option}
onChange={this.changed} />;
}
}
export class RadioButton extends TypedRadioButton<any> {}
export class RadioButtonString extends TypedRadioButton<string> {}
export class RadioButtonNumber extends TypedRadioButton<number> {}
export function RadioButtonUsing<T>(component: StyleComponent<HTMLInputElement>) {
const I = class extends TypedRadioButton<T> {};
return (props: RadioButtonProps<T>) => <I component={component} {...props}/>;
}