@gustavo-valsechi/client 1.4.5 → 1.4.7

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.
@@ -44,6 +44,7 @@ var Types = __toESM(require("./types"));
44
44
  __reExport(form_exports, require("./types"), module.exports);
45
45
  function Form(props) {
46
46
  const formRef = props.formRef || (0, import_react.useRef)({});
47
+ const [submitting, setSubmitting] = (0, import_react.useState)(false);
47
48
  const setSchema = () => {
48
49
  const schema2 = {};
49
50
  import_lodash.default.forEach(props.inputs, (data) => {
@@ -92,14 +93,27 @@ function Form(props) {
92
93
  }
93
94
  );
94
95
  };
95
- return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Container, { ref: formRef, onSubmit: handleSubmit(props.onSubmit), children: [
96
+ const onSubmit = async (data) => {
97
+ if (!props.onSubmit) return;
98
+ setSubmitting(true);
99
+ await props.onSubmit(data);
100
+ setSubmitting(false);
101
+ };
102
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Container, { ref: formRef, onSubmit: handleSubmit(onSubmit), children: [
96
103
  import_lodash.default.map(
97
104
  props.inputs,
98
105
  (data, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.default.Fragment, { children: component(data) }, index)
99
106
  ),
100
107
  import_lodash.default.isArray(props.buttons) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "form-buttons", children: import_lodash.default.map(
101
108
  props.buttons,
102
- (data, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Types.Button, { ...data }, index)
109
+ (data, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
110
+ Types.Button,
111
+ {
112
+ ...data,
113
+ loading: data.type === "submit" ? submitting || !!data.loading : !!data.loading
114
+ },
115
+ index
116
+ )
103
117
  ) })
104
118
  ] });
105
119
  }
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { jsx, jsxs } from "react/jsx-runtime";
3
- import React, { useEffect, useRef } from "react";
3
+ import React, { useEffect, useRef, useState } from "react";
4
4
  import { Container } from "./styles";
5
5
  import { z } from "zod";
6
6
  import { useForm } from "react-hook-form";
@@ -10,6 +10,7 @@ import * as Types from "./types";
10
10
  export * from "./types";
11
11
  function Form(props) {
12
12
  const formRef = props.formRef || useRef({});
13
+ const [submitting, setSubmitting] = useState(false);
13
14
  const setSchema = () => {
14
15
  const schema2 = {};
15
16
  _.forEach(props.inputs, (data) => {
@@ -58,14 +59,27 @@ function Form(props) {
58
59
  }
59
60
  );
60
61
  };
61
- return /* @__PURE__ */ jsxs(Container, { ref: formRef, onSubmit: handleSubmit(props.onSubmit), children: [
62
+ const onSubmit = async (data) => {
63
+ if (!props.onSubmit) return;
64
+ setSubmitting(true);
65
+ await props.onSubmit(data);
66
+ setSubmitting(false);
67
+ };
68
+ return /* @__PURE__ */ jsxs(Container, { ref: formRef, onSubmit: handleSubmit(onSubmit), children: [
62
69
  _.map(
63
70
  props.inputs,
64
71
  (data, index) => /* @__PURE__ */ jsx(React.Fragment, { children: component(data) }, index)
65
72
  ),
66
73
  _.isArray(props.buttons) && /* @__PURE__ */ jsx("div", { className: "form-buttons", children: _.map(
67
74
  props.buttons,
68
- (data, index) => /* @__PURE__ */ jsx(Types.Button, { ...data }, index)
75
+ (data, index) => /* @__PURE__ */ jsx(
76
+ Types.Button,
77
+ {
78
+ ...data,
79
+ loading: data.type === "submit" ? submitting || !!data.loading : !!data.loading
80
+ },
81
+ index
82
+ )
69
83
  ) })
70
84
  ] });
71
85
  }
@@ -56,10 +56,10 @@ function Button(props) {
56
56
  loading: String(loading || !!props.loading),
57
57
  disabled: props.disabled,
58
58
  type: props.type || "button",
59
- onClick,
59
+ onClick: async (e) => await onClick(e),
60
60
  ...import_lodash.default.omit(props, ["loading", "type", "onClick"]),
61
61
  children: [
62
- !!props.loading && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_loading.Loading, {}),
62
+ (loading || !!props.loading) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_loading.Loading, {}),
63
63
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: props.label })
64
64
  ]
65
65
  }
@@ -23,10 +23,10 @@ function Button(props) {
23
23
  loading: String(loading || !!props.loading),
24
24
  disabled: props.disabled,
25
25
  type: props.type || "button",
26
- onClick,
26
+ onClick: async (e) => await onClick(e),
27
27
  ..._.omit(props, ["loading", "type", "onClick"]),
28
28
  children: [
29
- !!props.loading && /* @__PURE__ */ jsx(Loading, {}),
29
+ (loading || !!props.loading) && /* @__PURE__ */ jsx(Loading, {}),
30
30
  /* @__PURE__ */ jsx("span", { children: props.label })
31
31
  ]
32
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gustavo-valsechi/client",
3
- "version": "1.4.5",
3
+ "version": "1.4.7",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",