1
- import React from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
4
// Components
@@ -16,15 +16,37 @@ const TextField = ({
16
16
visibility,
17
17
editMode,
18
18
saveState,
19
- error,
20
19
label,
21
20
placeholder,
22
21
instructions,
22
+ restrictions,
23
+ errorMessage,
23
24
changeHandler,
24
25
submitHandler,
25
26
closeHandler,
26
27
openHandler,
27
28
} ) => {
29
+ const [ displayedMessage , setErrorMessage ] = useState ( '' ) ;
30
+
31
+ useEffect ( ( ) => {
32
+ if ( ! value && errorMessage ?. required ) {
33
+ setErrorMessage ( errorMessage . required ) ;
34
+ } else if ( restrictions . max_length && value . length > restrictions . max_length ) {
35
+ setErrorMessage ( errorMessage . max_length ) ;
36
+ } else if ( restrictions . min_length && value . length < restrictions . min_length ) {
37
+ setErrorMessage ( errorMessage . min_length ) ;
38
+ } else {
39
+ setErrorMessage ( null ) ;
40
+ }
41
+ } , [
42
+ errorMessage . max_length ,
43
+ errorMessage . min_length ,
44
+ errorMessage . required ,
45
+ restrictions . max_length ,
46
+ restrictions . min_length ,
47
+ value ,
48
+ ] ) ;
49
+
28
50
const handleChange = ( e ) => {
29
51
const { name, value : newValue } = e . target ;
30
52
changeHandler ( name , newValue ) ;
@@ -52,7 +74,7 @@ const TextField = ({
52
74
< form data-testid = "test-form" onSubmit = { handleSubmit } >
53
75
< Form . Group
54
76
controlId = { formId }
55
- isInvalid = { error !== null }
77
+ isInvalid = { displayedMessage !== null }
56
78
>
57
79
< label className = "edit-section-header" htmlFor = { formId } >
58
80
{ label }
@@ -63,10 +85,12 @@ const TextField = ({
63
85
name = { formId }
64
86
value = { value }
65
87
onChange = { handleChange }
88
+ minLength = { restrictions . min_length }
89
+ maxLength = { restrictions . max_length }
66
90
/>
67
- { error !== null && (
91
+ { displayedMessage !== null && (
68
92
< Form . Control . Feedback hasIcon = { false } >
69
- { error }
93
+ { displayedMessage }
70
94
</ Form . Control . Feedback >
71
95
) }
72
96
</ Form . Group >
@@ -77,6 +101,7 @@ const TextField = ({
77
101
saveState = { saveState }
78
102
cancelHandler = { handleClose }
79
103
changeHandler = { handleChange }
104
+ disabled = { displayedMessage !== null }
80
105
/>
81
106
</ form >
82
107
</ div >
@@ -121,7 +146,15 @@ TextField.propTypes = {
121
146
visibility : PropTypes . oneOf ( [ 'private' , 'all_users' ] ) ,
122
147
editMode : PropTypes . oneOf ( [ 'editing' , 'editable' , 'empty' , 'static' ] ) ,
123
148
saveState : PropTypes . string ,
124
- error : PropTypes . string ,
149
+ errorMessage : PropTypes . shape ( {
150
+ required : PropTypes . string ,
151
+ max_length : PropTypes . string ,
152
+ min_length : PropTypes . string ,
153
+ } ) ,
154
+ restrictions : PropTypes . shape ( {
155
+ max_length : PropTypes . number ,
156
+ min_length : PropTypes . number ,
157
+ } ) ,
125
158
placeholder : PropTypes . string ,
126
159
instructions : PropTypes . string ,
127
160
label : PropTypes . string . isRequired ,
@@ -138,7 +171,6 @@ TextField.defaultProps = {
138
171
placeholder : '' ,
139
172
instructions : '' ,
140
173
visibility : 'private' ,
141
- error : null ,
142
174
} ;
143
175
144
176
export default connect ( editableFormSelector , { } ) ( TextField ) ;
0 commit comments