@@ -24,23 +24,36 @@ import {
2424 EuiFormRow ,
2525 EuiFieldText ,
2626 EuiSpacer ,
27+ EuiComboBox ,
2728} from '@elastic/eui' ;
2829import { useKibana } from '../../../src/plugins/kibana_react/public' ;
2930import { CustomFormFilterAccountsVisDependencies } from './plugin' ;
3031import { CustomFormFilterAccountsVisParams } from './types' ;
3132import { removeFiltersByControlledBy , stringToInt , stringToFloat } from './filter_helper' ;
33+ import { fetchData } from './fetch_data' ;
3234
3335const filterControlledBy = 'accountsVis' ;
3436
3537interface CustomFormFilterAccountsVisComponentProps extends CustomFormFilterAccountsVisParams {
3638 renderComplete : ( ) => { } ;
3739}
3840
41+ interface AccountFormState {
42+ age : string ,
43+ minimumBalance : string ,
44+ countryStates : any ,
45+ countryStateSelected : any
46+ }
47+
3948/**
4049 * The CustomFormFilterAccountsVisComponent renders the form.
4150 */
4251class CustomFormFilterAccountsVisComponent extends React . Component < CustomFormFilterAccountsVisComponentProps > {
4352
53+ asyncInitStated : boolean = false ;
54+ isLoading : boolean = true ;
55+ state : AccountFormState ;
56+
4457 /**
4558 * Will be called after the first render when the component is present in the DOM.
4659 *
@@ -63,17 +76,46 @@ class CustomFormFilterAccountsVisComponent extends React.Component<CustomFormFil
6376
6477 constructor ( props : CustomFormFilterAccountsVisComponentProps ) {
6578 super ( props ) ;
79+
6680 removeFiltersByControlledBy ( this . props . filterManager , filterControlledBy ) ;
81+ const initialCountryStates = [ { label : '-- All --' , value : 0 } ] ;
6782 this . state = {
6883 age : "" ,
69- minimumBalance : ""
84+ minimumBalance : "" ,
85+ countryStates : initialCountryStates ,
86+ countryStateSelected : [ initialCountryStates [ 0 ] ] ,
7087 } ;
7188 if ( props . age != null )
7289 this . state . age = String ( props . age ) ;
7390 if ( props . minimumBalance != null )
7491 this . state . minimumBalance = String ( props . minimumBalance ) ;
7592 }
7693
94+ async initControls ( ) {
95+ if ( this . asyncInitStated )
96+ return ;
97+ this . asyncInitStated = true ;
98+ let statesArray = null ;
99+ try {
100+ statesArray = await fetchData ( this . props . coreSetup , "bank*" , "state.keyword" ) ;
101+ statesArray . sort ( ) ;
102+ }
103+ catch ( error ) {
104+ statesArray = null ;
105+ }
106+ if ( ! statesArray )
107+ statesArray = [ ] ;
108+ statesArray . unshift ( '-- All --' ) ;
109+ let countryStates = [ ] ;
110+ for ( let i = 0 ; i < statesArray . length ; i ++ ) {
111+ countryStates . push ( { label : statesArray [ i ] , value : i } ) ;
112+ }
113+ this . isLoading = false ;
114+ this . setState ( {
115+ [ "countryStates" ] : countryStates
116+ } ) ;
117+ }
118+
77119 onClickButtonApplyFilter = ( ) => {
78120 removeFiltersByControlledBy ( this . props . filterManager , filterControlledBy ) ;
79121
@@ -113,6 +155,23 @@ class CustomFormFilterAccountsVisComponent extends React.Component<CustomFormFil
113155 } ;
114156 this . props . filterManager . addFilters ( minimumBalanceFilter ) ;
115157 }
158+
159+ if ( this . state . countryStateSelected [ 0 ] . value != 0 ) {
160+ const stateFilter = {
161+ meta : {
162+ controlledBy : filterControlledBy ,
163+ alias : 'State: ' + this . state . countryStateSelected [ 0 ] . label ,
164+ disabled : false ,
165+ negate : false ,
166+ } ,
167+ query : {
168+ match_phrase : {
169+ state : this . state . countryStateSelected [ 0 ] . label
170+ }
171+ }
172+ } ;
173+ this . props . filterManager . addFilters ( stateFilter ) ;
174+ }
116175 }
117176
118177 onClickButtonDeleteFilter = ( ) => {
@@ -122,6 +181,7 @@ class CustomFormFilterAccountsVisComponent extends React.Component<CustomFormFil
122181 onClickButtonClearForm = ( ) => {
123182 this . state . age = "" ;
124183 this . state . minimumBalance = "" ;
184+ this . state . countryStateSelected = [ this . state . countryStates [ 0 ] ] ;
125185 removeFiltersByControlledBy ( this . props . filterManager , filterControlledBy ) ;
126186 this . forceUpdate ( ) ;
127187 } ;
@@ -142,10 +202,17 @@ class CustomFormFilterAccountsVisComponent extends React.Component<CustomFormFil
142202 } ) ;
143203 } ;
144204
205+ onCountryStateChange = selectedOptions => {
206+ this . setState ( {
207+ [ "countryStateSelected" ] : selectedOptions
208+ } ) ;
209+ } ;
210+
145211 /**
146212 * Render the actual HTML.
147213 */
148214 render ( ) {
215+ this . initControls ( ) ;
149216 const minimumBalanceHelpText = `Input account minimum balance (Maximum is ${ this . props . maximumBalance } )` ;
150217 return (
151218 < div className = "cffVis" >
@@ -157,6 +224,16 @@ class CustomFormFilterAccountsVisComponent extends React.Component<CustomFormFil
157224 < EuiFieldText name = "minimumBalance" onChange = { e => this . onFormChange ( e ) } value = { this . state . minimumBalance } />
158225 </ EuiFormRow >
159226 < EuiSpacer />
227+ < EuiComboBox
228+ placeholder = "Select a state"
229+ isLoading = { this . isLoading }
230+ singleSelection = { { asPlainText : true } }
231+ options = { this . state . countryStates }
232+ selectedOptions = { this . state . countryStateSelected }
233+ onChange = { this . onCountryStateChange }
234+ isClearable = { false }
235+ />
236+ < EuiSpacer />
160237 < EuiButton onClick = { this . onClickButtonApplyFilter } fill > Apply filter</ EuiButton >
161238 < EuiButton onClick = { this . onClickButtonDeleteFilter } > Delete filter</ EuiButton >
162239 < EuiButton onClick = { this . onClickButtonClearForm } > Clear form</ EuiButton >
@@ -192,6 +269,7 @@ export function CustomFormFilterAccountsVisWrapper(props: CustomFormFilterAccoun
192269 renderComplete = { props . renderComplete }
193270 timefilter = { kibana . services . timefilter }
194271 filterManager = { kibana . services . filterManager }
272+ coreSetup = { kibana . services . coreSetup }
195273 />
196274 ) ;
197275}
0 commit comments