This repository was archived by the owner on May 24, 2019. It is now read-only.
File tree 1 file changed +62
-0
lines changed
1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import { Component } from 'react' ;
3
+ import { IValidationMap } from 'validated-proxy/dist/types/utils/validator-lookup' ;
4
+ import { validatedProxy , BufferedProxy } from 'validated-proxy' ;
5
+
6
+ interface HelperProps {
7
+ model : BufferedProxy
8
+ set : < T > ( name : string , value : T ) => void
9
+ reset : ( ) => void
10
+ isPristine : boolean
11
+ hasErrors : boolean
12
+ flush : ( ) => void
13
+ }
14
+
15
+ type RenderProp = ( p : HelperProps ) => JSX . Element
16
+
17
+ interface Props < T > {
18
+ model : T
19
+ as : IValidationMap
20
+ children : RenderProp
21
+ }
22
+
23
+ interface State {
24
+ model : BufferedProxy
25
+ }
26
+
27
+ class Validate < Model extends { } > extends Component < Props < Model > , State > {
28
+ constructor ( p : Props < Model > ) {
29
+ super ( p ) ;
30
+ this . state = {
31
+ model : validatedProxy ( p . model , { validations : p . as } ) ,
32
+ } ;
33
+ }
34
+
35
+ set = < V extends { } > ( name : string , value : V ) => {
36
+ this . state . model [ name ] = value ;
37
+ this . setState ( { model : this . state . model } ) ;
38
+ }
39
+
40
+ reset = ( ) => {
41
+ this . state . model . reset ( ) ;
42
+ this . setState ( { model : this . state . model } ) ;
43
+ }
44
+
45
+ flush = ( ) => {
46
+ this . state . model . flush ( ) ;
47
+ this . setState ( { model : this . state . model } ) ;
48
+ }
49
+
50
+ render ( ) {
51
+ return this . props . children ( {
52
+ model : this . state . model ,
53
+ set : this . set ,
54
+ reset : this . reset ,
55
+ isPristine : Object . keys ( this . state . model . cache ) . length === 0 ,
56
+ hasErrors : this . state . model . errors . length > 0 ,
57
+ flush : this . flush ,
58
+ } ) ;
59
+ }
60
+ }
61
+
62
+ export default Validate ;
You can’t perform that action at this time.
0 commit comments