Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lodash template with static react renderer for discover's row formatter #98072

Merged
merged 4 commits into from
Apr 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
* Side Public License, v 1.
*/

import { template } from 'lodash';
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom/server';
import { MAX_DOC_FIELDS_DISPLAYED } from '../../../../common';
import { getServices, IndexPattern } from '../../../kibana_services';

function noWhiteSpace(html: string) {
const TAGS_WITH_WS = />\s+</g;
return html.replace(TAGS_WITH_WS, '><');
interface Props {
defPairs: Array<[string, unknown]>;
}

const templateHtml = `
<dl class="source truncate-by-height">
<% defPairs.forEach(function (def) { %>
<dt><%- def[0] %>:</dt>
<dd><%= def[1] %></dd>
<%= ' ' %>
<% }); %>
</dl>`;
export const doTemplate = template(noWhiteSpace(templateHtml));
const TemplateComponent = ({ defPairs }: Props) => {
return (
<dl className={'source truncate-by-height'}>
{defPairs.map((pair, idx) => (
<Fragment key={idx}>
<dt>{pair[0]}:</dt>
<dd
dangerouslySetInnerHTML={{ __html: `${pair[1]}` }} // eslint-disable-line react/no-danger
legrego marked this conversation as resolved.
Show resolved Hide resolved
/>{' '}
</Fragment>
))}
</dl>
);
};

export const formatRow = (hit: Record<string, any>, indexPattern: IndexPattern) => {
const highlights = hit?.highlight ?? {};
Expand All @@ -38,7 +42,9 @@ export const formatRow = (hit: Record<string, any>, indexPattern: IndexPattern)
pairs.push([displayKey ? displayKey : key, val]);
});
const maxEntries = getServices().uiSettings.get(MAX_DOC_FIELDS_DISPLAYED);
return doTemplate({ defPairs: [...highlightPairs, ...sourcePairs].slice(0, maxEntries) });
return ReactDOM.renderToStaticMarkup(
<TemplateComponent defPairs={[...highlightPairs, ...sourcePairs].slice(0, maxEntries)} />
);
};

export const formatTopLevelObject = (
Expand Down Expand Up @@ -70,5 +76,7 @@ export const formatTopLevelObject = (
pairs.push([displayKey ? displayKey : key, formatted]);
});
const maxEntries = getServices().uiSettings.get(MAX_DOC_FIELDS_DISPLAYED);
return doTemplate({ defPairs: [...highlightPairs, ...sourcePairs].slice(0, maxEntries) });
return ReactDOM.renderToStaticMarkup(
<TemplateComponent defPairs={[...highlightPairs, ...sourcePairs].slice(0, maxEntries)} />
);
};