-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
each element is rendered as a separate jsx
- Loading branch information
1 parent
3f6da5c
commit f627f24
Showing
11 changed files
with
362 additions
and
332 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
75 changes: 75 additions & 0 deletions
75
frontend/taipy-gui/base/src/components/Taipy/TaipyElement.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,75 @@ | ||
import React, { ComponentType, useContext, useEffect, useMemo, useState } from "react"; | ||
import { createPortal } from "react-dom"; | ||
import { ErrorBoundary } from "react-error-boundary"; | ||
import JsxParser from "react-jsx-parser"; | ||
|
||
import { PageContext, TaipyContext } from "../../../../src/context/taipyContext"; | ||
import { Element } from "../../renderer/elementManager"; | ||
import useStore from "../../store"; | ||
import { getJsx } from "./utils"; | ||
import { emptyArray } from "../../../../src/utils"; | ||
import ErrorFallback from "../../../../src/utils/ErrorBoundary"; | ||
import { getRegisteredComponents } from "../../../../src/components/Taipy"; | ||
import { renderError, unregisteredRender } from "../../../../src/components/Taipy/Unregistered"; | ||
|
||
interface TaipyElementProps { | ||
editMode: boolean; | ||
element: Element; | ||
} | ||
|
||
const TaipyElement = (props: TaipyElementProps) => { | ||
const { state } = useContext(TaipyContext); | ||
const [module, setModule] = useState<string>(""); | ||
const [jsx, setJsx] = useState<string>(""); | ||
const app = useStore((state) => state.app); | ||
|
||
const renderConfig = useMemo( | ||
() => (props.editMode ? props.element.editModeRenderConfig : props.element.renderConfig), | ||
[props.element, props.editMode], | ||
); | ||
|
||
const pageState = useMemo(() => { | ||
return { jsx, module }; | ||
}, [jsx, module]); | ||
|
||
useEffect(() => { | ||
app && setModule(app.getContext()); | ||
}, [app]); | ||
|
||
useEffect(() => { | ||
const setJsxAsync = async () => { | ||
if (!app || !renderConfig) { | ||
setJsx(""); | ||
return; | ||
} | ||
const res = await getJsx(app, props.element, props.editMode); | ||
setJsx(`${renderConfig.wrapper[0]}${res}${renderConfig.wrapper[1]}`); | ||
}; | ||
setJsxAsync(); | ||
}, [app, props.editMode, props.element, renderConfig]); | ||
|
||
return renderConfig ? ( | ||
createPortal( | ||
<PageContext.Provider value={pageState}> | ||
<ErrorBoundary FallbackComponent={ErrorFallback}> | ||
<JsxParser | ||
disableKeyGeneration={true} | ||
bindings={state.data} | ||
components={getRegisteredComponents() as Record<string, ComponentType>} | ||
jsx={pageState.jsx} | ||
renderUnrecognized={unregisteredRender} | ||
allowUnknownElements={false} | ||
renderError={renderError} | ||
blacklistedAttrs={emptyArray} | ||
renderInWrapper={false} | ||
/> | ||
</ErrorBoundary> | ||
</PageContext.Provider>, | ||
renderConfig.root, | ||
) | ||
) : ( | ||
<></> | ||
); | ||
}; | ||
|
||
export default TaipyElement; |
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.