dune-react 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ export interface CompoundButtonProps {
8
8
  size?: ButtonProps["size"];
9
9
  icon?: IconName;
10
10
  type?: "button" | "submit" | "reset";
11
+ disabled?: boolean;
11
12
  className?: string;
12
13
  }
13
14
  export declare const CompoundButton: (props: CompoundButtonProps) => import("react/jsx-runtime").JSX.Element;
@@ -72,6 +72,7 @@ function CompoundButtonBase(props) {
72
72
  size: props.size,
73
73
  className: cn("gap-4", props.className),
74
74
  type: props.type ?? "button",
75
+ disabled: props.disabled,
75
76
  children: content
76
77
  }
77
78
  );
@@ -5,6 +5,10 @@ type EditorContextValue = {
5
5
  isEditor: boolean;
6
6
  toolbarPortalRef: RefObject<HTMLDivElement | null> | null;
7
7
  fetchLibraryImages?: () => Promise<string[]>;
8
+ /** 站点 ID,用于表单提交等业务接口 */
9
+ siteId?: string;
10
+ /** 后台 API 域名,如 https://api.example.com */
11
+ domain?: string;
8
12
  };
9
13
  export declare const EditorContextProvider: import("react").Provider<EditorContextValue>;
10
14
  export declare const useEditorContext: () => EditorContextValue;
@@ -17,17 +17,42 @@ const DEFAULT_FIELDS = [
17
17
  { label: "Message", name: "message", type: "textarea", required: true }
18
18
  ];
19
19
  const ContactUs = (props) => {
20
- var _a, _b, _c, _d, _e, _f, _g, _h;
21
- const { isEditor: isEditorMode } = useEditorContext();
20
+ var _a, _b, _c, _d, _e, _f;
21
+ const { isEditor: isEditorMode, siteId, domain } = useEditorContext();
22
22
  const formFields = ((_b = (_a = props.form) == null ? void 0 : _a.fields) == null ? void 0 : _b.length) ? props.form.fields : DEFAULT_FIELDS;
23
23
  const [values, setValues] = useState({});
24
+ const [submitting, setSubmitting] = useState(false);
24
25
  const setValue = (name, value) => setValues((prev) => ({ ...prev, [name]: value }));
25
- const handleSubmit = (e) => {
26
+ const handleSubmit = async (e) => {
26
27
  e.preventDefault();
27
28
  if (isEditorMode) return;
28
- console.log({ ...values });
29
- toast.success("Message sent successfully!");
30
- setValues({});
29
+ setSubmitting(true);
30
+ try {
31
+ const baseUrl = (domain || "").replace(/\/$/, "");
32
+ const res = await fetch(`${baseUrl}/api/form-leads`, {
33
+ method: "POST",
34
+ headers: { "Content-Type": "application/json" },
35
+ body: JSON.stringify({
36
+ site_id: siteId,
37
+ name: values.name || "",
38
+ email: values.email || "",
39
+ phone: values.phone || "",
40
+ message: values.message || "",
41
+ source_page: typeof window !== "undefined" ? window.location.pathname : ""
42
+ })
43
+ });
44
+ const data = await res.json().catch(() => null);
45
+ if (!res.ok || !(data == null ? void 0 : data.success)) {
46
+ throw new Error((data == null ? void 0 : data.errMsg) || `HTTP ${res.status}`);
47
+ }
48
+ toast.success("Message sent successfully!");
49
+ setValues({});
50
+ } catch (err) {
51
+ const msg = err instanceof Error ? err.message : "Failed to send message";
52
+ toast.error(msg);
53
+ } finally {
54
+ setSubmitting(false);
55
+ }
31
56
  };
32
57
  const submitLabel = ((_d = (_c = props.form) == null ? void 0 : _c.button) == null ? void 0 : _d.label) ?? "Send Message";
33
58
  const submitIcon = ((_f = (_e = props.form) == null ? void 0 : _e.button) == null ? void 0 : _f.icon) ?? "send";
@@ -49,8 +74,6 @@ const ContactUs = (props) => {
49
74
  {
50
75
  className: "rounded-md max-w-md w-full flex flex-col border p-8 gap-4",
51
76
  onSubmit: handleSubmit,
52
- action: (_g = props.form) == null ? void 0 : _g.action,
53
- method: (_h = props.form) == null ? void 0 : _h.method,
54
77
  children: [
55
78
  formFields.reduce((rows, field, i) => {
56
79
  var _a2, _b2, _c2;
@@ -127,11 +150,12 @@ const ContactUs = (props) => {
127
150
  /* @__PURE__ */ jsx(
128
151
  CompoundButton,
129
152
  {
130
- label: submitLabel,
153
+ label: submitting ? "Sending..." : submitLabel,
131
154
  variant: "default",
132
155
  size: "default",
133
- icon: submitIcon,
134
- type: "submit"
156
+ icon: submitting ? "loader" : submitIcon,
157
+ type: "submit",
158
+ disabled: submitting
135
159
  }
136
160
  )
137
161
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dune-react",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {