Skip to content

Commit

Permalink
[8.x] [controls] add example for programmatically interacting with co…
Browse files Browse the repository at this point in the history
…ntrols (#212665) (#212828)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[controls] add example for programmatically interacting with controls
(#212665)](#212665)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Nathan
Reese","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-03-01T00:16:07Z","message":"[controls]
add example for programmatically interacting with controls
(#212665)\n\nUpdate search control example with buttons to
programmatically interact\nwith controls\n\n<img width=\"800\"
alt=\"Screenshot 2025-02-27 at 8 41
05 AM\"\nsrc=\"https://github.com/user-attachments/assets/e936cdeb-ce51-4fca-a8bc-ec5d983e3155\"\n/>\n\n---------\n\nCo-authored-by:
kibanamachine
<[email protected]>\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"8784e4d68d31a0ce4818f4f3fbe1ac8b273642c1","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Presentation","release_note:skip","backport:version","v9.1.0","v8.19.0"],"title":"[controls]
add example for programmatically interacting with
controls","number":212665,"url":"https://github.com/elastic/kibana/pull/212665","mergeCommit":{"message":"[controls]
add example for programmatically interacting with controls
(#212665)\n\nUpdate search control example with buttons to
programmatically interact\nwith controls\n\n<img width=\"800\"
alt=\"Screenshot 2025-02-27 at 8 41
05 AM\"\nsrc=\"https://github.com/user-attachments/assets/e936cdeb-ce51-4fca-a8bc-ec5d983e3155\"\n/>\n\n---------\n\nCo-authored-by:
kibanamachine
<[email protected]>\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"8784e4d68d31a0ce4818f4f3fbe1ac8b273642c1"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/212665","number":212665,"mergeCommit":{"message":"[controls]
add example for programmatically interacting with controls
(#212665)\n\nUpdate search control example with buttons to
programmatically interact\nwith controls\n\n<img width=\"800\"
alt=\"Screenshot 2025-02-27 at 8 41
05 AM\"\nsrc=\"https://github.com/user-attachments/assets/e936cdeb-ce51-4fca-a8bc-ec5d983e3155\"\n/>\n\n---------\n\nCo-authored-by:
kibanamachine
<[email protected]>\nCo-authored-by:
Elastic Machine
<[email protected]>","sha":"8784e4d68d31a0ce4818f4f3fbe1ac8b273642c1"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nathan Reese <[email protected]>
  • Loading branch information
kibanamachine and nreese authored Mar 1, 2025
1 parent f8db6c1 commit 28d9076
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ import { lastValueFrom } from 'rxjs';
import { v4 as uuidv4 } from 'uuid';

import {
EuiButton,
EuiCallOut,
EuiLoadingSpinner,
EuiPanel,
EuiSpacer,
EuiText,
EuiTitle,
} from '@elastic/eui';
import { ControlGroupRenderer, ControlGroupRendererApi } from '@kbn/controls-plugin/public';
import {
ControlGroupRenderer,
ControlGroupRendererApi,
DefaultControlApi,
OptionsListControlApi,
} from '@kbn/controls-plugin/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { Filter, Query, TimeRange } from '@kbn/es-query';
import type { NavigationPublicPluginStart } from '@kbn/navigation-plugin/public';

import { PLUGIN_ID } from '../../constants';

interface Props {
Expand All @@ -33,6 +38,8 @@ interface Props {
navigation: NavigationPublicPluginStart;
}

const DEST_COUNTRY_CONTROL_ID = 'DEST_COUNTRY_CONTROL_ID';

export const SearchExample = ({ data, dataView, navigation }: Props) => {
const [controlFilters, setControlFilters] = useState<Filter[]>([]);
const [controlGroupAPI, setControlGroupAPI] = useState<ControlGroupRendererApi | undefined>();
Expand Down Expand Up @@ -107,7 +114,8 @@ export const SearchExample = ({ data, dataView, navigation }: Props) => {
<EuiText>
<p>
Pass filters, query, and time range to narrow controls. Combine search bar filters with
controls filters to narrow results.
controls filters to narrow results. Programmatically interact with individual controls by
accessing their API from controlGroupApi.children$.
</p>
</EuiText>
<EuiSpacer size="m" />
Expand All @@ -130,16 +138,21 @@ export const SearchExample = ({ data, dataView, navigation }: Props) => {
query={query}
showSearchBar={true}
/>

<ControlGroupRenderer
filters={filters}
getCreationOptions={async (initialState, builder) => {
await builder.addDataControlFromField(initialState, {
dataViewId: dataView.id!,
title: 'Destintion country',
fieldName: 'geo.dest',
width: 'medium',
grow: false,
});
await builder.addDataControlFromField(
initialState,
{
dataViewId: dataView.id!,
title: 'Destintion country',
fieldName: 'geo.dest',
width: 'medium',
grow: false,
},
DEST_COUNTRY_CONTROL_ID
);
await builder.addDataControlFromField(initialState, {
dataViewId: dataView.id!,
fieldName: 'bytes',
Expand All @@ -155,9 +168,42 @@ export const SearchExample = ({ data, dataView, navigation }: Props) => {
onApiAvailable={setControlGroupAPI}
timeRange={timeRange}
/>
<EuiSpacer />
<EuiCallOut title="Search results">
{isSearching ? <EuiLoadingSpinner size="l" /> : <p>Hits: {hits}</p>}
</EuiCallOut>

<EuiSpacer />
<EuiButton
color="text"
isDisabled={!Boolean(controlGroupAPI)}
onClick={() => {
if (controlGroupAPI) {
Object.values(controlGroupAPI.children$.getValue()).forEach((controlApi) => {
(controlApi as DefaultControlApi)?.clearSelections();
});
}
}}
>
Clear all controls
</EuiButton>
<EuiSpacer />
<EuiButton
color="text"
isDisabled={!Boolean(controlGroupAPI)}
onClick={() => {
if (controlGroupAPI) {
const controlApi = controlGroupAPI.children$.getValue()[
DEST_COUNTRY_CONTROL_ID
] as Partial<OptionsListControlApi>;
if (controlApi && controlApi.setSelectedOptions) {
controlApi.setSelectedOptions(['CN']);
}
}
}}
>
Set destination country to CN
</EuiButton>
</EuiPanel>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export const getOptionsListControlFactory = (): DataControlFactory<
if (invalidSelections$.getValue().size) invalidSelections$.next(new Set([]));
},
hasSelections$: hasSelections$ as PublishingSubject<boolean | undefined>,
setSelectedOptions: (options: OptionsListSelection[] | undefined) => {
selections.setSelectedOptions(options);
},
},
{
...dataControl.comparators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import type {
} from '../../../../common/options_list';
import type { DataControlApi } from '../types';

export type OptionsListControlApi = DataControlApi;
export type OptionsListControlApi = DataControlApi & {
setSelectedOptions: (options: OptionsListSelection[] | undefined) => void;
};

export interface OptionsListComponentState
extends Omit<OptionsListControlState, keyof OptionsListDisplaySettings> {
Expand Down
6 changes: 5 additions & 1 deletion src/platform/plugins/shared/controls/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ export {
} from './actions/constants';

export type { ControlGroupApi, ControlStateTransform } from './control_group/types';

export type { DataControlApi, DataControlFactory } from './controls/data_controls/types';
export type { DefaultControlApi } from './controls/types';
export type { OptionsListControlApi } from './controls/data_controls/options_list_control/types';
export type { RangesliderControlApi } from './controls/data_controls/range_slider/types';
export type { ESQLControlApi } from './controls/esql_control/types';
export type { TimesliderControlApi } from './controls/timeslider_control/types';

export {
ControlGroupRenderer,
Expand Down

0 comments on commit 28d9076

Please sign in to comment.