11import React from 'react' ;
2+ import PropTypes from 'prop-types' ;
23import { useField , splitFormProps } from "react-form" ;
34import "../input-container/input-container.css" ;
45import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
56import { faExclamation } from '@fortawesome/free-solid-svg-icons'
67
78// input-container contains form layout, style and validation for app
8- export const InputContainer = React . forwardRef ( ( props , ref ) => {
9- const { Input, Icon, label, ...passThru } = props ;
9+ export const InputContainer = ( props ) => {
10+ const { Input, required, Icon, ...passThru } = props
11+
12+ let validate = props . validate ;
13+ if ( required ) {
14+ validate = value => {
15+ if ( ! value ) {
16+ return "Required" ;
17+ }
18+ if ( props . validate ) {
19+ return props . validate ( value ) ;
20+ }
21+ return false ;
22+ } ;
23+ }
1024
1125 // get form-specific props
12- const [ field , fieldOptions ] = splitFormProps ( props ) ;
26+ const [ field , fieldOptions , rest ] = splitFormProps ( { ... passThru , validate } ) ;
1327
1428 // get field state
1529 const {
@@ -21,7 +35,7 @@ export const InputContainer = React.forwardRef((props, ref) => {
2135 < >
2236 < div className = "input-container" >
2337 < div className = "input-label" >
24- { label }
38+ { props . label }
2539 < span className = "required" > *</ span >
2640 </ div >
2741 < div className = "input-wrapper" >
@@ -31,7 +45,7 @@ export const InputContainer = React.forwardRef((props, ref) => {
3145 </ div >
3246 : null }
3347 < div className = "element-wrapper" >
34- < Input { ...{ ref , ... passThru , getInputProps } } />
48+ < Input { ...{ getInputProps , rest } } />
3549 </ div >
3650 { error ?
3751 < div className = "icon-wrapper error" >
@@ -51,10 +65,20 @@ export const InputContainer = React.forwardRef((props, ref) => {
5165 </ div >
5266 </ >
5367 ) ;
54- } ) ;
68+ } ;
5569
5670InputContainer . defaultProps = {
57- //default props
58- }
71+ field : '' ,
72+ label : '' ,
73+ defaultValue : '' ,
74+ required : false
75+ } ;
76+
77+ InputContainer . propTypes = {
78+ field : PropTypes . string ,
79+ label : PropTypes . string ,
80+ defaultValue : PropTypes . string ,
81+ required : PropTypes . bool
82+ } ;
5983
6084export default InputContainer ;
0 commit comments