@bouko/react 2.2.5 → 2.2.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.
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useState } from "react";
3
4
  import { useForm } from "./functions";
4
5
  import { cn } from "@bouko/style";
5
6
  import { RowBox } from "../layout/flex";
@@ -11,6 +12,7 @@ import Attachment from "../attachment";
11
12
  import { Button } from "../button";
12
13
  import CheckCircle from "../../assets/icons/check-circle.svg";
13
14
  import XCircle from "../../assets/icons/x-circle.svg";
15
+ import Spinner from "../../assets/icons/spinner.svg";
14
16
  export function mapIdToInitial(arrays) {
15
17
  const result = {};
16
18
  for (const inner of arrays) {
@@ -25,10 +27,24 @@ export function mapIdToInitial(arrays) {
25
27
  export default function FormBuilder({ fields, button, styles, submit, cancel = false }) {
26
28
  const initialShi = mapIdToInitial(fields);
27
29
  const { data, setField, clear } = useForm(initialShi);
28
- const isValid = true; // validator(data).success;
30
+ const [isLoading, setLoading] = useState(false);
31
+ const requiredFields = fields.flatMap(arr => arr.filter(item => item.required !== false));
32
+ const isValid = requiredFields.filter(x => {
33
+ const val = data[x.id];
34
+ return !val || val === "";
35
+ }).length === 0; // validator(data).success;
29
36
  // const play = useSound(sound);
30
37
  const handleSubmit = async () => {
31
- await submit(data);
38
+ setLoading(true);
39
+ try {
40
+ await submit(data);
41
+ }
42
+ catch (err) {
43
+ let msg = err.message;
44
+ if (msg !== "NEXT_REDIRECT")
45
+ alert(msg);
46
+ }
47
+ setLoading(false);
32
48
  };
33
49
  return (_jsxs("div", { className: cn("flex flex-col w-full gap-4", styles?.container), children: [fields.map((row, i) => (_jsx(RowBox, { style: "w-full gap-5 overflow-hidden", children: row.map(({ element, id, rows, label, placeholder, disabled, options, required, note }) => {
34
50
  if (element === "input")
@@ -41,5 +57,5 @@ export default function FormBuilder({ fields, button, styles, submit, cancel = f
41
57
  return (_jsx(MultipleChoice, { id: id, label: label, options: options || [], value: data[id], update: x => setField(id, x), note: note, required: required }, id));
42
58
  else if (element === "attachment")
43
59
  return (_jsx(Attachment, { id: id, label: label, value: data[id], update: x => setField(id, x), note: note, required: required }, id));
44
- }) }, i))), _jsxs(RowBox, { style: "items-center gap-2 mt-2 w-full", children: [_jsxs(Button, { style: cn("text-sm", styles?.submit), onClick: handleSubmit, disabled: !isValid, children: [_jsx("div", { children: _jsx(CheckCircle, {}) }), button || "Submit"] }), cancel !== false && (_jsxs(Button, { style: cn("text-sm text-error hover:text-error-light", styles?.cancel), variant: "ghost", onClick: clear, children: [_jsx(XCircle, {}), "Cancel"] }))] })] }));
60
+ }) }, i))), _jsxs(RowBox, { style: "items-center gap-2 mt-2 w-full", children: [_jsxs(Button, { style: cn("flex-1 text-sm", styles?.submit), onClick: handleSubmit, disabled: !isValid, children: [_jsx("div", { className: cn(isLoading ? "hidden" : ""), children: _jsx(CheckCircle, {}) }), _jsx("div", { className: cn(isLoading ? "animate-spin" : "hidden"), children: _jsx(Spinner, {}) }), button || "Submit"] }), cancel !== false && (_jsxs(Button, { style: cn("text-sm text-error hover:text-error-light", styles?.cancel), variant: "ghost", onClick: clear, children: [_jsx(XCircle, {}), "Cancel"] }))] })] }));
45
61
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  "name": "@bouko/react",
4
4
 
5
- "version": "2.2.5",
5
+ "version": "2.2.7",
6
6
 
7
7
  "main": "./dist/index.js",
8
8