/* * 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, { useMemo, useState } from 'react'; import { EmbeddableRenderer } from '@kbn/embeddable-plugin/public'; import { EuiCodeBlock, EuiFlexGroup, EuiFlexItem, EuiSpacer, EuiSuperDatePicker, EuiSwitch, EuiText, OnTimeChangeProps, } from '@elastic/eui'; import { BehaviorSubject, Subject } from 'rxjs'; import { TimeRange } from '@kbn/es-query'; import { PublishesDataLoading, useStateFromPublishingSubject } from '@kbn/presentation-publishing'; import { SearchEmbeddableRenderer } from '../react_embeddables/search/search_embeddable_renderer'; import { SEARCH_EMBEDDABLE_TYPE } from '../react_embeddables/search/constants'; import type { SearchApi, SearchSerializedState } from '../react_embeddables/search/types'; function DatePicker({ dataLoading$, onReload, timeRange, setTimeRange, }: { dataLoading$: PublishesDataLoading['dataLoading$']; timeRange: TimeRange; setTimeRange: (timeRange: TimeRange) => void; onReload: () => void; }) { const dataLoading = useStateFromPublishingSubject(dataLoading$); return ( { setTimeRange({ from: start, to: end, }); }} onRefresh={() => { onReload(); }} /> ); } export const RenderExamples = () => { const parentApi = useMemo(() => { const timeRange$ = new BehaviorSubject({ from: 'now-24h', to: 'now', }); const reload$ = new Subject(); return { reload$, onReload: () => { reload$.next(); }, getSerializedStateForChild: () => ({ rawState: { timeRange: undefined, }, }), timeRange$, setTimeRange: (timeRange: TimeRange | undefined) => { if (timeRange) timeRange$.next(timeRange); }, }; // only run onMount }, []); const [api, setApi] = useState(null); const [hidePanelChrome, setHidePanelChrome] = useState(false); const timeRange = useStateFromPublishingSubject(parentApi.timeRange$); return (
{api && ( )}

Use EmbeddableRenderer to render embeddables.

{` type={SEARCH_EMBEDDABLE_TYPE} getParentApi={() => parentApi} onApiAvailable={(newApi) => { setApi(newApi); }} hidePanelChrome={hidePanelChrome} />`} setHidePanelChrome(e.target.checked)} /> key={hidePanelChrome ? 'hideChrome' : 'showChrome'} type={SEARCH_EMBEDDABLE_TYPE} getParentApi={() => parentApi} onApiAvailable={(newApi) => { setApi(newApi); }} hidePanelChrome={hidePanelChrome} />

To avoid leaking embeddable details, wrap EmbeddableRenderer in a component.

{``}
); };