This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree 7 files changed +47
-8
lines changed
routes/ResourceBookingForm
7 files changed +47
-8
lines changed Original file line number Diff line number Diff line change @@ -10,13 +10,14 @@ import "./styles.module.scss";
10
10
11
11
const DateInput = ( props ) => {
12
12
return (
13
- < div styleName = " datepicker-wrapper" >
13
+ < div styleName = { ` datepicker-wrapper ${ props . className } ` } >
14
14
< DatePicker
15
15
dateFormat = "MM/dd/yyyy"
16
16
placeholderText = { props . placeholder }
17
17
selected = { props . value }
18
18
onChange = { props . onChange }
19
19
onBlur = { props . onBlur }
20
+ onCalendarClose = { props . onBlur }
20
21
onFocus = { props . onFocus }
21
22
/>
22
23
</ div >
Original file line number Diff line number Diff line change 4
4
& > div {
5
5
width : 100% ;
6
6
}
7
+ & .error {
8
+ input {
9
+ border-color : #fe665d ;
10
+ }
11
+
12
+ }
7
13
}
8
14
9
15
.datepicker-wrapper > div :nth-child (2 ) > div :nth-child (2 ) {
Original file line number Diff line number Diff line change @@ -13,11 +13,16 @@ import ReactSelect from "../../components/ReactSelect";
13
13
import DateInput from "../../components/DateInput" ;
14
14
import "./styles.module.scss" ;
15
15
16
+ const updateInput = ( field , input ) => {
17
+ field . input = input ;
18
+ }
19
+
16
20
const FormField = ( { field, isGroupField } ) => {
17
21
return (
18
22
< Field name = { field . name } >
19
23
{ ( { input, meta } ) => (
20
24
< div styleName = { isGroupField ? "field-group-field" : "" } >
25
+ { updateInput ( field , input ) }
21
26
{ ! field . readonly && (
22
27
< label
23
28
styleName = {
@@ -71,6 +76,7 @@ const FormField = ({ field, isGroupField }) => {
71
76
onChange = { input . onChange }
72
77
onBlur = { input . onBlur }
73
78
onFocus = { input . onFocus }
79
+ className = { meta . error && meta . touched ? "error" : "" }
74
80
/>
75
81
) }
76
82
{ field . type === FORM_FIELD_TYPE . SELECT && (
@@ -83,7 +89,7 @@ const FormField = ({ field, isGroupField }) => {
83
89
onFocus = { input . onFocus }
84
90
/>
85
91
) }
86
- { field . isRequired && meta . error && meta . touched && (
92
+ { ( field . isRequired || field . customValidator ) && meta . error && meta . touched && (
87
93
< div styleName = "field-error" > { meta . error } </ div >
88
94
) }
89
95
</ div >
@@ -111,6 +117,7 @@ FormField.prototype = {
111
117
maxLength : PT . number ,
112
118
styleName : PT . string ,
113
119
readonly : PT . string ,
120
+ input : PT . object
114
121
} )
115
122
) . isRequired ,
116
123
isGroupField : PT . bool ,
Original file line number Diff line number Diff line change @@ -106,6 +106,7 @@ TCForm.propTypes = {
106
106
label : PT . string ,
107
107
type : PT . oneOf ( Object . values ( FORM_FIELD_TYPE ) ) . isRequired ,
108
108
isRequired : PT . bool ,
109
+ customValidator : PT . func ,
109
110
validationMessage : PT . string ,
110
111
name : PT . string . isRequired ,
111
112
component : PT . element ,
Original file line number Diff line number Diff line change @@ -68,13 +68,19 @@ export const createConfigurationObject = (fields) => {
68
68
* @returns {Function } configuration object
69
69
*/
70
70
export const getValidator = ( fields ) => {
71
- return ( values ) => {
71
+ return ( values , inputs ) => {
72
72
const errors = { } ;
73
73
fields
74
- . filter ( ( f ) => f . isRequired )
74
+ . filter ( ( f ) => f . isRequired || f . customValidator )
75
75
. forEach ( ( f ) => {
76
- if ( ! values [ f . name ] ) {
76
+ if ( f . isRequired && ! values [ f . name ] ) {
77
77
errors [ f . name ] = f . validationMessage ;
78
+ } else if ( f . customValidator ) {
79
+ const error = f . customValidator ( f , fields , values ) ;
80
+ if ( error ) {
81
+ errors [ f . name ] = error ;
82
+ f . input . onBlur ( ) ;
83
+ }
78
84
}
79
85
} ) ;
80
86
return errors ;
Original file line number Diff line number Diff line change @@ -10,9 +10,7 @@ import "./styles.module.scss";
10
10
function TextInput ( props ) {
11
11
return (
12
12
< input
13
- styleName = { `TextInput ${ props . className } ${
14
- props . readonly ? "readonly" : ""
15
- } `}
13
+ styleName = { `TextInput ${ props . className } ${ props . readonly ? "readonly" : "" } ` }
16
14
maxLength = { props . maxLength }
17
15
min = { props . minValue }
18
16
onChange = { ( event ) => {
@@ -33,6 +31,7 @@ function TextInput(props) {
33
31
readOnly = { props . readonly ?? false }
34
32
onBlur = { props . onBlur }
35
33
onFocus = { props . onFocus }
34
+ step = { props . type === "number" ? ".01" : null }
36
35
/>
37
36
) ;
38
37
}
Original file line number Diff line number Diff line change 3
3
*
4
4
* utility class
5
5
*/
6
+ import moment from 'moment' ;
6
7
import {
7
8
STATUS_OPTIONS ,
8
9
FORM_ROW_TYPE ,
@@ -45,12 +46,30 @@ export const getEditResourceBookingConfig = (onSubmit) => {
45
46
type : FORM_FIELD_TYPE . DATE ,
46
47
name : "startDate" ,
47
48
placeholder : "Start Date" ,
49
+ customValidator : ( field , fields , values ) => {
50
+ const startDate = values [ field . name ] ;
51
+ const endDate = values [ 'endDate' ] ;
52
+ if ( startDate && endDate && moment ( endDate ) . startOf ( 'day' ) . isBefore ( moment ( startDate ) . startOf ( 'day' ) ) ) {
53
+ return 'Start Date should not be after End Date' ;
54
+ }
55
+ return null ;
56
+ }
48
57
} ,
49
58
{
50
59
label : "End Date" ,
51
60
type : FORM_FIELD_TYPE . DATE ,
52
61
name : "endDate" ,
53
62
placeholder : "End Date" ,
63
+ customValidator : ( field , fields , values ) => {
64
+ const endDate = values [ field . name ] ;
65
+ const startDate = values [ 'startDate' ] ;
66
+ if ( startDate && endDate && moment ( endDate ) . startOf ( 'day' ) . isBefore ( moment ( startDate ) . startOf ( 'day' ) ) ) {
67
+ return 'End Date should not be before Start Date' ;
68
+ } else if ( ! startDate && endDate ) {
69
+ return 'End Date should not be before Start Date' ;
70
+ }
71
+ return null ;
72
+ }
54
73
} ,
55
74
{
56
75
label : "Status" ,
You can’t perform that action at this time.
0 commit comments