-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add embeddable via saved object example
- Loading branch information
1 parent
29abe5b
commit 5a74919
Showing
31 changed files
with
679 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { TodoSavedObjectAttributes } from './todo_saved_object_attributes'; |
26 changes: 26 additions & 0 deletions
26
examples/embeddable_examples/common/todo_saved_object_attributes.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,26 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { SavedObjectAttributes } from '../../../src/core/types'; | ||
|
||
export interface TodoSavedObjectAttributes extends SavedObjectAttributes { | ||
task: string; | ||
icon?: string; | ||
title?: string; | ||
} |
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,36 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from 'kibana/public'; | ||
import { TodoSavedObjectAttributes } from '../common'; | ||
|
||
export async function createSampleData(client: SavedObjectsClientContract) { | ||
await client.create<TodoSavedObjectAttributes>( | ||
'todo', | ||
{ | ||
task: 'Take the garbage out', | ||
title: 'Garbage', | ||
icon: 'trash', | ||
}, | ||
{ | ||
id: 'sample-todo-saved-object', | ||
overwrite: true, | ||
} | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
There are two examples in here: | ||
- TodoEmbeddable | ||
- TodoRefEmbeddable | ||
|
||
# TodoEmbeddable | ||
|
||
The first example you should review is the HelloWorldEmbeddable. That is as basic an embeddable as you can get. | ||
This embeddable is the next step up - an embeddable that renders dynamic input data. The data is simple: | ||
- a required task string | ||
- an optional title | ||
- an optional icon string | ||
- an optional search string | ||
|
||
It also has output data, which is `hasMatch` - whether or not the search string has matched any input data. | ||
|
||
`hasMatch` is a better fit for output data than input data, because it's state that is _derived_ from input data. | ||
|
||
For example, if it was input data, you could create a TodoEmbeddable with input like this: | ||
|
||
```ts | ||
todoEmbeddableFactory.create({ task: 'take out the garabage', search: 'garbage', hasMatch: false }); | ||
``` | ||
|
||
That's wrong because there is actually a match from the search string inside the task. | ||
|
||
The TodoEmbeddable component itself doesn't do anything with the `hasMatch` variable other than set it, but | ||
if you check out `SearchableListContainer`, you can see an example where this output data is being used. | ||
|
||
## TodoRefEmbeddable | ||
|
||
This is an example of an embeddable based off of a saved object. The input is just the `savedObjectId` and | ||
the `search` string. It has even more output parameters, and this time, it does read it's own output parameters in | ||
order to calculate `hasMatch`. | ||
|
||
Output: | ||
```ts | ||
{ | ||
hasMatch: boolean, | ||
savedAttributes?: TodoSavedAttributes | ||
} | ||
``` | ||
|
||
`savedAttributes` is optional because it's possible a TodoSavedObject could not be found with the given savedObjectId. |
84 changes: 84 additions & 0 deletions
84
examples/embeddable_examples/public/todo/todo_ref_component.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,84 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React from 'react'; | ||
import { EuiFlexItem, EuiFlexGroup } from '@elastic/eui'; | ||
|
||
import { EuiText } from '@elastic/eui'; | ||
import { EuiAvatar } from '@elastic/eui'; | ||
import { EuiIcon } from '@elastic/eui'; | ||
import { EuiFlexGrid } from '@elastic/eui'; | ||
import { withEmbeddableSubscription } from '../../../../src/plugins/embeddable/public'; | ||
import { TodoRefInput, TodoRefOutput, TodoRefEmbeddable } from './todo_ref_embeddable'; | ||
|
||
interface Props { | ||
embeddable: TodoRefEmbeddable; | ||
input: TodoRefInput; | ||
output: TodoRefOutput; | ||
} | ||
|
||
function wrapSearchTerms(task?: string, search?: string) { | ||
if (!search) return task; | ||
if (!task) return task; | ||
const parts = task.split(new RegExp(`(${search})`, 'g')); | ||
return parts.map((part, i) => | ||
part === search ? ( | ||
<span key={i} style={{ backgroundColor: 'yellow' }}> | ||
{part} | ||
</span> | ||
) : ( | ||
part | ||
) | ||
); | ||
} | ||
|
||
export function TodoRefEmbeddableComponentInner({ | ||
input: { search }, | ||
output: { savedAttributes }, | ||
}: Props) { | ||
const icon = savedAttributes?.icon; | ||
const title = savedAttributes?.title; | ||
const task = savedAttributes?.task; | ||
return ( | ||
<EuiFlexGroup> | ||
<EuiFlexItem grow={false}> | ||
{icon ? ( | ||
<EuiIcon type={icon} size="l" /> | ||
) : ( | ||
<EuiAvatar name={title || task || ''} size="l" /> | ||
)} | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiFlexGrid columns={1}> | ||
<EuiFlexItem> | ||
<EuiText data-test-subj="todoEmbeddableTitle"> | ||
<h3>{wrapSearchTerms(title || '', search)}</h3> | ||
</EuiText> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiText data-test-subj="todoEmbeddableTask">{wrapSearchTerms(task, search)}</EuiText> | ||
</EuiFlexItem> | ||
</EuiFlexGrid> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
); | ||
} | ||
|
||
export const TodoRefEmbeddableComponent = withEmbeddableSubscription( | ||
TodoRefEmbeddableComponentInner | ||
); |
Oops, something went wrong.