-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormBinder.ts
More file actions
71 lines (61 loc) · 1.65 KB
/
Copy pathFormBinder.ts
File metadata and controls
71 lines (61 loc) · 1.65 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {
ButtonBindingOptions,
CheckboxBinding,
FormBindingOptions,
InputBinding,
ObservableFormBinder,
RadioBinding,
SelectBinding,
SubmitBinding,
SubmitButtonBinding,
} from "./types"
import { ObservableForm } from "@corets/form"
export class FormBinder implements ObservableFormBinder {
target: ObservableForm
constructor(target: ObservableForm) {
this.target = target
}
form(options: FormBindingOptions = {}): SubmitBinding {
options = { ...{ validate: undefined }, ...options }
return {
onSubmit: (e) => {
e.preventDefault()
this.target.submit({ validate: options.validate })
},
}
}
button(options: ButtonBindingOptions = {}): SubmitButtonBinding {
options = { ...{ validate: undefined, disableOnSubmit: true }, ...options }
return {
disabled: !!(options.disableOnSubmit && this.target.isSubmitting()),
onClick: (e) => {
e.preventDefault()
this.target.submit({ validate: options.validate })
},
}
}
input(path: string): InputBinding {
return {
name: path,
value: this.target.getAt(path),
onChange: (e) => this.target.setAt(path, e.target.value),
}
}
select(path: string): SelectBinding {
return this.input(path)
}
checkbox(path: string): CheckboxBinding {
return {
name: path,
checked: !!this.target.getAt(path),
onChange: (e) => this.target.setAt(path, !!e.target.checked),
}
}
radio(path: string, value: any = true): RadioBinding {
return {
name: path,
checked: value === this.target.getAt(path),
onChange: () => this.target.setAt(path, value),
}
}
}