diff --git a/src/plugins/data/common/field_formats/converters/color.test.ts b/src/plugins/data/common/field_formats/converters/color.test.ts index 9ce00db10b28..4b7f2733f56f 100644 --- a/src/plugins/data/common/field_formats/converters/color.test.ts +++ b/src/plugins/data/common/field_formats/converters/color.test.ts @@ -28,10 +28,10 @@ describe('Color Format', () => { expect(colorer.convert(99, HTML_CONTEXT_TYPE)).toBe('99'); expect(colorer.convert(100, HTML_CONTEXT_TYPE)).toBe( - '100' + '100' ); expect(colorer.convert(150, HTML_CONTEXT_TYPE)).toBe( - '150' + '150' ); expect(colorer.convert(151, HTML_CONTEXT_TYPE)).toBe('151'); }); @@ -74,22 +74,22 @@ describe('Color Format', () => { expect(converter('B', HTML_CONTEXT_TYPE)).toBe('B'); expect(converter('AAA', HTML_CONTEXT_TYPE)).toBe( - 'AAA' + 'AAA' ); expect(converter('AB', HTML_CONTEXT_TYPE)).toBe( - 'AB' + 'AB' ); expect(converter('a', HTML_CONTEXT_TYPE)).toBe('a'); expect(converter('B', HTML_CONTEXT_TYPE)).toBe('B'); expect(converter('AAA', HTML_CONTEXT_TYPE)).toBe( - 'AAA' + 'AAA' ); expect(converter('AB', HTML_CONTEXT_TYPE)).toBe( - 'AB' + 'AB' ); expect(converter('AB <', HTML_CONTEXT_TYPE)).toBe( - 'AB <' + 'AB <' ); expect(converter('a', HTML_CONTEXT_TYPE)).toBe('a'); }); diff --git a/src/plugins/data/common/field_formats/converters/color.ts b/src/plugins/data/common/field_formats/converters/color.tsx similarity index 79% rename from src/plugins/data/common/field_formats/converters/color.ts rename to src/plugins/data/common/field_formats/converters/color.tsx index f4603f32acc1..98f25fdf8181 100644 --- a/src/plugins/data/common/field_formats/converters/color.ts +++ b/src/plugins/data/common/field_formats/converters/color.tsx @@ -7,15 +7,15 @@ */ import { i18n } from '@kbn/i18n'; -import { findLast, cloneDeep, template, escape } from 'lodash'; +import React from 'react'; +import ReactDOM from 'react-dom/server'; +import { findLast, cloneDeep, escape } from 'lodash'; import { KBN_FIELD_TYPES } from '../../kbn_field_types/types'; import { FieldFormat } from '../field_format'; import { HtmlContextTypeConvert, FIELD_FORMAT_IDS } from '../types'; import { asPrettyString } from '../utils'; import { DEFAULT_CONVERTER_COLOR } from '../constants/color_default'; -const convertTemplate = template('<%- val %>'); - export class ColorFormat extends FieldFormat { static id = FIELD_FORMAT_IDS.COLOR; static title = i18n.translate('data.fieldFormats.color.title', { @@ -51,11 +51,18 @@ export class ColorFormat extends FieldFormat { htmlConvert: HtmlContextTypeConvert = (val) => { const color = this.findColorRuleForVal(val) as typeof DEFAULT_CONVERTER_COLOR; - if (!color) return escape(asPrettyString(val)); - let style = ''; - if (color.text) style += `color: ${color.text};`; - if (color.background) style += `background-color: ${color.background};`; - return convertTemplate({ val, style }); + const displayVal = escape(asPrettyString(val)); + if (!color) return displayVal; + + return ReactDOM.renderToStaticMarkup( + + ); }; } diff --git a/src/plugins/data/common/field_formats/converters/source.test.ts b/src/plugins/data/common/field_formats/converters/source.test.ts index f0576142892e..655cf315a05a 100644 --- a/src/plugins/data/common/field_formats/converters/source.test.ts +++ b/src/plugins/data/common/field_formats/converters/source.test.ts @@ -9,6 +9,7 @@ import { SourceFormat } from './source'; import { HtmlContextTypeConvert } from '../types'; import { HTML_CONTEXT_TYPE } from '../content_types'; +import { stubIndexPatternWithFields } from '../../index_patterns/index_pattern.stub'; describe('Source Format', () => { let convertHtml: Function; @@ -31,4 +32,19 @@ describe('Source Format', () => { '{"foo":"bar","number":42,"hello":"<h1>World</h1>","also":"with \\"quotes\\" or 'single quotes'"}' ); }); + + test('should render a description list if a field is passed', () => { + const hit = { + foo: 'bar', + number: 42, + hello: '

World

', + also: 'with "quotes" or \'single quotes\'', + }; + + const indexPattern = { ...stubIndexPatternWithFields, formatHit: (h: string) => h }; + + expect(convertHtml(hit, { field: 'field', indexPattern, hit })).toMatchInlineSnapshot( + `"
foo:
bar
number:
42
hello:

World

also:
with \\"quotes\\" or 'single quotes'
"` + ); + }); }); diff --git a/src/plugins/data/common/field_formats/converters/source.ts b/src/plugins/data/common/field_formats/converters/source.tsx similarity index 62% rename from src/plugins/data/common/field_formats/converters/source.ts rename to src/plugins/data/common/field_formats/converters/source.tsx index bacfc1ab4c73..d6176b321f3f 100644 --- a/src/plugins/data/common/field_formats/converters/source.ts +++ b/src/plugins/data/common/field_formats/converters/source.tsx @@ -6,40 +6,34 @@ * Side Public License, v 1. */ -import { template, escape, keys } from 'lodash'; +import React, { Fragment } from 'react'; +import ReactDOM from 'react-dom/server'; +import { escape, keys } from 'lodash'; import { shortenDottedString } from '../../utils'; import { KBN_FIELD_TYPES } from '../../kbn_field_types/types'; import { FieldFormat } from '../field_format'; import { TextContextTypeConvert, HtmlContextTypeConvert, FIELD_FORMAT_IDS } from '../types'; import { UI_SETTINGS } from '../../constants'; -/** - * Remove all of the whitespace between html tags - * so that inline elements don't have extra spaces. - * - * If you have inline elements (span, a, em, etc.) and any - * amount of whitespace around them in your markup, then the - * browser will push them apart. This is ugly in certain - * scenarios and is only fixed by removing the whitespace - * from the html in the first place (or ugly css hacks). - * - * @param {string} html - the html to modify - * @return {string} - modified html - */ -function noWhiteSpace(html: string) { - const TAGS_WITH_WS = />\s+<'); +interface Props { + defPairs: Array<[string, string]>; } - -const templateHtml = ` -
- <% defPairs.forEach(function (def) { %> -
<%- def[0] %>:
-
<%= def[1] %>
- <%= ' ' %> - <% }); %> -
`; -const doTemplate = template(noWhiteSpace(templateHtml)); +const TemplateComponent = ({ defPairs }: Props) => { + return ( +
+ {defPairs.map((pair, idx) => ( + +
+
{' '} + + ))} +
+ ); +}; export class SourceFormat extends FieldFormat { static id = FIELD_FORMAT_IDS._SOURCE; @@ -70,6 +64,8 @@ export class SourceFormat extends FieldFormat { pairs.push([newField, val]); }, []); - return doTemplate({ defPairs: highlightPairs.concat(sourcePairs) }); + return ReactDOM.renderToStaticMarkup( + + ); }; }