-
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
9 changed files
with
173 additions
and
169 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
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 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { K8sResourceCommon } from '@console/internal/module/k8s'; | ||
import { IRowData, OnSelect } from '@patternfly/react-table'; | ||
|
||
const getAllSelectedRows =<R extends K8sResourceCommon>(data: R[], selectedRows: string[]) => data.filter((row) => selectedRows.includes(row.metadata.uid)); | ||
|
||
export const useSelectList = <R extends K8sResourceCommon>( | ||
data: R[], | ||
visibleRows: string[], | ||
onRowSelected: (rows: R[]) => void, | ||
): { | ||
onSelect: OnSelect, | ||
selectedRows: string[]; | ||
setSelectedRows: React.Dispatch<React.SetStateAction<string[]>>; | ||
} => { | ||
const [selectedRows, setSelectedRows] = React.useState<string[]>([]); | ||
|
||
const onSelect = ( | ||
_event: React.MouseEvent, | ||
isSelected: boolean, | ||
rowIndex: number, | ||
rowData: IRowData, | ||
) => { | ||
let uniqueUIDs = []; | ||
if (rowIndex === -1) { | ||
if (isSelected) { | ||
uniqueUIDs = _.uniq([...visibleRows, ...selectedRows]); | ||
} else { | ||
uniqueUIDs = _.uniq(selectedRows.filter((uid) => !visibleRows.includes(uid))); | ||
} | ||
} else { | ||
uniqueUIDs = _.xor(selectedRows, [rowData?.props?.id]); | ||
} | ||
setSelectedRows(uniqueUIDs); | ||
onRowSelected(getAllSelectedRows(data, uniqueUIDs)); | ||
}; | ||
return { | ||
onSelect, | ||
selectedRows, | ||
setSelectedRows, | ||
}; | ||
}; |
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
Oops, something went wrong.