Skip to content

Commit

Permalink
update props
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Jan 27, 2022
1 parent 4727e48 commit f2b0e05
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
53 changes: 51 additions & 2 deletions x-pack/examples/embedded_lens_example/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
} from '../../../plugins/lens/public';

import { ViewMode } from '../../../../src/plugins/embeddable/public';
import { ActionExecutionContext } from '../../../../src/plugins/ui_actions/public';

// Generate a Lens state based on some app-specific input parameters.
// `TypedLensByValueInput` can be used for type-safety - it uses the same interfaces as Lens-internal code.
Expand Down Expand Up @@ -126,6 +127,9 @@ export const App = (props: {
to: 'now',
});

const [enableExtraAction, setEnableExtraAction] = useState(false);
const [enableDefaultAction, setEnableDefaultAction] = useState(false);

const LensComponent = props.plugins.lens.EmbeddableComponent;
const LensSaveModalComponent = props.plugins.lens.SaveModalComponent;

Expand Down Expand Up @@ -153,7 +157,7 @@ export const App = (props: {
configuration and navigate to a prefilled editor.
</p>

<EuiFlexGroup>
<EuiFlexGroup wrap>
<EuiFlexItem grow={false}>
<EuiButton
data-test-subj="lns-example-change-color"
Expand Down Expand Up @@ -238,10 +242,34 @@ export const App = (props: {
Change time range
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
aria-label="Enable extra action"
data-test-subj="lns-example-extra-action"
isDisabled={!attributes}
onClick={() => {
setEnableExtraAction((prevState) => !prevState);
}}
>
{enableExtraAction ? 'Disable extra action' : 'Enable extra action'}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
aria-label="Enable default actions"
data-test-subj="lns-example-default-action"
isDisabled={!attributes}
onClick={() => {
setEnableDefaultAction((prevState) => !prevState);
}}
>
{enableDefaultAction ? 'Disable default action' : 'Enable default action'}
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
<LensComponent
id=""
withActions
withDefaultActions={enableDefaultAction}
style={{ height: 500 }}
timeRange={time}
attributes={attributes}
Expand All @@ -261,6 +289,27 @@ export const App = (props: {
// call back event for on table row click event
}}
viewMode={ViewMode.VIEW}
extraActions={
enableExtraAction
? [
{
id: 'testAction',
type: 'link',
getIconType: () => 'save',
async isCompatible(
context: ActionExecutionContext<object>
): Promise<boolean> {
return true;
},
execute: async (context: ActionExecutionContext<object>) => {
alert('I am an extra action');
return;
},
getDisplayName: () => 'Extra action',
},
]
: undefined
}
/>
{isSaveModalVisible && (
<LensSaveModalComponent
Expand Down
21 changes: 15 additions & 6 deletions x-pack/plugins/lens/public/embeddable/embeddable_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export type TypedLensByValueInput = Omit<LensByValueInput, 'attributes'> & {
};

export type EmbeddableComponentProps = (TypedLensByValueInput | LensByReferenceInput) & {
withActions?: boolean | Action[];
withDefaultActions?: boolean;
extraActions?: Action[];
};

interface PluginsStartDependencies {
Expand All @@ -67,7 +68,9 @@ export function getEmbeddableComponent(core: CoreStart, plugins: PluginsStartDep
const factory = embeddableStart.getEmbeddableFactory('lens')!;
const input = { ...props };
const [embeddable, loading, error] = useEmbeddableFactory({ factory, input });
const hasActions = Boolean(props.withActions);
const hasActions =
Boolean(props.withDefaultActions) || (props.extraActions && props.extraActions?.length > 0);

const theme = core.theme;

if (loading) {
Expand All @@ -83,7 +86,8 @@ export function getEmbeddableComponent(core: CoreStart, plugins: PluginsStartDep
actionPredicate={() => hasActions}
input={input}
theme={theme}
extraActions={Array.isArray(props.withActions) ? props.withActions : []}
extraActions={props.extraActions}
withDefaultActions={props.withDefaultActions}
/>
);
}
Expand All @@ -99,7 +103,8 @@ interface EmbeddablePanelWrapperProps {
actionPredicate: (id: string) => boolean;
input: EmbeddableComponentProps;
theme: ThemeServiceStart;
extraActions: Action[];
extraActions?: Action[];
withDefaultActions?: boolean;
}

const EmbeddablePanelWrapper: FC<EmbeddablePanelWrapperProps> = ({
Expand All @@ -110,6 +115,7 @@ const EmbeddablePanelWrapper: FC<EmbeddablePanelWrapperProps> = ({
input,
theme,
extraActions,
withDefaultActions,
}) => {
useEffect(() => {
embeddable.updateInput(input);
Expand All @@ -120,8 +126,11 @@ const EmbeddablePanelWrapper: FC<EmbeddablePanelWrapperProps> = ({
hideHeader={false}
embeddable={embeddable as IEmbeddable<EmbeddableInput, EmbeddableOutput>}
getActions={async (triggerId, context) => {
const actions = await uiActions.getTriggerCompatibleActions(triggerId, context);
return [...extraActions, ...actions];
const actions = withDefaultActions
? await uiActions.getTriggerCompatibleActions(triggerId, context)
: [];

return [...(extraActions ?? []), ...actions];
}}
inspector={inspector}
actionPredicate={actionPredicate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ export class LensAttributes {
};
}

getJSON(refresh?: number): TypedLensByValueInput['attributes'] {
getJSON(): TypedLensByValueInput['attributes'] {
const uniqueIndexPatternsIds = Array.from(
new Set([...this.layerConfigs.map(({ indexPattern }) => indexPattern.id)])
);
Expand All @@ -792,7 +792,7 @@ export class LensAttributes {

return {
title: 'Prefilled from exploratory view app',
description: String(refresh),
description: '',
visualizationType: 'lnsXY',
references: [
...uniqueIndexPatternsIds.map((patternId) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export default function Embeddable({
timeRange={series?.time}
attributes={attributesJSON}
onBrushEnd={({ range }) => {}}
withActions={withActions ? actions : false}
withDefaultActions={Boolean(withActions)}
extraActions={actions}
/>
{isSaveOpen && attributesJSON && (
<LensSaveModalComponent
Expand Down

0 comments on commit f2b0e05

Please sign in to comment.