Skip to content

Commit

Permalink
Fix default values with bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Aug 17, 2022
1 parent d054d39 commit e6e4147
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
20 changes: 14 additions & 6 deletions packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ import usePageTitle from '../utils/usePageTitle';
import ComponentsContext, { useComponents, useComponent } from './ComponentsContext';
import { AppModulesProvider, useAppModules } from './AppModulesProvider';
import Pre from '../components/Pre';
import {
layoutBoxAlignArgTypeDef,
layoutBoxJustifyArgTypeDef,
} from '../toolpadComponents/layoutBox';
import { layoutBoxArgTypes } from '../toolpadComponents/layoutBox';

const USE_DATA_QUERY_CONFIG_KEYS: readonly (keyof UseDataQueryConfig)[] = [
'enabled',
Expand Down Expand Up @@ -315,10 +312,10 @@ function RenderedNodeContent({ node, childNodeGroups, Component }: RenderedNodeC
display: 'flex',
alignItems:
(componentConfig.hasLayoutBoxAlign && node.layout?.boxAlign?.value) ||
layoutBoxAlignArgTypeDef.defaultValue,
layoutBoxArgTypes.boxAlign.defaultValue,
justifyContent:
(componentConfig.hasLayoutBoxJustify && node.layout?.boxJustify?.value) ||
layoutBoxJustifyArgTypeDef.defaultValue,
layoutBoxArgTypes.boxJustify.defaultValue,
}}
>
<Component {...props} />
Expand Down Expand Up @@ -466,6 +463,17 @@ function parseBindings(
}
}
}

if (!isPageLayoutComponent(elm)) {
for (const [propName, argType] of Object.entries(layoutBoxArgTypes)) {
const binding =
elm.layout?.[propName as keyof typeof layoutBoxArgTypes] ||
appDom.createConst(argType?.defaultValue ?? undefined);
const bindingId = `${elm.id}.layout.${propName}`;
const scopePath = `${elm.name}.${propName}`;
parsedBindingsMap.set(bindingId, parseBinding(binding, { scopePath }));
}
}
}

if (appDom.isQuery(elm)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import ErrorAlert from './ErrorAlert';
import NodeNameEditor from '../NodeNameEditor';
import { useToolpadComponent } from '../toolpadComponents';
import { getElementNodeComponentId } from '../../../toolpadComponents';
import {
layoutBoxAlignArgTypeDef,
layoutBoxJustifyArgTypeDef,
} from '../../../toolpadComponents/layoutBox';
import { layoutBoxArgTypes } from '../../../toolpadComponents/layoutBox';

const classes = {
control: 'Toolpad_Control',
Expand Down Expand Up @@ -55,7 +52,7 @@ function ComponentPropsEditor<P>({ componentConfig, node }: ComponentPropsEditor
node={node}
namespace="layout"
name="boxJustify"
argType={layoutBoxJustifyArgTypeDef}
argType={layoutBoxArgTypes.boxJustify}
/>
</div>
) : null}
Expand All @@ -65,7 +62,7 @@ function ComponentPropsEditor<P>({ componentConfig, node }: ComponentPropsEditor
node={node}
namespace="layout"
name="boxAlign"
argType={layoutBoxAlignArgTypeDef}
argType={layoutBoxArgTypes.boxAlign}
/>
</div>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export default function NodeAttributeEditor({
[domApi, node, namespace, name],
);

const propValue: BindableAttrValue<unknown> | null =
(node as any)[namespace]?.[name] ??
(argType.defaultValue && appDom.createConst(argType.defaultValue)) ??
null;
const propValue: BindableAttrValue<unknown> | null = (node as any)[namespace]?.[name] ?? null;

const bindingId = `${node.id}${namespace ? `.${namespace}` : ''}.${name}`;
const { bindings, pageState } = usePageEditorState();
Expand Down
9 changes: 7 additions & 2 deletions packages/toolpad-app/src/toolpadComponents/layoutBox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BoxProps } from '@mui/material';
import { ArgTypeDefinition } from '@mui/toolpad-core';

export const layoutBoxAlignArgTypeDef: ArgTypeDefinition<BoxProps['alignItems']> = {
const layoutBoxAlignArgTypeDef: ArgTypeDefinition<BoxProps['alignItems']> = {
typeDef: {
type: 'string',
enum: ['start', 'center', 'end', 'space-between', 'space-around', 'space-evenly'],
Expand All @@ -11,7 +11,7 @@ export const layoutBoxAlignArgTypeDef: ArgTypeDefinition<BoxProps['alignItems']>
defaultValue: 'center',
};

export const layoutBoxJustifyArgTypeDef: ArgTypeDefinition<BoxProps['justifyContent']> = {
const layoutBoxJustifyArgTypeDef: ArgTypeDefinition<BoxProps['justifyContent']> = {
typeDef: {
type: 'string',
enum: ['start', 'center', 'end', 'space-between', 'space-around', 'space-evenly'],
Expand All @@ -20,3 +20,8 @@ export const layoutBoxJustifyArgTypeDef: ArgTypeDefinition<BoxProps['justifyCont
control: { type: 'HorizontalAlign' },
defaultValue: 'start',
};

export const layoutBoxArgTypes = {
boxAlign: layoutBoxAlignArgTypeDef,
boxJustify: layoutBoxJustifyArgTypeDef,
};

0 comments on commit e6e4147

Please sign in to comment.