Skip to content

Commit f20a218

Browse files
cee-chenCoenWarmer
authored andcommitted
[Fleet][EuiInMemoryTable] Replace usage of deprecated ref method with controlled selection.selected API (elastic#175727)
## Summary **Please help us QA your affected tables to confirm that your plugin's table selection still works as before!** EUI will shortly be removing this deprecated ref `setSelection` method in favor of the new controlled `selection.selected` prop. This PR converts Fleet's more advanced usage of controlled selection, which should not suffer any UI/UX regressions. See also: - elastic/eui#7321 - elastic#175722 (examples of basic conversions)
1 parent dfe8c31 commit f20a218

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/components/agent_list_table.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interface Props {
5252
sortField: keyof Agent;
5353
sortOrder: 'asc' | 'desc';
5454
onSelectionChange: (agents: Agent[]) => void;
55-
tableRef?: React.Ref<any>;
55+
selected: Agent[];
5656
showUpgradeable: boolean;
5757
totalAgents?: number;
5858
pagination: Pagination;
@@ -77,9 +77,9 @@ export const AgentListTable: React.FC<Props> = (props: Props) => {
7777
renderActions,
7878
sortField,
7979
sortOrder,
80-
tableRef,
8180
onTableChange,
8281
onSelectionChange,
82+
selected,
8383
totalAgents = 0,
8484
showUpgradeable,
8585
pagination,
@@ -318,7 +318,6 @@ export const AgentListTable: React.FC<Props> = (props: Props) => {
318318

319319
return (
320320
<EuiBasicTable<Agent>
321-
ref={tableRef}
322321
className="fleet__agentList__table"
323322
data-test-subj="fleetAgentListTable"
324323
loading={isLoading}
@@ -341,6 +340,7 @@ export const AgentListTable: React.FC<Props> = (props: Props) => {
341340
}}
342341
isSelectable={true}
343342
selection={{
343+
selected,
344344
onSelectionChange,
345345
selectable: isAgentSelectable,
346346
selectableMessage: (selectable, agent) => {

x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_list_page/index.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
import React, { useState, useMemo, useCallback, useRef } from 'react';
7+
import React, { useState, useMemo, useCallback } from 'react';
88
import { differenceBy, isEqual } from 'lodash';
9-
import type { EuiBasicTable } from '@elastic/eui';
109
import { EuiSpacer, EuiPortal } from '@elastic/eui';
1110

1211
import type { Agent } from '../../../types';
@@ -49,7 +48,6 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
4948
// Table and search states
5049
const [selectedAgents, setSelectedAgents] = useState<Agent[]>([]);
5150
const [selectionMode, setSelectionMode] = useState<SelectionMode>('manual');
52-
const tableRef = useRef<EuiBasicTable<Agent>>(null);
5351

5452
const {
5553
allTags,
@@ -208,21 +206,19 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
208206
);
209207

210208
const onSelectionChange = (newAgents: Agent[]) => {
211-
setSelectedAgents(newAgents);
212209
if (selectionMode === 'query' && newAgents.length < selectedAgents.length) {
213210
// differentiating between selection changed by agents dropping from current page or user action
214211
const areSelectedAgentsStillVisible =
215212
selectedAgents.length > 0 &&
216213
differenceBy(selectedAgents, agentsOnCurrentPage, 'id').length === 0;
217-
if (areSelectedAgentsStillVisible) {
218-
setSelectionMode('manual');
219-
} else {
214+
if (!areSelectedAgentsStillVisible) {
220215
// force selecting all agents on current page if staying in query mode
221-
if (tableRef?.current) {
222-
tableRef.current.setSelection(agentsOnCurrentPage);
223-
}
216+
return setSelectedAgents(agentsOnCurrentPage);
217+
} else {
218+
setSelectionMode('manual');
224219
}
225220
}
221+
setSelectedAgents(newAgents);
226222
};
227223

228224
const agentToUnenrollHasFleetServer = useMemo(() => {
@@ -429,10 +425,8 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
429425
setSelectionMode={setSelectionMode}
430426
selectedAgents={selectedAgents}
431427
setSelectedAgents={(newAgents: Agent[]) => {
432-
if (tableRef?.current) {
433-
tableRef.current.setSelection(newAgents);
434-
setSelectionMode('manual');
435-
}
428+
setSelectedAgents(newAgents);
429+
setSelectionMode('manual');
436430
}}
437431
clearFilters={clearFilters}
438432
isUsingFilter={isUsingFilter}
@@ -448,7 +442,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
448442
agentPoliciesIndexedById={agentPoliciesIndexedById}
449443
renderActions={renderActions}
450444
onSelectionChange={onSelectionChange}
451-
tableRef={tableRef}
445+
selected={selectedAgents}
452446
showUpgradeable={showUpgradeable}
453447
onTableChange={onTableChange}
454448
pagination={pagination}

0 commit comments

Comments
 (0)