-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from rszwajko/selectVm
Extract selection logic to withIdBasedSelection() HoC
- Loading branch information
Showing
2 changed files
with
149 additions
and
82 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
packages/forklift-console-plugin/src/components/page/withSelection.tsx
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,108 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { | ||
DefaultHeader, | ||
GlobalActionToolbarProps, | ||
RowProps, | ||
TableViewHeaderProps, | ||
} from '@kubev2v/common'; | ||
import { Th } from '@patternfly/react-table'; | ||
|
||
import StandardPage, { StandardPageProps } from './StandardPage'; | ||
|
||
export function withRowSelection<T>({ RowMapper, isSelected, toggleSelectFor }) { | ||
const Enhanced = (props: RowProps<T>) => ( | ||
<RowMapper | ||
{...props} | ||
isSelected={isSelected(props.resourceData)} | ||
// the check box will be always visible | ||
// with current interface disabling/hiding needs to be implemented at the row mapper level | ||
toggleSelect={() => toggleSelectFor([props.resourceData])} | ||
/> | ||
); | ||
Enhanced.displayName = `${RowMapper.displayName || 'RowMapper'}WithSelection`; | ||
return Enhanced; | ||
} | ||
|
||
export function withHeaderSelection<T>({ HeaderMapper, isSelected, canSelect, toggleSelectFor }) { | ||
const Enhanced = ({ dataOnScreen, ...other }: TableViewHeaderProps<T>) => { | ||
const selectableItems = dataOnScreen.filter(canSelect); | ||
const allSelected = selectableItems.every((it) => isSelected(it)); | ||
return ( | ||
<> | ||
<Th | ||
select={{ | ||
onSelect: () => toggleSelectFor(selectableItems), | ||
isSelected: allSelected, | ||
isHeaderSelectDisabled: !selectableItems?.length, // Disable if no selectable items | ||
}} | ||
/> | ||
<HeaderMapper {...{ ...other, dataOnScreen }} /> | ||
</> | ||
); | ||
}; | ||
Enhanced.displayName = `${HeaderMapper.displayName || 'HeaderMapper'}WithSelection`; | ||
return Enhanced; | ||
} | ||
|
||
export interface IdBasedSelectionProps<T> { | ||
/** | ||
* @returns string that can be used as an unique identifier | ||
*/ | ||
toId: (item: T) => string; | ||
|
||
/** | ||
* @returns true if items can be selected, false otherwise | ||
*/ | ||
canSelect: (item: T) => boolean; | ||
|
||
/** | ||
* global toolbar actions | ||
*/ | ||
actions: React.FC<GlobalActionToolbarProps<T> & { selectedIds: string[] }>[]; | ||
} | ||
|
||
/** | ||
* Adds ID based multi selection to StandardPage component. | ||
* Contract: | ||
* 1. provided row mapper renders check boxes when needed | ||
* 2. IDs provided with toId() function are unique and constant in time | ||
* 3. check box status at row level does not depend from other rows and can be calculated from the item via canSelect() function | ||
*/ | ||
export function withIdBasedSelection<T>({ toId, canSelect, actions }: IdBasedSelectionProps<T>) { | ||
const Enhanced = (props: StandardPageProps<T>) => { | ||
const [selectedIds, setSelectedIds]: [string[], (selected: string[]) => void] = useState([]); | ||
const isSelected = (item: T) => selectedIds.includes(toId(item)); | ||
const toggleSelectFor = (items: T[]) => { | ||
const ids = items.map(toId); | ||
const allSelected = ids.every((id) => selectedIds.includes(id)); | ||
setSelectedIds([ | ||
...selectedIds.filter((it) => !ids.includes(it)), | ||
...(allSelected ? [] : ids), | ||
]); | ||
}; | ||
return ( | ||
<StandardPage | ||
{...props} | ||
RowMapper={withRowSelection({ | ||
RowMapper: props.RowMapper, | ||
isSelected, | ||
toggleSelectFor, | ||
})} | ||
HeaderMapper={withHeaderSelection({ | ||
HeaderMapper: props.HeaderMapper ?? DefaultHeader, | ||
canSelect, | ||
isSelected, | ||
toggleSelectFor, | ||
})} | ||
GlobalActionToolbarItems={actions.map((Action) => { | ||
const ActionWithSelection = (props) => <Action {...{ ...props, selectedIds }} />; | ||
ActionWithSelection.displayName = `${Action.displayName || 'Action'}WithSelection`; | ||
return ActionWithSelection; | ||
})} | ||
/> | ||
); | ||
}; | ||
Enhanced.displayName = 'StandardPageWithSelection'; | ||
return Enhanced; | ||
} |
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