@bouko/react 2.2.4 → 2.2.5

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.
@@ -0,0 +1,24 @@
1
+ import { FormBuilderField } from "./types";
2
+ type Props<T> = {
3
+ styles?: {
4
+ container?: string;
5
+ submit?: string;
6
+ cancel?: string;
7
+ };
8
+ id?: string;
9
+ button?: string;
10
+ fields: FormBuilderField<T>;
11
+ validator?: (x: Partial<T>) => ({
12
+ success: boolean;
13
+ });
14
+ sound?: string;
15
+ cancel?: boolean;
16
+ submit: (data: T) => Promise<void | never>;
17
+ };
18
+ type Item = {
19
+ id: string;
20
+ initial?: string;
21
+ };
22
+ export declare function mapIdToInitial<T>(arrays: Item[][]): Partial<T>;
23
+ export default function FormBuilder<T>({ fields, button, styles, submit, cancel }: Props<T>): import("react/jsx-runtime").JSX.Element;
24
+ export {};
@@ -0,0 +1,45 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useForm } from "./functions";
4
+ import { cn } from "@bouko/style";
5
+ import { RowBox } from "../layout/flex";
6
+ import Input from "../input";
7
+ import TextArea from "../textarea";
8
+ import Select from "../select";
9
+ import MultipleChoice from "../multiple-choice";
10
+ import Attachment from "../attachment";
11
+ import { Button } from "../button";
12
+ import CheckCircle from "../../assets/icons/check-circle.svg";
13
+ import XCircle from "../../assets/icons/x-circle.svg";
14
+ export function mapIdToInitial(arrays) {
15
+ const result = {};
16
+ for (const inner of arrays) {
17
+ for (const obj of inner) {
18
+ if ("initial" in obj && obj["initial"]) {
19
+ result[obj.id] = obj.initial;
20
+ }
21
+ }
22
+ }
23
+ return result;
24
+ }
25
+ export default function FormBuilder({ fields, button, styles, submit, cancel = false }) {
26
+ const initialShi = mapIdToInitial(fields);
27
+ const { data, setField, clear } = useForm(initialShi);
28
+ const isValid = true; // validator(data).success;
29
+ // const play = useSound(sound);
30
+ const handleSubmit = async () => {
31
+ await submit(data);
32
+ };
33
+ 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
+ if (element === "input")
35
+ return (_jsx(Input, { id: id, styles: { container: "flex-1" }, label: label, placeholder: placeholder, value: data[id], update: x => setField(id, x), disabled: disabled, note: note, required: required }, id));
36
+ else if (element === "select")
37
+ return (_jsx(Select, { id: id, label: label, placeholder: placeholder, options: options || [], value: data[id], update: x => setField(id, x), note: note, required: required }, id));
38
+ else if (element === "textarea")
39
+ return (_jsx(TextArea, { id: id, label: label, placeholder: placeholder, rows: rows, value: data[id], update: x => setField(id, x), note: note, required: required }, id));
40
+ else if (element === "multiple-choice")
41
+ return (_jsx(MultipleChoice, { id: id, label: label, options: options || [], value: data[id], update: x => setField(id, x), note: note, required: required }, id));
42
+ else if (element === "attachment")
43
+ 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"] }))] })] }));
45
+ }
@@ -1,27 +1,3 @@
1
- import { FormBuilderField } from "./fields";
1
+ export { default as FormBuilder } from "./builder";
2
2
  export * from "./functions";
3
3
  export * from "./types";
4
- type Props<T> = {
5
- styles?: {
6
- container?: string;
7
- submit?: string;
8
- cancel?: string;
9
- };
10
- id?: string;
11
- button?: string;
12
- fields: FormBuilderField<T>;
13
- validator?: (x: Partial<T>) => ({
14
- success: boolean;
15
- });
16
- sound?: string;
17
- cancel?: boolean;
18
- submit: (data: T) => Promise<void | never>;
19
- };
20
- export declare function useSound(filename?: string): import("use-sound/dist/types").PlayFunction | undefined;
21
- export * from "./loaders";
22
- type Item = {
23
- id: string;
24
- initial?: string;
25
- };
26
- export declare function mapIdToInitial<T>(arrays: Item[][]): Partial<T>;
27
- export declare function FormBuilder<T>({ fields, button, styles, submit, cancel }: Props<T>): import("react/jsx-runtime").JSX.Element;
@@ -1,39 +1,3 @@
1
- "use client";
2
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useForm } from "./functions";
4
- import { cn } from "@bouko/style";
5
- import Fields from "./fields";
6
- import CheckCircle from "../../assets/icons/check-circle.svg";
7
- import XCircle from "../../assets/icons/x-circle.svg";
8
- import { default as base } from "use-sound";
1
+ export { default as FormBuilder } from "./builder";
9
2
  export * from "./functions";
10
3
  export * from "./types";
11
- import { RowBox } from "../layout/flex";
12
- import { Button } from "../button";
13
- export function useSound(filename) {
14
- if (!filename)
15
- return;
16
- return base(filename)[0];
17
- }
18
- export * from "./loaders";
19
- export function mapIdToInitial(arrays) {
20
- const result = {};
21
- for (const inner of arrays) {
22
- for (const obj of inner) {
23
- if ("initial" in obj && obj["initial"]) {
24
- result[obj.id] = obj.initial;
25
- }
26
- }
27
- }
28
- return result;
29
- }
30
- export function FormBuilder({ fields, button, styles, submit, cancel = false }) {
31
- const initialShi = mapIdToInitial(fields);
32
- const { data, setField, clear } = useForm(initialShi);
33
- const isValid = true; // validator(data).success;
34
- // const play = useSound(sound);
35
- const handleSubmit = async () => {
36
- await submit(data);
37
- };
38
- return (_jsxs("div", { className: cn("flex flex-col w-full gap-4", styles?.container), children: [_jsx(Fields, { fields: fields, data: data, setField: setField }), _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"] }))] })] }));
39
- }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  "name": "@bouko/react",
4
4
 
5
- "version": "2.2.4",
5
+ "version": "2.2.5",
6
6
 
7
7
  "main": "./dist/index.js",
8
8