-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
211 additions
and
168 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
39 changes: 39 additions & 0 deletions
39
vuu-ui/packages/vuu-layout/src/grid-layout/react-element-utils.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,39 @@ | ||
import React, { ReactElement } from "react"; | ||
|
||
export const getChildComponent = ( | ||
children: ReactElement[], | ||
gridItemId: string | ||
): ReactElement => { | ||
const targetGridItem = children.find( | ||
(child) => child.props.id === gridItemId | ||
); | ||
if (targetGridItem) { | ||
const childComponent = targetGridItem.props.children; | ||
if (React.isValidElement(childComponent)) { | ||
return childComponent; | ||
} else if (Array.isArray(childComponent)) { | ||
return childComponent.at(0) as ReactElement; | ||
} else { | ||
throw Error(`invalid child component`); | ||
} | ||
} else { | ||
throw Error(`getChildComponent #${gridItemId} not found`); | ||
} | ||
}; | ||
|
||
export const addChildComponentToStack = ( | ||
stackElement: ReactElement, | ||
childElement: ReactElement | ||
) => { | ||
if (Array.isArray(stackElement.props.children)) { | ||
console.log(`children is an array`); | ||
} | ||
|
||
const stackChildren = stackElement.props.children; | ||
// can we add an imperative API method to Stack ? | ||
return React.cloneElement( | ||
stackElement, | ||
{}, | ||
stackChildren.concat(childElement) | ||
); | ||
}; |
Oops, something went wrong.