-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Runtime fields editor] Expose editor for consuming apps #82116
Merged
sebelga
merged 19 commits into
elastic:feature/runtime-field-editor
from
sebelga:runtime-fields/flyout-component
Nov 3, 2020
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f7f52f9
Create basic runtime field editor + add documentation lib
sebelga e909564
Export editor instead of form
sebelga 061f16f
Add RuntimeFieldEditorFlyout
sebelga 8c3f735
Add backgroundColor to script editor
sebelga 1f50413
Set link to painless syntax
sebelga f7e8022
Display callout error in flyout footer
sebelga a969923
Use PainlessLang.ID for CodeEditor language
sebelga 58ec343
Use "defaultValue" prop instead of "field"
sebelga 8246c8f
Export RuntimeFieldEditor and RuntimeFieldFormState publicly
sebelga 3727b43
Add README.md
sebelga aba171b
Update README.md
sebelga 7c90cc4
Update plugin-list.asciidoc
sebelga 56ab2bc
Fix typo in test
sebelga f210436
Update plugin size limit
sebelga af0c638
Merge branch 'feature/runtime-field-editor' into runtime-fields/flyou…
kibanamachine 3f4eae2
Merge branch 'runtime-fields/flyout-component' of github.com:sebelga/…
sebelga 14ccc9a
Remove scss override of monaco editor
sebelga d1147c3
Set the CodeEditor width to 100%
sebelga 51d5b5c
Rename RuntimeEditorFlyout -> RuntimeEditorFlyoutContent
sebelga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,136 @@ | ||
# Runtime fields | ||
|
||
Welcome to the home of the runtime field editor and everything related to runtime fields! | ||
|
||
## The runtime field editor | ||
|
||
The runtime field editor is exported in 2 flavours: | ||
|
||
* As the content of a `<EuiFlyout />` | ||
* As a standalone component that you can inline anywhere | ||
|
||
### Content of a `<EuiFlyout />` | ||
|
||
```js | ||
import React, { useState } from 'react'; | ||
import { EuiFlyoutBody, EuiButton } from '@elastic/eui'; | ||
import { RuntimeFieldEditorFlyout, RuntimeField } from '../runtime_fields/public'; | ||
|
||
const MyComponent = () => { | ||
const { docLinksStart } = useCoreContext(); // access the core start service | ||
const [isFlyoutVisilbe, setIsFlyoutVisible] = useState(false); | ||
|
||
const saveRuntimeField = useCallback((field: RuntimeField) => { | ||
// Do something with the field | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<EuiButton onClick={() => setIsFlyoutVisible(true)}>Create field</EuiButton> | ||
|
||
{isFlyoutVisible && ( | ||
<EuiFlyout onClose={() => setIsFlyoutVisible(false)}> | ||
<RuntimeFieldEditorFlyout | ||
onSave={saveRuntimeField} | ||
onCancel={() => setIsFlyoutVisible(false)} | ||
docLinks={docLinksStart} | ||
defaultValue={/*optional runtime field to edit*/} | ||
/> | ||
</EuiFlyout> | ||
)} | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
#### With the `core.overlays.openFlyout` | ||
|
||
As an alternative you can open the flyout with the `core.overlays.openFlyout`. In this case you will need to wrap the editor with the `Provider` from the "kibana_react" plugin as it is a required dependency for the `<CodeEditor />` component. | ||
|
||
```js | ||
import React, { useRef } from 'react'; | ||
import { EuiButton } from '@elastic/eui'; | ||
import { OverlayRef } from 'src/core/public'; | ||
|
||
import { createKibanaReactContext, toMountPoint } from '../../src/plugins/kibana_react/public'; | ||
import { RuntimeFieldEditorFlyout, RuntimeField } from '../runtime_fields/public'; | ||
|
||
const MyComponent = () => { | ||
// Access the core start service | ||
const { docLinksStart, overlays, uiSettings } = useCoreContext(); | ||
const flyoutEditor = useRef<OverlayRef | null>(null); | ||
|
||
const { openFlyout } = overlays; | ||
|
||
const saveRuntimeField = useCallback((field: RuntimeField) => { | ||
// Do something with the field | ||
}, []); | ||
|
||
const openRuntimeFieldEditor = useCallback(() => { | ||
const { Provider: KibanaReactContextProvider } = createKibanaReactContext({ uiSettings }); | ||
|
||
flyoutEditor.current = openFlyout( | ||
toMountPoint( | ||
<KibanaReactContextProvider> | ||
<RuntimeFieldEditorFlyout | ||
onSave={saveRuntimeField} | ||
onCancel={() => flyoutEditor.current?.close()} | ||
docLinks={docLinksStart} | ||
defaultValue={defaultRuntimeField} | ||
/> | ||
</KibanaReactContextProvider> | ||
) | ||
); | ||
}, [openFlyout, saveRuntimeField, uiSettings]); | ||
|
||
return ( | ||
<> | ||
<EuiButton onClick={openRuntimeFieldEditor}>Create field</EuiButton> | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
### Standalone component | ||
|
||
```js | ||
import React, { useState } from 'react'; | ||
import { EuiButton, EuiSpacer } from '@elastic/eui'; | ||
import { RuntimeFieldEditor, RuntimeField, RuntimeFieldFormState } from '../runtime_fields/public'; | ||
|
||
const MyComponent = () => { | ||
const { docLinksStart } = useCoreContext(); // access the core start service | ||
const [runtimeFieldFormState, setRuntimeFieldFormState] = useState<RuntimeFieldFormState>({ | ||
isSubmitted: false, | ||
isValid: undefined, | ||
submit: async() => Promise.resolve({ isValid: false, data: {} as RuntimeField }) | ||
}); | ||
|
||
const { submit, isValid: isFormValid, isSubmitted } = runtimeFieldFormState; | ||
|
||
const saveRuntimeField = useCallback(async () => { | ||
const { isValid, data } = await submit(); | ||
if (isValid) { | ||
// Do something with the field (data) | ||
} | ||
}, [submit]); | ||
|
||
return ( | ||
<> | ||
<RuntimeFieldEditor | ||
onChange={setRuntimeFieldFormState} | ||
docLinks={docLinksStart} | ||
defaultValue={/*optional runtime field to edit*/} | ||
/> | ||
|
||
<EuiSpacer /> | ||
|
||
<EuiButton | ||
onClick={saveRuntimeField} | ||
disabled={isSubmitted && !isFormValid}> | ||
Save field | ||
</EuiButton> | ||
</> | ||
) | ||
} | ||
``` |
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
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/runtime_fields/public/components/runtime_field_editor/index.ts
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,7 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { RuntimeFieldEditor } from './runtime_field_editor'; |
71 changes: 71 additions & 0 deletions
71
...ugins/runtime_fields/public/components/runtime_field_editor/runtime_field_editor.test.tsx
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,71 @@ | ||
/* | ||
* 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 { act } from 'react-dom/test-utils'; | ||
import { DocLinksStart } from 'src/core/public'; | ||
|
||
import '../../__jest__/setup_environment'; | ||
import { registerTestBed, TestBed } from '../../test_utils'; | ||
import { RuntimeField } from '../../types'; | ||
import { RuntimeFieldForm, FormState } from '../runtime_field_form/runtime_field_form'; | ||
import { RuntimeFieldEditor, Props } from './runtime_field_editor'; | ||
|
||
const setup = (props?: Props) => | ||
registerTestBed(RuntimeFieldEditor, { | ||
memoryRouter: { | ||
wrapComponent: false, | ||
}, | ||
})(props) as TestBed; | ||
|
||
const docLinks: DocLinksStart = { | ||
ELASTIC_WEBSITE_URL: 'https://jestTest.elastic.co', | ||
DOC_LINK_VERSION: 'jest', | ||
links: {} as any, | ||
}; | ||
|
||
describe('Runtime field editor', () => { | ||
let testBed: TestBed; | ||
let onChange: jest.Mock<Props['onChange']> = jest.fn(); | ||
|
||
const lastOnChangeCall = (): FormState[] => onChange.mock.calls[onChange.mock.calls.length - 1]; | ||
|
||
beforeEach(() => { | ||
onChange = jest.fn(); | ||
}); | ||
|
||
test('should render the <RuntimeFieldForm />', () => { | ||
testBed = setup({ docLinks }); | ||
const { component } = testBed; | ||
|
||
expect(component.find(RuntimeFieldForm).length).toBe(1); | ||
}); | ||
|
||
test('should accept a defaultValue and onChange prop to forward the form state', async () => { | ||
const defaultValue: RuntimeField = { | ||
name: 'foo', | ||
type: 'date', | ||
script: 'test=123', | ||
}; | ||
testBed = setup({ onChange, defaultValue, docLinks }); | ||
|
||
expect(onChange).toHaveBeenCalled(); | ||
|
||
let lastState = lastOnChangeCall()[0]; | ||
expect(lastState.isValid).toBe(undefined); | ||
expect(lastState.isSubmitted).toBe(false); | ||
expect(lastState.submit).toBeDefined(); | ||
|
||
let data; | ||
await act(async () => { | ||
({ data } = await lastState.submit()); | ||
}); | ||
expect(data).toEqual(defaultValue); | ||
|
||
// Make sure that both isValid and isSubmitted state are now "true" | ||
lastState = lastOnChangeCall()[0]; | ||
expect(lastState.isValid).toBe(true); | ||
expect(lastState.isSubmitted).toBe(true); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
...ck/plugins/runtime_fields/public/components/runtime_field_editor/runtime_field_editor.tsx
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,24 @@ | ||
/* | ||
* 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 { DocLinksStart } from 'src/core/public'; | ||
|
||
import { RuntimeField } from '../../types'; | ||
import { getLinks } from '../../lib'; | ||
import { RuntimeFieldForm, Props as FormProps } from '../runtime_field_form/runtime_field_form'; | ||
|
||
export interface Props { | ||
docLinks: DocLinksStart; | ||
defaultValue?: RuntimeField; | ||
onChange?: FormProps['onChange']; | ||
} | ||
|
||
export const RuntimeFieldEditor = ({ defaultValue, onChange, docLinks }: Props) => { | ||
const links = getLinks(docLinks); | ||
|
||
return <RuntimeFieldForm links={links} defaultValue={defaultValue} onChange={onChange} />; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, this component only renders the |
||
}; |
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/runtime_fields/public/components/runtime_field_editor_flyout/index.ts
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,7 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { RuntimeFieldEditorFlyout } from './runtime_field_editor_flyout'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I recall us talking about something similar to this in the past but why is the
RuntimeFieldEditorFlyout
wrapped inEuiFlyout
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it can be injected in a flyout when calling
core.overalys.openFlyout()
. It is a similar mechanism that I put in place for the<GlobalFlyout />
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe renaming
RuntimeFieldEditorFlyout
toRuntimeFieldEditorFlyoutContent
would make the relationship clearer? Otherwise it is a bit of a head-scratcher since the code makes it look like a flyout is being putting inside another flyout.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I will rename the component 👍