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 default value not initializing for controlled properties #3445

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions packages/toolpad-studio/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ function parseBindings(
for (const [propName, argType] of Object.entries(argTypes)) {
invariant(argType, `Missing argType for prop "${propName}"`);

const initializerId = argType.defaultValueProp
? `${elm.id}.props.${argType.defaultValueProp}`
: undefined;
const initializerExpression = argType.defaultValueProp
? `${elm.name}.${argType.defaultValueProp}`
: JSON.stringify(getArgTypeDefaultValue(argType));

const propValue: BindableAttrValue<any> = elm.props?.[propName];

Expand All @@ -503,7 +503,7 @@ function parseBindings(
controlled.add(bindingId);
parsedBindingsMap.set(bindingId, {
scopePath,
initializer: initializerId,
initializer: initializerExpression,
});
} else {
parsedBindingsMap.set(bindingId, parseBinding(binding, { scopePath }));
Expand Down
2 changes: 1 addition & 1 deletion packages/toolpad-studio/src/runtime/evalJsBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default function evalJsBindings(

const initializer = binding.initializer;
if (initializer) {
const result = evaluateBinding(initializer, scopePath);
const result = jsRuntime.evaluateExpression(initializer, proxiedScope);
if (result) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v1
kind: application
spec: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from 'react';
import { createComponent } from '@toolpad/studio/browser';

export interface MyTextFieldProps {
msg: string;
onMsgChange: (newMsg: string) => void;
}

function MyTextField({ msg, onMsgChange }: MyTextFieldProps) {
return <input value={msg} onChange={(event) => onMsgChange(event.target.value)} />;
}

export default createComponent(MyTextField, {
argTypes: {
msg: {
type: 'string',
default: 'Hello world!',
onChangeProp: 'onMsgChange',
},
},
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/mui/mui-toolpad/v0.1.54/docs/schemas/v1/definitions.json#properties/Page

apiVersion: v1
kind: page
spec:
id: 5h03vdu
title: page
display: shell
content:
- component: PageRow
name: pageRow
children:
- component: codeComponent.MyBarChart
name: codeComponent_MyBarChart
- component: codeComponent.MyBarChart
name: codeComponent_MyBarChart
layout:
columnSize: 1
alias:
- 5h03vdu
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/mui/mui-toolpad/v0.1.54/docs/schemas/v1/definitions.json#properties/Page

apiVersion: v1
kind: page
spec:
title: page1
display: shell
authorization:
allowAll: true
content:
- component: codeComponent.MyTextField
name: myTextField
- component: Text
name: text
props:
value:
$$jsExpression: '`Output: ${myTextField.msg}`'
8 changes: 8 additions & 0 deletions test/integration/codeComponents/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ export default createComponent(MyInspector, {

await expect(editorModel.appCanvas.getByText('Hello everyone!')).toBeVisible();
});

test('Can handle default values for controlled props', async ({ page }) => {
const runtimeModel = new ToolpadRuntime(page);
await runtimeModel.goToPage('page1');

const test1 = page.getByText('Output: Hello world!');
await expect(test1).toBeVisible();
});
Loading