11import React from 'react' ;
22import Autosuggest from 'react-autosuggest' ;
3- import { StandardEditorProps } from '@grafana/data' ;
3+ import { StandardEditorContext , StandardEditorProps } from '@grafana/data' ;
44import './TypeaheadTextfield.css' ;
55import { PanelSettings } from '../../types' ;
66
77interface Props extends StandardEditorProps < string , PanelSettings > {
88 item : any ;
99 value : string ;
1010 onChange : ( value ?: string ) => void ;
11- context : any ;
11+ context : StandardEditorContext < any > ;
1212}
1313
1414interface State {
1515 item : any ;
1616 value : string ;
1717 onChange : ( value ?: string ) => void ;
18- context : any ;
18+ context : StandardEditorContext < any > ;
1919 suggestions : string [ ] ;
2020}
2121
2222export class TypeaheadTextField extends React . PureComponent < Props , State > {
2323 constructor ( props : Props | Readonly < Props > ) {
2424 super ( props ) ;
2525
26- var value = props . value ;
26+ var { value } = props ;
2727 if ( value === undefined ) {
2828 value = props . item . defaultValue ;
2929 }
@@ -34,47 +34,53 @@ export class TypeaheadTextField extends React.PureComponent<Props, State> {
3434 } ;
3535 }
3636
37- renderSuggestion ( suggestion : any ) {
37+ renderSuggestion ( suggestion : string ) {
3838 return < div > { suggestion } </ div > ;
3939 }
4040
41- getColumns ( ) {
41+ getColumnNames ( ) {
4242 var { data } = this . props . context ;
4343 var series ;
4444 var columnNames = [ ] ;
4545 if ( data !== undefined && data . length > 0 ) {
4646 series = data [ 0 ] . fields ;
4747 for ( const index in series ) {
4848 const field = series [ index ] ;
49- if ( field . config !== undefined && field . config . displayName !== undefined ) {
50- columnNames . push ( field . config . displayName ) ;
49+ const { config, name } = field ;
50+ if ( config !== undefined && config . displayName !== undefined ) {
51+ columnNames . push ( config . displayName ) ;
5152 } else {
52- columnNames . push ( field . name ) ;
53+ columnNames . push ( name ) ;
5354 }
5455 }
5556 }
5657 return columnNames ;
5758 }
5859
59- onChange = ( event : any , { newValue, method } : any ) => {
60+ onChange = ( event : React . FormEvent < HTMLInputElement > , { newValue } : { newValue : string } ) => {
61+ //TODO make this type nicer!
62+ const { path } = this . props . item ;
63+ const { value } = event . currentTarget ;
6064 this . setState ( {
61- value : event . currentTarget . value ,
65+ value : value ,
6266 } ) ;
63- this . props . onChange . call ( this . props . item . path , newValue ) ;
67+ this . props . onChange . call ( path , newValue ) ;
6468 } ;
6569
66- getSuggestions = ( value : any ) => {
70+ getSuggestions = ( value : string ) => {
6771 var inputValue = '' ;
68- if ( value . value !== undefined ) {
72+ if ( value !== undefined ) {
6973 return [ ] ;
7074 }
7175 if ( value !== undefined && value !== null && value !== '' ) {
7276 inputValue = value . trim ( ) . toLowerCase ( ) ;
7377 }
78+
7479 const inputLength = inputValue . length ;
75- return inputLength === 0
76- ? [ ]
77- : this . getColumns ( ) . filter ( column => column . toLowerCase ( ) . slice ( 0 , inputLength ) === inputValue ) ;
80+ if ( inputLength === 0 || inputValue === undefined ) {
81+ return [ ] ;
82+ }
83+ return this . getColumnNames ( ) . filter ( columnName => columnName . toLowerCase ( ) . startsWith ( inputValue ) ) ;
7884 } ;
7985
8086 onSuggestionsFetchRequested = ( value : any ) => {
@@ -83,7 +89,7 @@ export class TypeaheadTextField extends React.PureComponent<Props, State> {
8389 } ) ;
8490 } ;
8591
86- getSuggestionValue = ( suggestion : any ) => {
92+ getSuggestionValue = ( suggestion : string ) => {
8793 return suggestion ;
8894 } ;
8995
@@ -94,7 +100,7 @@ export class TypeaheadTextField extends React.PureComponent<Props, State> {
94100 } ;
95101
96102 render ( ) {
97- var value = this . props . value ;
103+ var { value } = this . props ;
98104 if ( value === undefined ) {
99105 value = this . props . item . defaultValue ;
100106 }
0 commit comments