1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . FetchTextBox = undefined ;
7
+
8
+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
9
+
10
+ var _react = require ( 'react' ) ;
11
+
12
+ var _react2 = _interopRequireDefault ( _react ) ;
13
+
14
+ require ( './styles.css' ) ;
15
+
16
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
17
+
18
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
19
+
20
+ function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
21
+
22
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
23
+
24
+ // This component is contains the implementation of Fetch Texbox.
25
+
26
+ var FetchTextBox = exports . FetchTextBox = function ( _Component ) {
27
+ _inherits ( FetchTextBox , _Component ) ;
28
+
29
+ function FetchTextBox ( props ) {
30
+ _classCallCheck ( this , FetchTextBox ) ;
31
+
32
+ var _this = _possibleConstructorReturn ( this , ( FetchTextBox . __proto__ || Object . getPrototypeOf ( FetchTextBox ) ) . call ( this , props ) ) ;
33
+
34
+ _this . state = {
35
+ data : [ ] ,
36
+ isLoading : false ,
37
+ value : '' ,
38
+ hideSuggestions : false
39
+ } ;
40
+ return _this ;
41
+ }
42
+
43
+ _createClass ( FetchTextBox , [ {
44
+ key : 'logChange' ,
45
+ value : function logChange ( e ) {
46
+ this . setState ( { value : e . target . value } ) ;
47
+ if ( this . state . value . length >= 2 ) this . fetchResults ( e . target . value ) ;
48
+ try {
49
+ this . sendTextBoxValue ( e . target . value ) ;
50
+ } catch ( ex ) { }
51
+ }
52
+ } , {
53
+ key : 'setTextboxValue' ,
54
+ value : function setTextboxValue ( e ) {
55
+ this . setState ( { hideSuggestions : true , value : e . target . innerHTML } ) ;
56
+ try {
57
+ this . sendTextBoxValue ( e . target . innerHTML ) ;
58
+ } catch ( ex ) { }
59
+ }
60
+ } , {
61
+ key : 'sendTextBoxValue' ,
62
+ value : function sendTextBoxValue ( va ) {
63
+ this . props . sendData ( va ) ;
64
+ }
65
+ } , {
66
+ key : 'fetchResults' ,
67
+ value : function fetchResults ( queryText ) {
68
+ var self = this ;
69
+ var header = new Headers ( {
70
+ 'Content-Type' : 'application/json'
71
+ } ) ;
72
+
73
+ this . setState ( { isLoading : true , hideSuggestions : false } ) ;
74
+
75
+ fetch ( this . props . url + queryText , {
76
+ method : this . props . method
77
+ } ) . then ( function ( response ) {
78
+ if ( response . status >= 400 ) {
79
+ throw new Error ( "Bad response from server" ) ;
80
+ }
81
+ return response . json ( ) ;
82
+ } ) . then ( function ( data ) {
83
+ self . setState ( { data : data [ self . props . jsonArrayKey ] != null ? data [ self . props . jsonArrayKey ] : [ ] , isLoading : false } ) ;
84
+ } ) . catch ( function ( err ) {
85
+ console . log ( 'Error occurred' , err ) ;
86
+ } ) ;
87
+ }
88
+ } , {
89
+ key : 'render' ,
90
+ value : function render ( ) {
91
+ var _this2 = this ;
92
+
93
+ var className = 'form-control' ;
94
+ if ( this . state . isLoading ) {
95
+ className += ' loading-state' ;
96
+ }
97
+
98
+ var suggestionsButtonStyleClassName = 'list-group-item list-group-item-action custom-item' ;
99
+
100
+ if ( this . state . hideSuggestions || this . state . value . length < 2 ) {
101
+ suggestionsButtonStyleClassName = 'hidden' ;
102
+ }
103
+
104
+ return _react2 . default . createElement (
105
+ 'div' ,
106
+ { className : 'form-group' } ,
107
+ _react2 . default . createElement ( 'input' , { id : 'fetch-textbox' , value : this . state . value , onChange : this . logChange . bind ( this ) , placeholder : 'Enter Value' , type : 'text' , className : className } ) ,
108
+ _react2 . default . createElement (
109
+ 'ul' ,
110
+ { className : 'list-group fetch-textbox-suggestions' } ,
111
+ this . state . data . map ( function ( item , key ) {
112
+ return _react2 . default . createElement (
113
+ 'li' ,
114
+ { key : key , onClick : _this2 . setTextboxValue . bind ( _this2 ) , className : suggestionsButtonStyleClassName } ,
115
+ item [ _this2 . props . fieldName ]
116
+ ) ;
117
+ } )
118
+ )
119
+ ) ;
120
+ }
121
+ } ] ) ;
122
+
123
+ return FetchTextBox ;
124
+ } ( _react . Component ) ;
125
+
126
+ exports . default = FetchTextBox ;
0 commit comments