-
Notifications
You must be signed in to change notification settings - Fork 22
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
MTV-1726: Edit VMs list in migration plan #1414
Open
jschuler
wants to merge
2
commits into
kubev2v:main
Choose a base branch
from
jschuler:mtv-1726_add-vms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
149 changes: 149 additions & 0 deletions
149
packages/forklift-console-plugin/src/components/page/StandardPageWithExpansion.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,149 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { RowProps, withTr } from '@kubev2v/common'; | ||
import { Td, Th } from '@patternfly/react-table'; | ||
|
||
import StandardPage, { StandardPageProps } from './StandardPage'; | ||
|
||
export function withRowExpansion<T>({ CellMapper, isExpanded, toggleExpandFor }) { | ||
const Enhanced = (props: RowProps<T>) => ( | ||
<> | ||
{isExpanded && ( | ||
<Td | ||
expand={{ | ||
rowIndex: props.resourceIndex, | ||
isExpanded: isExpanded(props.resourceData), | ||
onToggle: () => toggleExpandFor([props.resourceData]), | ||
}} | ||
/> | ||
)} | ||
<CellMapper {...props} /> | ||
</> | ||
); | ||
Enhanced.displayName = `${CellMapper.displayName || 'CellMapper'}WithExpansion`; | ||
return Enhanced; | ||
} | ||
|
||
export interface IdBasedExpansionProps<T> { | ||
/** | ||
* @returns string that can be used as an unique identifier | ||
*/ | ||
toId?: (item: T) => string; | ||
|
||
/** | ||
* onExpand is called when expand changes | ||
*/ | ||
onExpand?: (expandedIds: string[]) => void; | ||
|
||
/** | ||
* Expanded ids | ||
*/ | ||
expandedIds?: string[]; | ||
} | ||
|
||
/** | ||
* Adds ID based expansion to StandardPage component. | ||
* Contract: | ||
* 1. IDs provided with toId() function are unique and constant in time | ||
*/ | ||
export function withIdBasedExpansion<T>({ | ||
toId, | ||
onExpand, | ||
expandedIds: initialExpandedIds, | ||
}: IdBasedExpansionProps<T>) { | ||
const Enhanced = (props: StandardPageProps<T>) => { | ||
const [expandedIds, setExpandedIds] = useState(initialExpandedIds); | ||
|
||
const isExpanded = | ||
onExpand || expandedIds ? (item: T) => expandedIds.includes(toId(item)) : undefined; | ||
|
||
const toggleExpandFor = (items: T[]) => { | ||
const ids = items.map(toId); | ||
const allExpanded = ids.every((id) => expandedIds?.includes(id)); | ||
const newExpandedIds = [ | ||
...(expandedIds || []).filter((it) => !ids.includes(it)), | ||
...(allExpanded ? [] : ids), | ||
]; | ||
|
||
setExpandedIds(newExpandedIds); | ||
if (onExpand) { | ||
onExpand(newExpandedIds); | ||
} | ||
}; | ||
|
||
const { CellMapper, ExpandedComponent, ...rest } = props; | ||
|
||
const RowMapper = withTr( | ||
withRowExpansion({ | ||
CellMapper: CellMapper, | ||
isExpanded, | ||
toggleExpandFor, | ||
}), | ||
ExpandedComponent, | ||
); | ||
|
||
return ( | ||
<StandardPage | ||
{...rest} | ||
expandedIds={expandedIds} | ||
toId={toId} | ||
RowMapper={RowMapper} | ||
HeaderMapper={() => <Th />} | ||
GlobalActionToolbarItems={props.GlobalActionToolbarItems} | ||
/> | ||
); | ||
}; | ||
Enhanced.displayName = 'StandardPageWithExpansion'; | ||
return Enhanced; | ||
} | ||
|
||
/** | ||
* Properties for the `StandardPageWithExpansion` component. | ||
* These properties extend the base `StandardPageProps` and add additional ones related to expansion. | ||
* | ||
* @typedef {Object} StandardPageWithExpansionProps | ||
* @property {Function} toId - A function that returns a unique identifier for each item. | ||
* @property {Function} onExpand - A callback function that is triggered when row is expanded or un expanded. | ||
* @property {string[]} expandedIds - An array of identifiers for the currently expanded items. | ||
* @property {...StandardPageProps<T>} - Other props that are passed through to the `StandardPage` component. | ||
* | ||
* @template T - The type of the items being displayed in the table. | ||
*/ | ||
export interface StandardPageWithExpansionProps<T> extends StandardPageProps<T> { | ||
toId?: (item: T) => string; | ||
onExpand?: (expandedIds: string[]) => void; | ||
expandedIds?: string[]; | ||
} | ||
|
||
/** | ||
* Renders a standard page with expansion capabilities. | ||
* This component wraps the `StandardPage` component and adds support for row expansion. | ||
* It uses the provided `toId`, `onExpand`, and `expandedIds` props to manage the expansion state. | ||
* | ||
* @param {Object} props - The properties passed to the component. | ||
* @param {Function} props.toId - A function that returns a unique identifier for each item. | ||
* @param {...StandardPageProps<T>} props - Other props that are passed through to the `StandardPage` component. | ||
* | ||
* @template T - The type of the items being displayed in the table. | ||
* | ||
* @example | ||
* <StandardPageWithExpansion | ||
* toId={item => item.id} | ||
* // ...other props | ||
* /> | ||
*/ | ||
export function StandardPageWithExpansion<T>(props: StandardPageWithExpansionProps<T>) { | ||
const { toId, onExpand, expandedIds, ...rest } = props; | ||
|
||
if (onExpand && (!toId || !expandedIds)) { | ||
throw new Error('Missing required properties: toId, expandedIds'); | ||
} | ||
|
||
const EnhancedStandardPage = withIdBasedExpansion<T>({ | ||
toId, | ||
onExpand, | ||
expandedIds, | ||
}); | ||
|
||
return <EnhancedStandardPage {...rest} />; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/forklift-console-plugin/src/modules/Plans/utils/types/PlanEditAction.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 @@ | ||
export type PlanEditAction = 'PLAN' | 'VMS'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will allow us next (with small changes like this) to edit plans too |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is basically a copy of packages/forklift-console-plugin/src/components/page/StandardPageWithSelection.tsx but without selection, only expansion