Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parsing support for nested components (#36)
This PR adds the ability to parse nested components that use props.children, and represent them in the UI as a tree. Saving to file does not work. Nested components do not show up in the preview. Updated uuid usages from v1 to v4, since v1 uuids looked too similar and were annoying to test with. TEST=manual check in react dev tools that the parentUUIDsFromRoot is correct
- Loading branch information
Showing
35 changed files
with
398 additions
and
246 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ReactNode } from 'react'; | ||
import { HexColor } from '../../studio/types'; | ||
|
||
export interface CardProps { | ||
bgColor?: HexColor | ||
text?: string | ||
children?: ReactNode | ||
} | ||
|
||
export default function Card(props: CardProps) { | ||
return ( | ||
<div style={{backgroundColor: props.bgColor}}> | ||
hi this is a card | ||
{props.text} | ||
{props.children} | ||
</div> | ||
) | ||
} |
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
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
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,22 @@ | ||
import { ComponentState } from '../../shared/models' | ||
|
||
export default function findComponentState( | ||
componentStateToFind: Pick<ComponentState, 'uuid' | 'parentUUIDsFromRoot' | 'props'>, | ||
componentsState: ComponentState[] | ||
): ComponentState | undefined { | ||
let parentComponentState: ComponentState | undefined | ||
for (const uuid of componentStateToFind.parentUUIDsFromRoot ?? []) { | ||
parentComponentState = getComponentState(uuid) | ||
if (!parentComponentState) { | ||
console.error( | ||
'Unable to find parent uuid', uuid, 'for parentUUIDsFromRoot', componentStateToFind.parentUUIDsFromRoot) | ||
return undefined | ||
} | ||
} | ||
return getComponentState(componentStateToFind.uuid) | ||
|
||
function getComponentState(uuid: string) { | ||
return parentComponentState?.children?.find(c => c.uuid === uuid) ?? | ||
componentsState.find(c => c.uuid === uuid) | ||
} | ||
} |
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,17 @@ | ||
import { ComponentState } from '../../shared/models' | ||
|
||
type Handler = (c: ComponentState) => void | ||
|
||
export default function iterateOverComponentsState( | ||
componentsState: ComponentState[], | ||
handler: Handler | ||
): void { | ||
componentsState.forEach(c => handleComponentState(c, handler)) | ||
} | ||
|
||
function handleComponentState(componentState: ComponentState, handler: Handler) { | ||
handler(componentState) | ||
if (componentState.children) { | ||
iterateOverComponentsState(componentState.children, handler) | ||
} | ||
} |
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,12 @@ | ||
import Card from '../components/Card' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<Card bgColor='#453d0d'> | ||
<Card/> | ||
</Card> | ||
</> | ||
) | ||
} | ||
|
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.