-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathfield_select.tsx
227 lines (209 loc) · 7.33 KB
/
field_select.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import './field_select.scss';
import { partition } from 'lodash';
import React, { useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import {
EuiComboBox,
EuiFlexGroup,
EuiFlexItem,
EuiComboBoxOptionOption,
EuiComboBoxProps,
} from '@elastic/eui';
import classNames from 'classnames';
import { EuiHighlight } from '@elastic/eui';
import { OperationType } from '../indexpattern';
import { LensFieldIcon } from '../lens_field_icon';
import { DataType } from '../../types';
import { OperationSupportMatrix } from './operation_support';
import { IndexPattern, IndexPatternPrivateState } from '../types';
import { trackUiEvent } from '../../lens_ui_telemetry';
import { fieldExists } from '../pure_helpers';
export interface FieldChoice {
type: 'field';
field: string;
operationType: OperationType;
}
export interface FieldSelectProps extends EuiComboBoxProps<EuiComboBoxOptionOption['value']> {
currentIndexPattern: IndexPattern;
selectedOperationType?: OperationType;
selectedField?: string;
incompleteOperation?: OperationType;
operationSupportMatrix: OperationSupportMatrix;
onChoose: (choice: FieldChoice) => void;
onDeleteColumn?: () => void;
existingFields: IndexPatternPrivateState['existingFields'];
fieldIsInvalid: boolean;
markAllFieldsCompatible?: boolean;
}
export function FieldSelect({
currentIndexPattern,
incompleteOperation,
selectedOperationType,
selectedField,
operationSupportMatrix,
onChoose,
onDeleteColumn,
existingFields,
fieldIsInvalid,
markAllFieldsCompatible,
...rest
}: FieldSelectProps) {
const { operationByField } = operationSupportMatrix;
const memoizedFieldOptions = useMemo(() => {
const fields = Object.keys(operationByField).sort();
const currentOperationType = incompleteOperation ?? selectedOperationType;
function isCompatibleWithCurrentOperation(fieldName: string) {
return !currentOperationType || operationByField[fieldName]!.has(currentOperationType);
}
const [specialFields, normalFields] = partition(
fields,
(field) => currentIndexPattern.getFieldByName(field)?.type === 'document'
);
const containsData = (field: string) =>
currentIndexPattern.getFieldByName(field)?.type === 'document' ||
fieldExists(existingFields, currentIndexPattern.title, field);
function fieldNamesToOptions(items: string[]) {
return items
.filter((field) => currentIndexPattern.getFieldByName(field)?.displayName)
.map((field) => {
return {
label: currentIndexPattern.getFieldByName(field)?.displayName,
value: {
type: 'field',
field,
dataType: currentIndexPattern.getFieldByName(field)?.type,
// Use the operation directly, or choose the first compatible operation.
// All fields are guaranteed to have at least one operation because they
// won't appear in the list otherwise
operationType:
currentOperationType && isCompatibleWithCurrentOperation(field)
? currentOperationType
: operationByField[field]!.values().next().value,
},
exists: containsData(field),
compatible: markAllFieldsCompatible || isCompatibleWithCurrentOperation(field),
};
})
.sort((a, b) => {
if (a.compatible && !b.compatible) {
return -1;
}
if (!a.compatible && b.compatible) {
return 1;
}
return 0;
})
.map(({ label, value, compatible, exists }) => ({
label,
value,
className: classNames({
// eslint-disable-next-line @typescript-eslint/naming-convention
'lnFieldSelect__option--incompatible': !compatible,
// eslint-disable-next-line @typescript-eslint/naming-convention
'lnFieldSelect__option--nonExistant': !exists,
}),
'data-test-subj': `lns-fieldOption${compatible ? '' : 'Incompatible'}-${value.field}`,
}));
}
const [metaFields, nonMetaFields] = partition(
normalFields,
(field) => currentIndexPattern.getFieldByName(field)?.meta
);
const [availableFields, emptyFields] = partition(nonMetaFields, containsData);
const constructFieldsOptions = (fieldsArr: string[], label: string) =>
fieldsArr.length > 0 && {
label,
options: fieldNamesToOptions(fieldsArr),
};
const availableFieldsOptions = constructFieldsOptions(
availableFields,
i18n.translate('xpack.lens.indexPattern.availableFieldsLabel', {
defaultMessage: 'Available fields',
})
);
const emptyFieldsOptions = constructFieldsOptions(
emptyFields,
i18n.translate('xpack.lens.indexPattern.emptyFieldsLabel', {
defaultMessage: 'Empty fields',
})
);
const metaFieldsOptions = constructFieldsOptions(
metaFields,
i18n.translate('xpack.lens.indexPattern.metaFieldsLabel', {
defaultMessage: 'Meta fields',
})
);
return [
...fieldNamesToOptions(specialFields),
availableFieldsOptions,
emptyFieldsOptions,
metaFieldsOptions,
].filter(Boolean);
}, [
incompleteOperation,
selectedOperationType,
currentIndexPattern,
operationByField,
existingFields,
markAllFieldsCompatible,
]);
return (
<EuiComboBox
fullWidth
compressed
isClearable={false}
data-test-subj="indexPattern-dimension-field"
placeholder={i18n.translate('xpack.lens.indexPattern.fieldPlaceholder', {
defaultMessage: 'Field',
})}
options={(memoizedFieldOptions as unknown) as EuiComboBoxOptionOption[]}
isInvalid={Boolean(incompleteOperation || fieldIsInvalid)}
selectedOptions={
((selectedOperationType && selectedField
? [
{
label: fieldIsInvalid
? selectedField
: currentIndexPattern.getFieldByName(selectedField)?.displayName,
value: { type: 'field', field: selectedField },
},
]
: []) as unknown) as EuiComboBoxOptionOption[]
}
singleSelection={{ asPlainText: true }}
onChange={(choices) => {
if (choices.length === 0) {
onDeleteColumn?.();
return;
}
const choice = (choices[0].value as unknown) as FieldChoice;
if (choice.field !== selectedField) {
trackUiEvent('indexpattern_dimension_field_changed');
onChoose(choice);
}
}}
renderOption={(option, searchValue) => {
return (
<EuiFlexGroup gutterSize="s" alignItems="center" responsive={false}>
<EuiFlexItem grow={null}>
<LensFieldIcon
type={((option.value as unknown) as { dataType: DataType }).dataType}
fill="none"
/>
</EuiFlexItem>
<EuiFlexItem>
<EuiHighlight search={searchValue}>{option.label}</EuiHighlight>
</EuiFlexItem>
</EuiFlexGroup>
);
}}
{...rest}
/>
);
}