-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding custom hook for selection on list component
- Loading branch information
Showing
10 changed files
with
264 additions
and
170 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
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
63 changes: 63 additions & 0 deletions
63
frontend/packages/console-shared/src/hooks/__mocks__/select-list-data.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,63 @@ | ||
export const data = [ | ||
{ | ||
metadata: { | ||
name: 'node-1.internal', | ||
uid: '1', | ||
labels: { | ||
'failure-domain.beta.kubernetes.io/zone': 'us-east-1a', | ||
'kubernetes.io/hostname': 'node-1', | ||
'node-role.kubernetes.io/worker': '', | ||
}, | ||
status: { | ||
capacity: { | ||
cpu: '16', | ||
}, | ||
allocatable: { | ||
memory: '1000Ki', | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
metadata: { | ||
name: 'node-2.internal', | ||
uid: '2', | ||
labels: { | ||
'failure-domain.beta.kubernetes.io/zone': 'us-east-1b', | ||
'kubernetes.io/hostname': 'node-2', | ||
'node-role.kubernetes.io/worker': '', | ||
}, | ||
status: { | ||
capacity: { | ||
cpu: '16', | ||
}, | ||
allocatable: { | ||
memory: '1000Ki', | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
metadata: { | ||
name: 'node-3.internal', | ||
uid: '3', | ||
labels: { | ||
'failure-domain.beta.kubernetes.io/zone': 'us-east-1c', | ||
'kubernetes.io/hostname': 'node-3', | ||
'node-role.kubernetes.io/worker': '', | ||
}, | ||
status: { | ||
capacity: { | ||
cpu: '16', | ||
}, | ||
allocatable: { | ||
memory: '1000Ki', | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
export const visibleRows = new Set(['1', '2', '3']); | ||
|
||
export const onRowSelected = jest.fn(); |
43 changes: 43 additions & 0 deletions
43
frontend/packages/console-shared/src/hooks/__tests__/select-list.spec.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,43 @@ | ||
import { act } from 'react-dom/test-utils'; | ||
import { testHook } from '../../test-utils/hooks-utils'; | ||
import { useSelectList } from '../select-list'; | ||
import { data, visibleRows, onRowSelected } from '../__mocks__/select-list-data'; | ||
|
||
describe('useSelectList', () => { | ||
let onSelect; | ||
let selectedRows; | ||
let updateSelectedRows; | ||
|
||
beforeEach(() => { | ||
testHook(() => { | ||
({ onSelect, selectedRows, updateSelectedRows } = useSelectList( | ||
data, | ||
visibleRows, | ||
onRowSelected, | ||
)); | ||
}); | ||
}); | ||
|
||
it('onSelect should update selectedRows properly', () => { | ||
act(() => { | ||
onSelect({}, true, -1); | ||
}); | ||
expect(selectedRows).toEqual(visibleRows); | ||
act(() => { | ||
onSelect({}, false, 1, { props: { id: '2' } }); | ||
}); | ||
expect(selectedRows).toEqual(new Set(['1', '3'])); | ||
act(() => { | ||
onSelect({}, false, -1); | ||
}); | ||
expect(selectedRows).toEqual(new Set()); | ||
}); | ||
|
||
it('updateSelectedRows should update selectedRows properly and call onRowSelected', () => { | ||
act(() => { | ||
updateSelectedRows(data); | ||
}); | ||
expect(selectedRows).toEqual(visibleRows); | ||
expect(onRowSelected).toHaveBeenCalled(); | ||
}); | ||
}); |
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,47 @@ | ||
import * as React from 'react'; | ||
import { OnSelect } from '@patternfly/react-table'; | ||
import { K8sResourceCommon } from '@console/internal/module/k8s'; | ||
|
||
export const useSelectList = <R extends K8sResourceCommon>( | ||
data: R[], | ||
visibleRows: Set<string>, | ||
onRowSelected: (rows: R[]) => void, | ||
): { | ||
onSelect: OnSelect; | ||
selectedRows: Set<string>; | ||
updateSelectedRows: (rows: R[]) => void; | ||
} => { | ||
const [selectedRows, setSelectedRows] = React.useState<Set<string>>(new Set()); | ||
|
||
const onSelect = React.useCallback( | ||
(_event, isSelected, rowIndex, rowData) => { | ||
const uniqueUIDs: Set<string> = selectedRows ? new Set([...selectedRows]) : new Set<string>(); | ||
|
||
if (rowIndex === -1) { | ||
isSelected | ||
? visibleRows.forEach((uid) => uniqueUIDs.add(uid)) | ||
: visibleRows.forEach((uid) => uniqueUIDs.delete(uid)); | ||
} else { | ||
isSelected ? uniqueUIDs.add(rowData?.props?.id) : uniqueUIDs.delete(rowData?.props?.id); | ||
} | ||
|
||
setSelectedRows(uniqueUIDs); | ||
onRowSelected(data.filter((row) => uniqueUIDs.has(row.metadata.uid))); | ||
}, | ||
[data, onRowSelected, selectedRows, visibleRows], | ||
); | ||
|
||
const updateSelectedRows = React.useCallback( | ||
(rows: R[]) => { | ||
onRowSelected(rows); | ||
setSelectedRows(new Set(rows.map((row) => row.metadata.uid))); | ||
}, | ||
[onRowSelected], | ||
); | ||
|
||
return { | ||
onSelect, | ||
selectedRows, | ||
updateSelectedRows, | ||
}; | ||
}; |
Oops, something went wrong.