Skip to content

Commit

Permalink
Return handler to close flyout
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Nov 4, 2020
1 parent eea0489 commit b392722
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
12 changes: 11 additions & 1 deletion x-pack/plugins/runtime_fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const MyComponent = () => {
// Access the plugin through context
const { runtimeFields } = useAppPlugins();

// Ref for handler to close the editor
const closeRuntimeFieldEditor = useRef(() => {});

const saveRuntimeField = (field: RuntimeField) => {
// Do something with the field
};
Expand All @@ -38,12 +41,19 @@ const MyComponent = () => {
// Lazy load the editor
const { openEditor } = await runtimeFields.loadEditor();

openEditor({
closeRuntimeFieldEditor.current = openEditor({
onSave: saveRuntimeField,
/* defaultValue: optional field to edit */
});
};

useEffect(() => {
return () => {
// Make sure to remove the editor when the component unmounts
closeRuntimeFieldEditor.current();
};
}, []);

return (
<button onClick={openRuntimeFieldsEditor}>Add field</button>
)
Expand Down
9 changes: 8 additions & 1 deletion x-pack/plugins/runtime_fields/public/load_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ export const getRuntimeFieldEditorLoader = (coreSetup: CoreSetup) => async (): P
let overlayRef: OverlayRef | null = null;

const openEditor = ({ onSave, defaultValue }: OpenRuntimeFieldEditorProps) => {
const onSaveField = (field: RuntimeField) => {
const closeEditor = () => {
overlayRef?.close();
overlayRef = null;
};

const onSaveField = (field: RuntimeField) => {
closeEditor();
onSave(field);
};

Expand All @@ -42,6 +47,8 @@ export const getRuntimeFieldEditorLoader = (coreSetup: CoreSetup) => async (): P
</KibanaReactContextProvider>
)
);

return closeEditor;
};

return {
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/runtime_fields/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { StartPlugins, PluginStart } from './types';
import { RuntimeFieldEditorFlyoutContent } from './components';
import { RuntimeFieldsPlugin } from './plugin';

const noop = () => {};

describe('RuntimeFieldsPlugin', () => {
let coreSetup: CoreSetup<StartPlugins, PluginStart>;
let plugin: RuntimeFieldsPlugin;
Expand Down Expand Up @@ -69,4 +71,12 @@ describe('RuntimeFieldsPlugin', () => {
arg.props.children.props.onSave();
expect(onSaveSpy).toHaveBeenCalled();
});

test('should return a handler to close the flyout', async () => {
const setupApi = await plugin.setup(coreSetup, {});
const { openEditor } = await setupApi.loadEditor();

const closeEditorHandler = openEditor({ onSave: noop });
expect(typeof closeEditorHandler).toBe('function');
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/runtime_fields/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RUNTIME_FIELD_TYPES } from './constants';
import { OpenRuntimeFieldEditorProps } from './load_editor';

export interface LoadEditorResponse {
openEditor(props: OpenRuntimeFieldEditorProps): void;
openEditor(props: OpenRuntimeFieldEditorProps): () => void;
}

export interface PluginSetup {
Expand Down

0 comments on commit b392722

Please sign in to comment.