Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Add initial props

Ivan Starkov1ac167d959a4
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
index b219be37ae..c77ee9bcbf 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
@@ -5,7 +5,10 @@ import type {
MetaProps,
} from "@webstudio-is/react-sdk";
import { type Publish } from "~/shared/pubsub";
-import { getComponentMetaProps } from "@webstudio-is/react-sdk";
+import {
+ getComponentMeta,
+ getComponentMetaProps,
+} from "@webstudio-is/react-sdk";
import ObjectId from "bson-objectid";
import produce from "immer";
// @todo: importing normally doesn't work in Jest for some reason
@@ -100,11 +103,55 @@ const getPropsWithDefaultValue = (
});
};
+const getInitialProps = (
+ selectedInstanceData: SelectedInstanceData
+): UserProp[] => {
+ const props =
+ selectedInstanceData.props === undefined ||
+ selectedInstanceData.props.props.length === 0
+ ? []
+ : selectedInstanceData.props.props;
+
+ const { component } = selectedInstanceData;
+ const meta = getComponentMeta(component);
+ const metaProps = getComponentMetaProps(component);
+
+ const initialProps = meta.initialProps ?? [];
+
+ const userProps: UserProp[] = [];
+
+ // Preserve ordering from `initialProps` getting values from DB props or default values
+ for (const initialProp of initialProps) {
+ const userProp = props.find((prop) => prop.prop === initialProp);
+ if (userProp !== undefined) {
+ userProps.push(userProp);
+ continue;
+ }
+
+ const metaPropValue = metaProps[initialProp];
+ if (metaPropValue !== undefined) {
+ const typedValue = getValueFromPropMeta(metaPropValue);
+
+ userProps.push({
+ id: ObjectId().toString(),
+ prop: initialProp,
+ ...typedValue,
+ });
+
+ continue;
+ }
+ }
+ return userProps;
+};
+
type UsePropsLogic = {
publish: Publish;
selectedInstanceData: SelectedInstanceData;
};
+/**
+ * usePropsLogic expects that key={selectedInstanceData.id} is used on the ancestor component
+ */
export const usePropsLogic = ({
selectedInstanceData,
publish,
@@ -115,17 +162,19 @@ export const usePropsLogic = ({
? []
: selectedInstanceData.props.props;
- const initialState = uniqBy(
- [
- ...props,
- ...getPropsWithDefaultValue(selectedInstanceData),
- ...getRequiredProps(selectedInstanceData),
- ],
- "prop"
+ // Prefer ordering from `initialProps`
+ const [userProps, setUserProps] = useState<Array<UserProp>>(() =>
+ uniqBy(
+ [
+ ...getInitialProps(selectedInstanceData),
+ ...getRequiredProps(selectedInstanceData),
+ ...getPropsWithDefaultValue(selectedInstanceData),
+ ...props,
+ ],
+ "prop"
+ )
);
- const [userProps, setUserProps] = useState<Array<UserProp>>(initialState);
-
const updateProps = (updates: UserPropsUpdates["updates"]) => {
publish({
type: "updateProps",
diff --git a/packages/react-sdk/src/components/component-type.ts b/packages/react-sdk/src/components/component-type.ts
index 50f3b37737..d1101faab2 100644
--- a/packages/react-sdk/src/components/component-type.ts
+++ b/packages/react-sdk/src/components/component-type.ts
@@ -26,6 +26,7 @@ export type WsComponentMeta = {
defaultStyle?: Style;
children?: Array<string>;
props: MetaProps;
+ initialProps?: Array<string>;
};
const Props = z.record(
@@ -85,5 +86,6 @@ export const WsComponentMeta = z.lazy(() =>
defaultStyle: z.optional(z.any()),
children: z.optional(z.array(z.string())),
props: Props,
+ initialProps: z.optional(z.array(z.string())),
})
);
diff --git a/packages/react-sdk/src/components/image.ws.tsx b/packages/react-sdk/src/components/image.ws.tsx
index 3125562ce7..d8143b7670 100644
--- a/packages/react-sdk/src/components/image.ws.tsx
+++ b/packages/react-sdk/src/components/image.ws.tsx
@@ -23,6 +23,7 @@ const meta: WsComponentMeta = {
Icon: ImageIcon,
defaultStyle,
props: props as MetaProps,
+ initialProps: ["src", "width", "height", "alt", "loading"],
};
export default meta;