1+ 'use strict' ;
2+
3+ var PropTypes = require ( 'prop-types' ) ;
4+ var utils = require ( './utils.js' ) ;
5+ var React = global . React || require ( 'react' ) ;
6+
7+ var convertValidationsToObject = function convertValidationsToObject ( validations ) {
8+
9+ if ( typeof validations === 'string' ) {
10+
11+ return validations . split ( / \, (? ! [ ^ { \[ ] * [ } \] ] ) / g) . reduce ( function ( validations , validation ) {
12+ var args = validation . split ( ':' ) ;
13+ var validateMethod = args . shift ( ) ;
14+
15+ args = args . map ( function ( arg ) {
16+ try {
17+ return JSON . parse ( arg ) ;
18+ } catch ( e ) {
19+ return arg ; // It is a string if it can not parse it
20+ }
21+ } ) ;
22+
23+ if ( args . length > 1 ) {
24+ throw new Error ( 'Formsy does not support multiple args on string validations. Use object format of validations instead.' ) ;
25+ }
26+
27+ validations [ validateMethod ] = args . length ? args [ 0 ] : true ;
28+ return validations ;
29+ } , { } ) ;
30+ }
31+
32+ return validations || { } ;
33+ } ;
34+
35+ module . exports = {
36+ getInitialState : function getInitialState ( ) {
37+ return {
38+ _value : this . props . value ,
39+ _isRequired : false ,
40+ _isValid : true ,
41+ _isPristine : true ,
42+ _pristineValue : this . props . value ,
43+ _validationError : [ ] ,
44+ _externalError : null ,
45+ _formSubmitted : false
46+ } ;
47+ } ,
48+ contextTypes : {
49+ formsy : PropTypes . object // What about required?
50+ } ,
51+ getDefaultProps : function getDefaultProps ( ) {
52+ return {
53+ validationError : '' ,
54+ validationErrors : { }
55+ } ;
56+ } ,
57+
58+ componentWillMount : function componentWillMount ( ) {
59+ var configure = function ( ) {
60+ this . setValidations ( this . props . validations , this . props . required ) ;
61+
62+ // Pass a function instead?
63+ this . context . formsy . attachToForm ( this ) ;
64+ //this.props._attachToForm(this);
65+ } . bind ( this ) ;
66+
67+ if ( ! this . props . name ) {
68+ throw new Error ( 'Form Input requires a name property when used' ) ;
69+ }
70+
71+ /*
72+ if (!this.props._attachToForm) {
73+ return setTimeout(function () {
74+ if (!this.isMounted()) return;
75+ if (!this.props._attachToForm) {
76+ throw new Error('Form Mixin requires component to be nested in a Form');
77+ }
78+ configure();
79+ }.bind(this), 0);
80+ }
81+ */
82+ configure ( ) ;
83+ } ,
84+
85+ // We have to make the validate method is kept when new props are added
86+ componentWillReceiveProps : function componentWillReceiveProps ( nextProps ) {
87+ this . setValidations ( nextProps . validations , nextProps . required ) ;
88+ } ,
89+
90+ componentDidUpdate : function componentDidUpdate ( prevProps ) {
91+
92+ // If the value passed has changed, set it. If value is not passed it will
93+ // internally update, and this will never run
94+ if ( ! utils . isSame ( this . props . value , prevProps . value ) ) {
95+ this . setValue ( this . props . value ) ;
96+ }
97+
98+ // If validations or required is changed, run a new validation
99+ if ( ! utils . isSame ( this . props . validations , prevProps . validations ) || ! utils . isSame ( this . props . required , prevProps . required ) ) {
100+ this . context . formsy . validate ( this ) ;
101+ }
102+ } ,
103+
104+ // Detach it when component unmounts
105+ componentWillUnmount : function componentWillUnmount ( ) {
106+ this . context . formsy . detachFromForm ( this ) ;
107+ //this.props._detachFromForm(this);
108+ } ,
109+
110+ setValidations : function setValidations ( validations , required ) {
111+
112+ // Add validations to the store itself as the props object can not be modified
113+ this . _validations = convertValidationsToObject ( validations ) || { } ;
114+ this . _requiredValidations = required === true ? { isDefaultRequiredValue : true } : convertValidationsToObject ( required ) ;
115+ } ,
116+
117+ // We validate after the value has been set
118+ setValue : function setValue ( value ) {
119+ this . setState ( {
120+ _value : value ,
121+ _isPristine : false
122+ } , function ( ) {
123+ this . context . formsy . validate ( this ) ;
124+ //this.props._validate(this);
125+ } . bind ( this ) ) ;
126+ } ,
127+ resetValue : function resetValue ( ) {
128+ this . setState ( {
129+ _value : this . state . _pristineValue ,
130+ _isPristine : true
131+ } , function ( ) {
132+ this . context . formsy . validate ( this ) ;
133+ //this.props._validate(this);
134+ } ) ;
135+ } ,
136+ getValue : function getValue ( ) {
137+ return this . state . _value ;
138+ } ,
139+ hasValue : function hasValue ( ) {
140+ return this . state . _value !== '' ;
141+ } ,
142+ getErrorMessage : function getErrorMessage ( ) {
143+ var messages = this . getErrorMessages ( ) ;
144+ return messages . length ? messages [ 0 ] : null ;
145+ } ,
146+ getErrorMessages : function getErrorMessages ( ) {
147+ return ! this . isValid ( ) || this . showRequired ( ) ? this . state . _externalError || this . state . _validationError || [ ] : [ ] ;
148+ } ,
149+ isFormDisabled : function isFormDisabled ( ) {
150+ return this . context . formsy . isFormDisabled ( ) ;
151+ //return this.props._isFormDisabled();
152+ } ,
153+ isValid : function isValid ( ) {
154+ return this . state . _isValid ;
155+ } ,
156+ isPristine : function isPristine ( ) {
157+ return this . state . _isPristine ;
158+ } ,
159+ isFormSubmitted : function isFormSubmitted ( ) {
160+ return this . state . _formSubmitted ;
161+ } ,
162+ isRequired : function isRequired ( ) {
163+ return ! ! this . props . required ;
164+ } ,
165+ showRequired : function showRequired ( ) {
166+ return this . state . _isRequired ;
167+ } ,
168+ showError : function showError ( ) {
169+ return ! this . showRequired ( ) && ! this . isValid ( ) ;
170+ } ,
171+ isValidValue : function isValidValue ( value ) {
172+ return this . context . formsy . isValidValue . call ( null , this , value ) ;
173+ //return this.props._isValidValue.call(null, this, value);
174+ }
175+ } ;
0 commit comments