44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7- import PropTypes from 'prop-types' ;
87import React from 'react' ;
98
10- import { EuiComboBox , EuiHighlight , EuiFlexGroup , EuiFlexItem } from '@elastic/eui' ;
11- import { FIELD_ORIGIN } from '../../../../../common/constants' ;
9+ import {
10+ EuiComboBox ,
11+ EuiComboBoxProps ,
12+ EuiComboBoxOptionOption ,
13+ EuiHighlight ,
14+ EuiFlexGroup ,
15+ EuiFlexItem ,
16+ } from '@elastic/eui' ;
1217import { i18n } from '@kbn/i18n' ;
18+ import { FIELD_ORIGIN , VECTOR_STYLES } from '../../../../../common/constants' ;
1319import { FieldIcon } from '../../../../../../../../src/plugins/kibana_react/public' ;
20+ import { StyleField } from '../style_fields_helper' ;
1421
15- function renderOption ( option , searchValue , contentClassName ) {
22+ function renderOption (
23+ option : EuiComboBoxOptionOption < StyleField > ,
24+ searchValue : string ,
25+ contentClassName : string
26+ ) {
27+ const fieldIcon = option . value ? < FieldIcon type = { option . value . type } fill = "none" /> : null ;
1628 return (
1729 < EuiFlexGroup className = { contentClassName } gutterSize = "s" alignItems = "center" >
18- < EuiFlexItem grow = { null } >
19- < FieldIcon type = { option . value . type } fill = "none" />
20- </ EuiFlexItem >
30+ < EuiFlexItem grow = { null } > { fieldIcon } </ EuiFlexItem >
2131 < EuiFlexItem >
2232 < EuiHighlight search = { searchValue } > { option . label } </ EuiHighlight >
2333 </ EuiFlexItem >
2434 </ EuiFlexGroup >
2535 ) ;
2636}
2737
28- function groupFieldsByOrigin ( fields ) {
29- const fieldsByOriginMap = new Map ( ) ;
38+ function groupFieldsByOrigin ( fields : StyleField [ ] ) {
39+ const fieldsByOriginMap = new Map < FIELD_ORIGIN , StyleField [ ] > ( ) ;
3040 fields . forEach ( ( field ) => {
3141 if ( fieldsByOriginMap . has ( field . origin ) ) {
3242 const fieldsList = fieldsByOriginMap . get ( field . origin ) ;
33- fieldsList . push ( field ) ;
34- fieldsByOriginMap . set ( field . origin , fieldsList ) ;
43+ fieldsList ! . push ( field ) ;
44+ fieldsByOriginMap . set ( field . origin , fieldsList ! ) ;
3545 } else {
3646 fieldsByOriginMap . set ( field . origin , [ field ] ) ;
3747 }
3848 } ) ;
3949
40- function fieldsListToOptions ( fieldsList ) {
50+ function fieldsListToOptions ( fieldsList : StyleField [ ] ) {
4151 return fieldsList
4252 . map ( ( field ) => {
4353 return { value : field , label : field . label } ;
@@ -51,10 +61,13 @@ function groupFieldsByOrigin(fields) {
5161 // do not show origin group if all fields are from same origin
5262 const onlyOriginKey = fieldsByOriginMap . keys ( ) . next ( ) . value ;
5363 const fieldsList = fieldsByOriginMap . get ( onlyOriginKey ) ;
54- return fieldsListToOptions ( fieldsList ) ;
64+ return fieldsListToOptions ( fieldsList ! ) ;
5565 }
5666
57- const optionGroups = [ ] ;
67+ const optionGroups : Array < {
68+ label : string ;
69+ options : Array < EuiComboBoxOptionOption < StyleField > > ;
70+ } > = [ ] ;
5871 fieldsByOriginMap . forEach ( ( fieldsList , fieldOrigin ) => {
5972 optionGroups . push ( {
6073 label : i18n . translate ( 'xpack.maps.style.fieldSelect.OriginLabel' , {
@@ -65,29 +78,46 @@ function groupFieldsByOrigin(fields) {
6578 } ) ;
6679 } ) ;
6780
68- optionGroups . sort ( ( a , b ) => {
69- return a . label . toLowerCase ( ) . localeCompare ( b . label . toLowerCase ( ) ) ;
70- } ) ;
81+ optionGroups . sort (
82+ ( a : EuiComboBoxOptionOption < StyleField > , b : EuiComboBoxOptionOption < StyleField > ) => {
83+ return a . label . toLowerCase ( ) . localeCompare ( b . label . toLowerCase ( ) ) ;
84+ }
85+ ) ;
7186
7287 return optionGroups ;
7388}
7489
75- export function FieldSelect ( { fields, selectedFieldName, onChange, styleName, ...rest } ) {
76- const onFieldChange = ( selectedFields ) => {
90+ type Props = {
91+ fields : StyleField [ ] ;
92+ selectedFieldName : string ;
93+ onChange : ( { field } : { field : StyleField | null } ) => void ;
94+ styleName : VECTOR_STYLES ;
95+ } & Omit <
96+ EuiComboBoxProps < StyleField > ,
97+ | 'selectedOptions'
98+ | 'options'
99+ | 'onChange'
100+ | 'singleSelection'
101+ | 'isClearable'
102+ | 'fullWidth'
103+ | 'renderOption'
104+ > ;
105+
106+ export function FieldSelect ( { fields, selectedFieldName, onChange, styleName, ...rest } : Props ) {
107+ const onFieldChange = ( selectedFields : Array < EuiComboBoxOptionOption < StyleField > > ) => {
77108 onChange ( {
78- field : selectedFields . length > 0 ? selectedFields [ 0 ] . value : null ,
109+ field : selectedFields . length > 0 && selectedFields [ 0 ] . value ? selectedFields [ 0 ] . value : null ,
79110 } ) ;
80111 } ;
81112
82113 let selectedOption ;
83114 if ( selectedFieldName ) {
84- const field = fields . find ( ( field ) => {
85- return field . name === selectedFieldName ;
115+ const field = fields . find ( ( f ) => {
116+ return f . name === selectedFieldName ;
86117 } ) ;
87- //Do not spread in all the other unused values (e.g. type, supportsAutoDomain etc...)
88118 if ( field ) {
89119 selectedOption = {
90- value : field . value ,
120+ value : field ,
91121 label : field . label ,
92122 } ;
93123 }
@@ -110,15 +140,3 @@ export function FieldSelect({ fields, selectedFieldName, onChange, styleName, ..
110140 />
111141 ) ;
112142}
113-
114- export const fieldShape = PropTypes . shape ( {
115- name : PropTypes . string . isRequired ,
116- origin : PropTypes . oneOf ( Object . values ( FIELD_ORIGIN ) ) . isRequired ,
117- type : PropTypes . string . isRequired ,
118- } ) ;
119-
120- FieldSelect . propTypes = {
121- selectedFieldName : PropTypes . string ,
122- fields : PropTypes . arrayOf ( fieldShape ) . isRequired ,
123- onChange : PropTypes . func . isRequired ,
124- } ;
0 commit comments