/* * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ import React from 'react'; import { EuiBasicTable, EuiCallOut, EuiCode, EuiCodeBlock, EuiLink, EuiPageTemplate, EuiProvider, EuiSpacer, EuiText, EuiTitle, } from '@elastic/eui'; import { FieldFormatsStart } from '@kbn/field-formats-plugin/public'; import * as example1 from './examples/1_using_existing_format'; import * as example2 from './examples/2_creating_custom_formatter'; // @ts-ignore import example1SampleCode from '!!raw-loader!./examples/1_using_existing_format'; // @ts-ignore import example2SampleCodePart1 from '!!raw-loader!../common/example_currency_format'; // @ts-ignore import example2SampleCodePart2 from '!!raw-loader!./examples/2_creating_custom_formatter'; // @ts-ignore import example2SampleCodePart3 from '!!raw-loader!../server/examples/2_creating_custom_formatter'; // @ts-ignore import example3SampleCode from '!!raw-loader!./examples/3_creating_custom_format_editor'; export interface Deps { fieldFormats: FieldFormatsStart; /** * Just for demo purposes */ openDateViewNumberFieldEditor: () => void; } const UsingAnExistingFieldFormatExample: React.FC<{ deps: Deps }> = (props) => { const sample = example1.getSample(props.deps.fieldFormats); return ( <>

This example shows how to use existing field formatter to format values. As an example, we have a following sample configuration{' '} {JSON.stringify(example1.sampleSerializedFieldFormat)} representing a{' '} bytes field formatter with a 0.00b pattern.

{example1SampleCode} ); }; const CreatingCustomFieldFormat: React.FC<{ deps: Deps }> = (props) => { const sample = example2.getSample(props.deps.fieldFormats); return ( <>

This example shows how to create a custom field formatter. As an example, we create a currency formatter and then display some values as USD. Custom field formatter has to be registered both client and server side.

{example2SampleCodePart1} {example2SampleCodePart2} {example2SampleCodePart3}

Currency formatter that we've just created is already integrated with data views. It can be applied to any numeric field of any data view.{' '} props.deps.openDateViewNumberFieldEditor()}> Open data view field editor to give it a try.

); }; const CreatingCustomFieldFormatEditor: React.FC<{ deps: Deps }> = (props) => { return ( <>

This example shows how to create a custom field formatter editor. As an example, we will create a format editor for the currency formatter created in the previous section. This custom editor will allow to select either USD or EUR{' '} currency.

{example3SampleCode}

Currency formatter and its custom editor are integrated with data views. It can be applied to any numeric field of any data view.{' '} props.deps.openDateViewNumberFieldEditor()}> Open date view field editor to give it a try.

); }; export const App: React.FC<{ deps: Deps }> = (props) => { return (

Using an existing field format

Creating a custom field format

Creating a custom field format editor

); };