-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobx-form.ts
41 lines (32 loc) · 840 Bytes
/
mobx-form.ts
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
import { makeAutoObservable } from 'mobx';
export class TextInput {
constructor(public value = '') {
makeAutoObservable(this);
}
private onChange = (e: { currentTarget: { value: string } }) => {
this.value = e.currentTarget.value;
};
setValue = (value: string) => {
this.value = value;
};
get toInput() {
return { value: this.value, onChange: this.onChange };
}
}
export class CheckboxInput {
constructor(public checked: boolean) {
makeAutoObservable(this);
}
toggle = () => {
this.checked = !this.checked;
};
setValue = (value: boolean) => {
this.checked = value;
};
onChange = (event: { currentTarget: { checked: boolean } }) => {
this.checked = event.currentTarget.checked;
};
get toInput() {
return { checked: this.checked, onChange: this.onChange };
}
}