1+ "use strict" ;
2+
3+ function _typeof ( obj ) { "@babel/helpers - typeof" ; if ( typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ) { _typeof = function _typeof ( obj ) { return typeof obj ; } ; } else { _typeof = function _typeof ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ; } return _typeof ( obj ) ; }
4+
5+ Object . defineProperty ( exports , "__esModule" , {
6+ value : true
7+ } ) ;
8+ exports . default = FormControl ;
9+
10+ var _react = _interopRequireWildcard ( require ( "react" ) ) ;
11+
12+ var _classnames = _interopRequireDefault ( require ( "classnames" ) ) ;
13+
14+ require ( "./FormControl.scss" ) ;
15+
16+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
17+
18+ function _getRequireWildcardCache ( ) { if ( typeof WeakMap !== "function" ) return null ; var cache = new WeakMap ( ) ; _getRequireWildcardCache = function _getRequireWildcardCache ( ) { return cache ; } ; return cache ; }
19+
20+ function _interopRequireWildcard ( obj ) { if ( obj && obj . __esModule ) { return obj ; } if ( obj === null || _typeof ( obj ) !== "object" && typeof obj !== "function" ) { return { default : obj } ; } var cache = _getRequireWildcardCache ( ) ; if ( cache && cache . has ( obj ) ) { return cache . get ( obj ) ; } var newObj = { } ; var hasPropertyDescriptor = Object . defineProperty && Object . getOwnPropertyDescriptor ; for ( var key in obj ) { if ( Object . prototype . hasOwnProperty . call ( obj , key ) ) { var desc = hasPropertyDescriptor ? Object . getOwnPropertyDescriptor ( obj , key ) : null ; if ( desc && ( desc . get || desc . set ) ) { Object . defineProperty ( newObj , key , desc ) ; } else { newObj [ key ] = obj [ key ] ; } } } newObj . default = obj ; if ( cache ) { cache . set ( obj , newObj ) ; } return newObj ; }
21+
22+ function addErrorClass ( child ) {
23+ if ( ! child || ! child . props ) return child ;
24+ var wrappedClassName = ( 0 , _classnames . default ) ( child . props . className , {
25+ 'is-invalid' : this . hasErrors
26+ } ) ;
27+ return ( 0 , _react . cloneElement ) ( child , {
28+ className : wrappedClassName
29+ } ) ;
30+ }
31+
32+ function renderErrors ( errors ) {
33+ if ( typeof errors === 'string' ) {
34+ return errors ;
35+ }
36+
37+ return _react . default . createElement ( "ol" , {
38+ className : "invalid-feedback__list"
39+ } , // eslint-disable-next-line react/no-array-index-key
40+ errors . map ( function ( e , idx ) {
41+ return _react . default . createElement ( "li" , {
42+ key : idx
43+ } , e ) ;
44+ } ) ) ;
45+ }
46+
47+ function FormControl ( props ) {
48+ var errors = props . errors ,
49+ inputKey = props . inputKey ;
50+ var hasErrors = errors [ inputKey ] && errors [ inputKey ] . length > 0 ;
51+ return _react . default . createElement ( "div" , {
52+ className : ( 0 , _classnames . default ) ( 'FormControl' , props . inputClassName )
53+ } , _react . Children . map ( props . children , addErrorClass , {
54+ hasErrors : hasErrors
55+ } ) , props . displayErrorText && hasErrors && _react . default . createElement ( "div" , {
56+ className : "invalid-feedback"
57+ } , renderErrors ( errors [ inputKey ] ) ) , props . description && _react . default . createElement ( "div" , {
58+ className : "form-input__description"
59+ } , props . description ) ) ;
60+ }
61+
62+ FormControl . defaultProps = {
63+ children : undefined ,
64+ description : '' ,
65+ displayErrorText : true ,
66+ errors : { } ,
67+ inputClassName : '' ,
68+ inputKey : null
69+ } ;
0 commit comments