-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose handler from plugin setup() to load the editor
- Loading branch information
Showing
6 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React from 'react'; | ||
import { CoreSetup, OverlayRef } from 'src/core/public'; | ||
|
||
import { toMountPoint, createKibanaReactContext } from './shared_imports'; | ||
import { LoadEditorResponse, RuntimeField } from './types'; | ||
|
||
export interface OpenRuntimeFieldEditorProps { | ||
onSave(field: RuntimeField): void; | ||
defaultValue?: RuntimeField; | ||
} | ||
|
||
export const getRuntimeFieldEditorLoader = (coreSetup: CoreSetup) => async (): Promise< | ||
LoadEditorResponse | ||
> => { | ||
const { RuntimeFieldEditorFlyoutContent } = await import('./components'); | ||
const [core] = await coreSetup.getStartServices(); | ||
const { uiSettings, overlays, docLinks } = core; | ||
const { Provider: KibanaReactContextProvider } = createKibanaReactContext({ uiSettings }); | ||
|
||
let overlayRef: OverlayRef | null = null; | ||
|
||
const openEditor = ({ onSave, defaultValue }: OpenRuntimeFieldEditorProps) => { | ||
const onSaveField = (field: RuntimeField) => { | ||
overlayRef?.close(); | ||
onSave(field); | ||
}; | ||
|
||
overlayRef = overlays.openFlyout( | ||
toMountPoint( | ||
<KibanaReactContextProvider> | ||
<RuntimeFieldEditorFlyoutContent | ||
onSave={onSaveField} | ||
onCancel={() => overlayRef?.close()} | ||
docLinks={docLinks} | ||
defaultValue={defaultValue} | ||
/> | ||
</KibanaReactContextProvider> | ||
) | ||
); | ||
}; | ||
|
||
return { | ||
openEditor, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { CoreSetup } from 'src/core/public'; | ||
import { coreMock } from 'src/core/public/mocks'; | ||
|
||
import { StartPlugins, PluginStart } from './types'; | ||
import { RuntimeFieldsPlugin } from './plugin'; | ||
|
||
describe('RuntimeFieldsPlugin', () => { | ||
const noop = () => {}; | ||
let coreSetup: CoreSetup<StartPlugins, PluginStart>; | ||
let plugin: RuntimeFieldsPlugin; | ||
|
||
beforeEach(() => { | ||
plugin = new RuntimeFieldsPlugin(); | ||
coreSetup = coreMock.createSetup(); | ||
}); | ||
|
||
test('should return a handler to load the runtime field editor', async () => { | ||
const setupApi = await plugin.setup(coreSetup, {}); | ||
expect(setupApi.loadEditor).toBeDefined(); | ||
}); | ||
|
||
test('once it is loaded it should expose a handler to open the editor', async () => { | ||
const setupApi = await plugin.setup(coreSetup, {}); | ||
const response = await setupApi.loadEditor(); | ||
expect(response.openEditor).toBeDefined(); | ||
}); | ||
|
||
test('should call core.overlays.openFlyout when opening the editor', async () => { | ||
const openFlyout = jest.fn(); | ||
const mockCore = { | ||
overlays: { | ||
openFlyout, | ||
}, | ||
uiSettings: {}, | ||
}; | ||
coreSetup.getStartServices = async () => [mockCore] as any; | ||
const setupApi = await plugin.setup(coreSetup, {}); | ||
const { openEditor } = await setupApi.loadEditor(); | ||
|
||
openEditor({ onSave: noop }); | ||
|
||
expect(openFlyout).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters