-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckBox.tsx
47 lines (39 loc) · 1.42 KB
/
CheckBox.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
43
44
45
46
47
import * as React from "react";
import { action, Lambda, autorun } from "mobx";
import { observer } from "mobx-react";
import { FormElementProps, removeProps, StyleComponent } from "./FormElementProps";
export interface CheckBoxProps extends FormElementProps {
value: { // https://github.com/danielearwicker/bidi-mobx/issues/14
get(): boolean | undefined;
set(value: boolean): void;
};
component?: StyleComponent<HTMLInputElement>
}
@observer
export class CheckBox extends React.Component<CheckBoxProps, {}> {
quit: Lambda;
indeterminate = (input: HTMLInputElement) => {
if (input) {
this.quit = autorun(() => input.indeterminate = this.props.value.get() === undefined);
} else {
this.quit();
}
}
@action.bound
changed(e: React.FormEvent<HTMLInputElement>) {
this.props.value.set(e.currentTarget.checked);
}
render() {
const { component: InputComponent = "input" } = this.props;
return (
<InputComponent type="checkbox"
{...removeProps(this.props, "value")}
checked={this.props.value.get() || false}
ref={ this.indeterminate }
onChange={this.changed}/>
);
}
}
export function CheckBoxUsing(component: StyleComponent<HTMLInputElement>) {
return (props: CheckBoxProps) => <CheckBox component={component} {...props}/>;
}