Skip to content

Commit

Permalink
[Discover] make field icons consistent across field list and doc tabl…
Browse files Browse the repository at this point in the history
…es (#129621)
  • Loading branch information
drewdaemon authored Apr 11, 2022
1 parent f20218f commit 6954b0f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { i18n } from '@kbn/i18n';
import { UiCounterMetricType } from '@kbn/analytics';
import classNames from 'classnames';
import { FieldButton, FieldIcon } from '@kbn/react-field';
import { getTypeForFieldIcon } from '../../../../utils/get_type_for_field_icon';
import { DiscoverFieldDetails } from './discover_field_details';
import { FieldDetails } from './types';
import type { DataViewField, DataView } from '../../../../../../data_views/public';
Expand Down Expand Up @@ -59,9 +60,10 @@ const FieldInfoIcon: React.FC = memo(() => (
));

const DiscoverFieldTypeIcon: React.FC<{ field: DataViewField }> = memo(({ field }) => {
// If it's a string type, we want to distinguish between keyword and text
const tempType = field.type === 'string' && field.esTypes ? field.esTypes[0] : field.type;
return <FieldIcon type={tempType} label={getFieldTypeName(tempType)} scripted={field.scripted} />;
const typeForIcon = getTypeForFieldIcon(field);
return (
<FieldIcon type={typeForIcon} label={getFieldTypeName(typeForIcon)} scripted={field.scripted} />
);
});

const FieldName: React.FC<{ field: DataViewField }> = memo(({ field }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import '../table.scss';
import React, { useCallback, useMemo } from 'react';
import { EuiInMemoryTable } from '@elastic/eui';
import { getTypeForFieldIcon } from '../../../../../utils/get_type_for_field_icon';
import { useDiscoverServices } from '../../../../../utils/use_discover_services';
import { flattenHit } from '../../../../../../../data/public';
import { SHOW_MULTIFIELDS } from '../../../../../../common';
Expand Down Expand Up @@ -73,7 +74,11 @@ export const DocViewerLegacyTable = ({
.map((field) => {
const fieldMapping = mapping(field);
const displayName = fieldMapping?.displayName ?? field;
const fieldType = isNestedFieldParent(field, dataView) ? 'nested' : fieldMapping?.type;
const fieldType = isNestedFieldParent(field, dataView)
? 'nested'
: fieldMapping
? getTypeForFieldIcon(fieldMapping)
: undefined;
const ignored = getIgnoredReason(fieldMapping ?? field, hit._ignored);
return {
action: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { debounce } from 'lodash';
import { getTypeForFieldIcon } from '../../../../utils/get_type_for_field_icon';
import { useDiscoverServices } from '../../../../utils/use_discover_services';
import { Storage } from '../../../../../../kibana_utils/public';
import { usePager } from '../../../../utils/use_pager';
Expand Down Expand Up @@ -157,7 +158,11 @@ export const DocViewerTable = ({
(field: string) => {
const fieldMapping = mapping(field);
const displayName = fieldMapping?.displayName ?? field;
const fieldType = isNestedFieldParent(field, dataView) ? 'nested' : fieldMapping?.type;
const fieldType = isNestedFieldParent(field, dataView)
? 'nested'
: fieldMapping
? getTypeForFieldIcon(fieldMapping)
: undefined;

const ignored = getIgnoredReason(fieldMapping ?? field, hit._ignored);

Expand Down
39 changes: 39 additions & 0 deletions src/plugins/discover/public/utils/get_type_for_field_icon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DataViewField } from 'src/plugins/data_views/common';
import { getTypeForFieldIcon } from './get_type_for_field_icon';

describe('getTypeForFieldIcon', () => {
it('extracts type for non-string types', () => {
expect(
getTypeForFieldIcon({
type: 'not-string',
esTypes: ['bar'],
} as DataViewField)
).toBe('not-string');
});

it('extracts type when type is string but esTypes is unavailable', () => {
expect(
getTypeForFieldIcon({
type: 'string',
esTypes: undefined,
} as DataViewField)
).toBe('string');
});

it('extracts esType when type is string and esTypes is available', () => {
expect(
getTypeForFieldIcon({
type: 'string',
esTypes: ['version'],
} as DataViewField)
).toBe('version');
});
});
17 changes: 17 additions & 0 deletions src/plugins/discover/public/utils/get_type_for_field_icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { DataViewField } from 'src/plugins/data_views/common';

/**
* Extracts the type from a data view field that will match the right icon.
*
* We define custom logic for Discover in order to distinguish between various "string" types.
*/
export const getTypeForFieldIcon = (field: DataViewField) =>
field.type === 'string' && field.esTypes ? field.esTypes[0] : field.type;

0 comments on commit 6954b0f

Please sign in to comment.