Skip to content
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

Fix component flicker #1061

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
BindableAttrValue,
UseDataQueryConfig,
NestedBindableAttrs,
BindableAttrValues,
} from '@mui/toolpad-core';
import { QueryClient, QueryClientProvider, useMutation } from '@tanstack/react-query';
import {
Expand Down Expand Up @@ -179,11 +180,22 @@ function RenderedNodeContent({ node, childNodeGroups, Component }: RenderedNodeC

const nodeId = node.id;

const isPageNode = appDom.isPage(node);
const isElementNode = appDom.isElement(node);

const nodeProps: BindableAttrValues<Record<string, any>> = React.useMemo(
() => (isElementNode ? node.props || {} : {}),
[isElementNode, node],
);
const nodeLayoutProps = React.useMemo(
() => (isElementNode ? node.layout || {} : {}),
[isElementNode, node],
);

const componentConfig = Component[TOOLPAD_COMPONENT];
const { argTypes, errorProp, loadingProp, loadingPropSource } = componentConfig;

const isLayoutNode =
appDom.isPage(node) || (appDom.isElement(node) && isPageLayoutComponent(node));
const isLayoutNode = isPageNode || (isElementNode && isPageLayoutComponent(node));

const liveBindings = useBindingsContext();
const boundProps: Record<string, any> = React.useMemo(() => {
Expand All @@ -207,8 +219,12 @@ function RenderedNodeContent({ node, childNodeGroups, Component }: RenderedNodeC
}
}

if (typeof hookResult[propName] === 'undefined' && argType) {
hookResult[propName] = argType.defaultValue;
if (typeof hookResult[propName] === 'undefined') {
if (nodeProps && nodeProps[propName]?.type === 'const') {
hookResult[propName] = nodeProps[propName]?.value;
} else if (argType) {
hookResult[propName] = argType.defaultValue;
}
}
}

Expand All @@ -225,25 +241,32 @@ function RenderedNodeContent({ node, childNodeGroups, Component }: RenderedNodeC
}

return hookResult;
}, [argTypes, errorProp, liveBindings, loadingProp, loadingPropSource, nodeId]);
}, [argTypes, errorProp, liveBindings, loadingProp, loadingPropSource, nodeId, nodeProps]);

const boundLayoutProps: Record<string, any> = React.useMemo(() => {
const hookResult: Record<string, any> = {};

for (const [propName, argType] of isLayoutNode ? [] : Object.entries(layoutBoxArgTypes)) {
for (const layoutBoxArgTypesEntry of isLayoutNode ? [] : Object.entries(layoutBoxArgTypes)) {
const propName = layoutBoxArgTypesEntry[0] as keyof typeof layoutBoxArgTypes;
const argType = layoutBoxArgTypesEntry[1];

const bindingId = `${nodeId}.layout.${propName}`;
const binding = liveBindings[bindingId];
if (binding) {
hookResult[propName] = binding.value;
}

if (typeof hookResult[propName] === 'undefined' && argType) {
hookResult[propName] = argType.defaultValue;
if (typeof hookResult[propName] === 'undefined') {
if (nodeLayoutProps && nodeLayoutProps[propName]?.type === 'const') {
hookResult[propName] = nodeLayoutProps[propName]?.value;
} else if (argType) {
hookResult[propName] = argType.defaultValue;
}
}
}

return hookResult;
}, [isLayoutNode, liveBindings, nodeId]);
}, [isLayoutNode, liveBindings, nodeId, nodeLayoutProps]);

const onChangeHandlers: Record<string, (param: any) => void> = React.useMemo(
() =>
Expand Down