New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding custom hook for selection on list component #5757
Adding custom hook for selection on list component #5757
Conversation
frontend/packages/ceph-storage-plugin/src/components/ocs-install/node-list.tsx
Outdated
Show resolved
Hide resolved
2ba07dc
to
330b61d
Compare
frontend/packages/ceph-storage-plugin/src/components/ocs-install/node-list.tsx
Outdated
Show resolved
Hide resolved
330b61d
to
f5043b5
Compare
...kages/local-storage-operator-plugin/src/components/local-volume-set/nodes-selection-list.tsx
Outdated
Show resolved
Hide resolved
...es/local-storage-operator-plugin/src/components/local-volume-set/create-local-volume-set.tsx
Outdated
Show resolved
Hide resolved
...kages/local-storage-operator-plugin/src/components/local-volume-set/nodes-selection-list.tsx
Outdated
Show resolved
Hide resolved
...kages/local-storage-operator-plugin/src/components/local-volume-set/nodes-selection-list.tsx
Outdated
Show resolved
Hide resolved
...kages/local-storage-operator-plugin/src/components/local-volume-set/nodes-selection-list.tsx
Outdated
Show resolved
Hide resolved
We can use the reducers as well if passing data is getting too complicated https://github.com/openshift/console/blob/master/frontend/packages/console-plugin-sdk/src/typings/reducers.ts. |
cc @jcaianirh who was going to look bulk actions |
/assign @rawagner |
32500da
to
074ff13
Compare
/retest |
1 similar comment
/retest |
074ff13
to
a058348
Compare
onRowSelected(getAllSelectedRows(data, uniqueUIDs)); | ||
}; | ||
|
||
const updateSelectedRows = (rows: R[]) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd wrap this into React.useCallback
uniqueUIDs = _.xor(selectedRows, [rowData?.props?.id]); | ||
} | ||
setSelectedRows(uniqueUIDs); | ||
onRowSelected(getAllSelectedRows(data, uniqueUIDs)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont see a lot of value in having getAllSelectedRows
as separate function
Just inlining works fine (and is more transparent IMO)
onRowSelected(getAllSelectedRows(data, uniqueUIDs)); | |
onRowSelected(data.filter((datum) => selectedRows.includes(datum.metadata.uid))); |
|
||
const onSelect = (_event, isSelected, rowIndex, rowData) => { | ||
let uniqueUIDs = []; | ||
if (rowIndex === -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when can rowIndex be -1 ?
I dont understand why this is not just
const selectedUIDs = isSelected ? selectedRows.push(rowData.props.id) : selectedRows.filter((uid) => rowData.props.id != uid);
setSelectedRows(selectedUIDs);
why do we need to do all the uniq and xors ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you click select all on top of the table you will get a rowIndex of -1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, when you select checkbox that corresponds to select all nodes, the rowIndex comes as -1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uniq is done to uniquely get the uid of the nodes as its possible that the visibleRows
and selectedRows
can have common uid in it.
} => { | ||
const [selectedRows, setSelectedRows] = React.useState<string[]>([]); | ||
|
||
const onSelect = (_event, isSelected, rowIndex, rowData) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably be wrapped in React.useCallback
too
@@ -153,7 +153,11 @@ export const CreateOCSServiceForm = withHandlePromise< | |||
kind={NodeModel.kind} | |||
showTitle={false} | |||
ListComponent={NodeTable} | |||
customData={{ selectedNodes, setSelectedNodes, visibleRows, setVisibleRows }} | |||
customData={{ | |||
onRowSelected: (selectedNodes: NodeKind[]) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onRowSelected: (selectedNodes: NodeKind[]) => { | |
onRowSelected: setNodes |
a058348
to
51f9bc3
Compare
@rawagner Addressed your comments. Please review. |
if (isSelected) { | ||
uniqueUIDs = _.uniq([...visibleRows, ...selectedRows]); | ||
} else { | ||
uniqueUIDs = _.uniq(selectedRows.filter((uid) => !visibleRows.includes(uid))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you dont have to do uniq
here
15c3932
to
9ba2b39
Compare
...kages/local-storage-operator-plugin/src/components/local-volume-set/nodes-selection-list.tsx
Outdated
Show resolved
Hide resolved
66ef398
to
24af09b
Compare
24af09b
to
f83ef18
Compare
@rawagner Updated. Please review. |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: gnehapk, rawagner The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@@ -50,25 +52,28 @@ const getColumns = () => { | |||
]; | |||
}; | |||
|
|||
const getSelected = (selected: NodeKind[], nodeUID: string) => | |||
selected.map((node) => node.metadata.uid).includes(nodeUID); | |||
const isSelected = (selected: Set<string>, nodeUID: string): boolean => selected.has(nodeUID); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just inline use it. I don't think we need a helper function for this.
{ | ||
componentProps, | ||
}: { | ||
componentProps: { data: NodeKind[] }; | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you destructuring componentProps
?
{ | |
componentProps, | |
}: { | |
componentProps: { data: NodeKind[] }; | |
}, | |
componentProps: { data: NodeKind[] }; |
Can't it be ^^?
selected: _.isArray(selectedNodes) | ||
? getSelected(selectedNodes, node.metadata.uid) | ||
selected: selectedNodes | ||
? isSelected(selectedNodes, node.metadata.uid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? isSelected(selectedNodes, node.metadata.uid) | |
? (selectedNodes.has(node.metadata.uid)) |
|
||
const onSelect = React.useCallback( | ||
(_event, isSelected, rowIndex, rowData) => { | ||
const uniqueUIDs: Set<string> = selectedRows ? new Set([...selectedRows]) : new Set<string>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't selectedRows being initialized as a Set?
if (_.isEmpty(rows)) return true; | ||
return Object.keys(rowUIDMap).some((uid) => rows?.[uid]?.selected !== rowUIDMap?.[uid]?.selected); | ||
}; | ||
const isSelected = (selected: Set<string>, nodeUID: string): boolean => selected.has(nodeUID); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use has
Creating a custom hook for abstracting the selection logic for a list component