@@ -14,7 +14,15 @@ const shallowequal = require("shallowequal");
1414const tools_1 = require ( "../tools" ) ;
1515const Form_1 = require ( "./Form" ) ;
1616const Validation_1 = require ( "./Validation" ) ;
17- _a = React . createContext ( ) , exports . Provider = _a . Provider , exports . Consumer = _a . Consumer ;
17+ const defaultProps = {
18+ validationErrors : [ ] ,
19+ validationScope : [ ] ,
20+ rest : { } ,
21+ formState : {
22+ model : { } ,
23+ } ,
24+ } ;
25+ _a = React . createContext ( defaultProps ) , exports . Provider = _a . Provider , exports . Consumer = _a . Consumer ;
1826class FieldClass extends React . Component {
1927 constructor ( ) {
2028 super ( ...arguments ) ;
@@ -40,34 +48,38 @@ class FieldClass extends React.Component {
4048 } ) ;
4149 } ;
4250 this . update = ( nextValue ) => {
43- const { formState : { handleChange } , name, value, isChanged, isVisited, } = this . props ;
51+ const { formState : { handleChange } , name, value, isChanged, isVisited, onChange , } = this . props ;
4452 const updValue = Object . assign ( { value,
4553 isChanged,
4654 isVisited } , ( nextValue || { } ) ) ;
4755 handleChange ( name , updValue ) ;
48- if ( this . props . onChange ) {
49- this . props . onChange ( this . props . name , updValue ) ;
56+ if ( onChange ) {
57+ onChange ( name , updValue ) ;
5058 }
5159 } ;
5260 }
5361 render ( ) {
54- const { children } = this . props ;
55- const props = Object . assign ( { } , this . props , { onChange : this . handleChange , onClick : this . onClick } ) ;
56- const rChildren = children
57- && typeof children === "function"
58- ? children ( props )
59- : children ;
60- return ( React . createElement ( exports . Provider , { value : props } , rChildren ) ) ;
62+ const { value, children } = this . props ;
63+ const context = Object . assign ( { } , this . props , { value : value === undefined ? "" : value , onChange : this . handleChange , onClick : this . onClick } ) ;
64+ return ( children && typeof children === "function"
65+ ? children ( context )
66+ : React . createElement ( exports . Provider , { value : context } , children ) ) ;
6167 }
6268 componentDidMount ( ) {
6369 this . update ( ) ;
6470 }
71+ componentDidUpdate ( prevProps ) {
72+ if ( prevProps . value === undefined ) {
73+ this . update ( ) ;
74+ }
75+ }
6576 shouldComponentUpdate ( nextProps ) {
66- const _a = nextProps , { validationErrors : nextErrors , validationScope : nextScope , formState : _ , children : __ } = _a , nextRest = __rest ( _a , [ "validationErrors" , "validationScope" , "formState" , "children" ] ) ;
67- const _b = this . props , { validationErrors, validationScope, formState, children } = _b , rest = __rest ( _b , [ "validationErrors" , "validationScope" , "formState" , "children" ] ) ;
68- if ( ! tools_1 . isArrayEqual ( ( validationErrors || [ ] ) . map ( error => error . message ) , ( nextErrors || [ ] ) . map ( error => error . message ) )
69- || ! tools_1 . isArrayEqual ( ( validationScope || [ ] ) . map ( error => error . message ) , ( nextScope || [ ] ) . map ( error => error . message ) )
70- || ! shallowequal ( nextRest , rest ) ) {
77+ const { name : nextName , value : nextValue , isVisited : nextIsVisited , isChanged : nextIsChanged , isValid : nextIsValid , validationErrors : nextErrors , validationScope : nextScope , rest : nextRest , } = nextProps ;
78+ const { name, value, isVisited, isChanged, isValid, validationErrors, validationScope, rest, } = this . props ;
79+ if ( ! tools_1 . isArrayEqual ( validationErrors . map ( error => error . message ) , nextErrors . map ( error => error . message ) )
80+ || ! tools_1 . isArrayEqual ( validationScope . map ( error => error . message ) , nextScope . map ( error => error . message ) )
81+ || ! shallowequal ( nextRest , rest )
82+ || ! shallowequal ( { nextName, nextValue, nextIsChanged, nextIsValid, nextIsVisited } , { name, value, isVisited, isChanged, isValid } ) ) {
7183 return true ;
7284 }
7385 return false ;
@@ -78,20 +90,21 @@ class FieldClass extends React.Component {
7890 }
7991 }
8092}
93+ FieldClass . defaultProps = defaultProps ;
8194exports . FieldClass = FieldClass ;
8295class Field extends React . Component {
8396 render ( ) {
8497 return ( React . createElement ( Form_1 . Consumer , null , ( formState ) => ( React . createElement ( Validation_1 . Consumer , null , validation => {
85- const props = this . props ;
86- const modelValue = formState . model [ props . name ] ;
87- const value = modelValue === undefined ? "" : modelValue . value ;
98+ const _a = this . props , { name , children , onClick , onChange } = _a , rest = __rest ( _a , [ "name" , "children" , "onClick" , "onChange" ] ) ;
99+ const modelValue = formState . model [ name ] ;
100+ const value = modelValue === undefined ? undefined : modelValue . value ;
88101 const isChanged = modelValue === undefined ? false : modelValue . isChanged ;
89102 const isVisited = modelValue === undefined ? false : modelValue . isVisited ;
90103 const isValid = ( validation . errors [ this . props . name ] === undefined
91104 || validation . errors [ this . props . name ] . length === 0 )
92105 && ( validation . scope === undefined || validation . scope . length === 0 ) ;
93106 const _Field = FieldClass ;
94- return ( React . createElement ( _Field , Object . assign ( { } , props , { value : value , validationErrors : validation . errors [ this . props . name ] , validationScope : validation . scope , formState : formState , isChanged : isChanged , isVisited : isVisited , isValid : isValid } ) ) ) ;
107+ return ( React . createElement ( _Field , { name : name , value : value , validationErrors : validation . errors [ this . props . name ] , validationScope : validation . scope , formState : formState , isChanged : isChanged , isVisited : isVisited , isValid : isValid , onClick : onClick , onChange : onChange , children : children , rest : rest } ) ) ;
95108 } ) ) ) ) ;
96109 }
97110}
0 commit comments