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.

Forms playground (#1823)

Ivan Starkov0827fd3e6bb5
diff --git a/package.json b/package.json
index 60b61df3aa..ddb48d365c 100644
--- a/package.json
+++ b/package.json
@@ -16,7 +16,11 @@
"storybook:build": "turbo run storybook:build",
"migrations": "cd apps/builder && pnpm migrations --dev",
"build-figma-tokens": "cd packages/design-system && pnpm build-figma-tokens",
- "prepare": "git config core.hooksPath .git/hooks/ && simple-git-hooks"
+ "prepare": "git config core.hooksPath .git/hooks/ && simple-git-hooks",
+ "local:version-snapshot": "pnpm -r exec pnpm version prepatch --preid $(cat /dev/urandom | LC_ALL=C tr -dc 'a-z' | fold -w 8 | head -n 1)",
+ "local:publish-snapshot": "pnpm -r publish --access public --no-git-checks --registry http://localhost:4873",
+ "local:dangerously-undo-version-snapshot": "git restore --source=$(git branch --show-current) '**/*/package.json'",
+ "local:release": "pnpm build && pnpm dts && pnpm local:version-snapshot && pnpm local:publish-snapshot && pnpm local:dangerously-undo-version-snapshot && echo \"now execute\npnpm up -r -L '@webstudio-is/*' --registry http://localhost:4873\""
},
"simple-git-hooks": {
"pre-commit": "./node_modules/.bin/nano-staged"
diff --git a/packages/design-system/src/components/text.ts b/packages/design-system/src/components/text.ts
index bc7c449b1f..c9750093e6 100644
--- a/packages/design-system/src/components/text.ts
+++ b/packages/design-system/src/components/text.ts
@@ -36,6 +36,7 @@ export const textStyle = css({
// destructive in most cases used to show 3rd party errors
// we don't want it to break layout
overflowWrap: "anywhere",
+ userSelect: "auto",
},
},
align: {
diff --git a/packages/form-handlers/src/index.ts b/packages/form-handlers/src/index.ts
index 9addf56192..5d0dc3cccb 100644
--- a/packages/form-handlers/src/index.ts
+++ b/packages/form-handlers/src/index.ts
@@ -1,8 +1,10 @@
export {
formToEmail,
formIdFieldName,
+ formHiddenFieldPrefix,
getFormId,
hasMatchingForm,
+ getFormProperties,
type EmailInfo,
type FormInfo,
} from "./shared";
diff --git a/packages/form-handlers/src/n8n.ts b/packages/form-handlers/src/n8n.ts
index 6889c5eb35..18c73cb66f 100644
--- a/packages/form-handlers/src/n8n.ts
+++ b/packages/form-handlers/src/n8n.ts
@@ -50,6 +50,8 @@ export const n8nHandler = async ({
email: formToEmail(formInfo),
// globally unique form id (can be used for unsubscribing)
formId: [formInfo.projectId, formId].join("--"),
+ action: formInfo.action,
+ method: formInfo.method,
formData: Object.fromEntries(getFormEntries(formInfo.formData)),
};
diff --git a/packages/form-handlers/src/shared.ts b/packages/form-handlers/src/shared.ts
index 5b7c5fb265..4df362e5a0 100644
--- a/packages/form-handlers/src/shared.ts
+++ b/packages/form-handlers/src/shared.ts
@@ -1,6 +1,7 @@
import type { Build } from "@webstudio-is/project-build";
-export const formIdFieldName = "ws--form-id";
+export const formHiddenFieldPrefix = "ws--form";
+export const formIdFieldName = `${formHiddenFieldPrefix}-id`;
// Input data common for all handlers
export type FormInfo = {
@@ -9,6 +10,9 @@ export type FormInfo = {
formData: FormData;
toEmail: string;
fromEmail: string;
+ // null as serializable
+ action: string | null;
+ method: "get" | "post";
};
export type EmailInfo = {
@@ -25,7 +29,9 @@ export type Result = { success: true } | { success: false; errors: string[] };
/** Returns form entries that should be send in email: removes `File` entries and `formId` */
export const getFormEntries = (formData: FormData): [string, string][] =>
[...formData.entries()].flatMap(([key, value]) =>
- key !== formIdFieldName && typeof value === "string" ? [[key, value]] : []
+ key.startsWith(formHiddenFieldPrefix) === false && typeof value === "string"
+ ? [[key, value]]
+ : []
);
export const getFormId = (formData: FormData) => {
@@ -124,3 +130,41 @@ export const hasMatchingForm = (
// We could also check that each entry in formData has a corresponding input control in the tree,
// but that seem like an overkill for now.
};
+
+export const getFormProperties = (
+ formData: FormData,
+ instances: Build["instances"],
+ props: Build["props"]
+) => {
+ const formId = getFormId(formData);
+
+ if (formId === undefined) {
+ return undefined;
+ }
+
+ const hasForm = instances.some(
+ ([, instance]) => instance.id === formId && instance.component === "Form"
+ );
+
+ if (hasForm === false) {
+ return undefined;
+ }
+
+ const objectProps = Object.fromEntries(
+ props
+ .filter(([_, value]) => value.instanceId === formId)
+ .map(([_, value]) => [value.name, value])
+ );
+
+ const action =
+ objectProps.action?.type === "string"
+ ? objectProps.action.value
+ : undefined;
+
+ const method =
+ objectProps.method?.type === "string"
+ ? objectProps.method.value
+ : undefined;
+
+ return { action, method };
+};
diff --git a/packages/sdk-components-react-remix/src/__generated__/form.props.ts b/packages/sdk-components-react-remix/src/__generated__/form.props.ts
index 1eaca1ac73..8d36074047 100644
--- a/packages/sdk-components-react-remix/src/__generated__/form.props.ts
+++ b/packages/sdk-components-react-remix/src/__generated__/form.props.ts
@@ -5,8 +5,10 @@ export const props: Record<string, PropMeta> = {
style: { required: false, control: "text", type: "string" },
title: { required: false, control: "text", type: "string" },
acceptCharset: { required: false, control: "text", type: "string" },
+ action: { required: false, control: "text", type: "string" },
autoComplete: { required: false, control: "text", type: "string" },
encType: { required: false, control: "text", type: "string" },
+ method: { required: false, control: "text", type: "string" },
name: { required: false, control: "text", type: "string" },
noValidate: { required: false, control: "boolean", type: "boolean" },
target: { required: false, control: "text", type: "string" },
diff --git a/packages/sdk-components-react-remix/src/form.tsx b/packages/sdk-components-react-remix/src/form.tsx
index 277611d1d5..b27effb7a5 100644
--- a/packages/sdk-components-react-remix/src/form.tsx
+++ b/packages/sdk-components-react-remix/src/form.tsx
@@ -81,10 +81,10 @@ const withoutMessages = (children: ReactNode) =>
export const Form = forwardRef<
ElementRef<typeof defaultTag>,
- Omit<ComponentProps<typeof defaultTag>, "method" | "action"> & {
+ ComponentProps<typeof defaultTag> & {
initialState?: "initial" | "success" | "error";
}
->(({ children, initialState = "initial", ...props }, ref) => {
+>(({ children, initialState = "initial", action, method, ...props }, ref) => {
const fetcher = useFetcher();
const state =