kibana/examples/embeddable_explorer/public/embeddable_panel_example.tsx
Nathan Reese d3d0cdba73
[Presentation] Migrate all usages of EuiPage*_Deprecated (#161496)
closes https://github.com/elastic/kibana/issues/161428

PR also updates examples title. Instead of naming the embeddable used,
the title now reflects what the example demonstrates.
* "Hello world embeddable" -> "Render embeddable"
* "Todo embeddable" -> "Update embeddable state"
* "List container embeddable" -> "Groups of embeddables"
* "Dynamically adding children to a container" -> "Context menu"

There is a lot more that could be done to enhance these examples, but I
did not want to get more side tracked then I already did.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2023-07-10 12:28:30 -06:00

134 lines
3.8 KiB
TypeScript

/*
* 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 React, { useState, useEffect, useRef } from 'react';
import { EuiPanel, EuiText, EuiPageTemplate } from '@elastic/eui';
import { EuiSpacer } from '@elastic/eui';
import { EmbeddableStart, IEmbeddable } from '@kbn/embeddable-plugin/public';
import {
HELLO_WORLD_EMBEDDABLE,
TODO_EMBEDDABLE,
BOOK_EMBEDDABLE,
MULTI_TASK_TODO_EMBEDDABLE,
SearchableListContainerFactory,
} from '@kbn/embeddable-examples-plugin/public';
interface Props {
embeddableServices: EmbeddableStart;
searchListContainerFactory: SearchableListContainerFactory;
}
export function EmbeddablePanelExample({ embeddableServices, searchListContainerFactory }: Props) {
const searchableInput = {
id: '1',
title: 'My searchable todo list',
panels: {
'1': {
type: HELLO_WORLD_EMBEDDABLE,
explicitInput: {
id: '1',
title: 'Hello',
},
},
'2': {
type: TODO_EMBEDDABLE,
explicitInput: {
id: '2',
task: 'Goes out on Wednesdays!',
icon: 'broom',
title: 'Take out the trash',
},
},
'3': {
type: MULTI_TASK_TODO_EMBEDDABLE,
explicitInput: {
id: '3',
icon: 'searchProfilerApp',
title: 'Learn more',
tasks: ['Go to school', 'Watch planet earth', 'Read the encyclopedia'],
},
},
'4': {
type: BOOK_EMBEDDABLE,
explicitInput: {
id: '4',
savedObjectId: 'sample-book-saved-object',
},
},
'5': {
type: BOOK_EMBEDDABLE,
explicitInput: {
id: '5',
attributes: {
title: 'The Sympathizer',
author: 'Viet Thanh Nguyen',
readIt: true,
},
},
},
'6': {
type: BOOK_EMBEDDABLE,
explicitInput: {
id: '6',
attributes: {
title: 'The Hobbit',
author: 'J.R.R. Tolkien',
readIt: false,
},
},
},
},
};
const [embeddable, setEmbeddable] = useState<IEmbeddable | undefined>(undefined);
const ref = useRef(false);
useEffect(() => {
ref.current = true;
if (!embeddable) {
const promise = searchListContainerFactory.create(searchableInput);
if (promise) {
promise.then((e) => {
if (ref.current) {
setEmbeddable(e);
}
});
}
}
return () => {
ref.current = false;
};
});
return (
<>
<EuiPageTemplate.Header pageTitle="Context menu" />
<EuiPageTemplate.Section grow={false}>
<>
<EuiText>
You can render your embeddable inside the EmbeddablePanel component. This adds some
extra rendering and offers a context menu with pluggable actions. Using EmbeddablePanel
to render your embeddable means you get access to the &quot;Add panel flyout&quot;. Now
you can see how to add embeddables to your container, and how
&quot;getExplicitInput&quot; is used to grab input not provided by the container.
</EuiText>
<EuiPanel data-test-subj="embeddedPanelExample" paddingSize="none" role="figure">
{embeddable ? (
<embeddableServices.EmbeddablePanel embeddable={embeddable} />
) : (
<EuiText>Loading...</EuiText>
)}
</EuiPanel>
<EuiSpacer />
</>
</EuiPageTemplate.Section>
</>
);
}