@cripty2001/utils 0.0.100 → 0.0.101
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.
- package/dist/react-ui/button.d.ts +6 -0
- package/dist/react-ui/button.js +24 -0
- package/dist/react-ui/form.d.ts +19 -0
- package/dist/react-ui/form.js +32 -0
- package/dist/react-ui/index.d.ts +6 -0
- package/dist/react-ui/index.js +16 -0
- package/dist/react-ui/input.d.ts +16 -0
- package/dist/react-ui/input.js +31 -0
- package/dist/react-ui/loader.d.ts +8 -0
- package/dist/react-ui/loader.js +16 -0
- package/dist/react-ui/table.d.ts +5 -0
- package/dist/react-ui/table.js +12 -0
- package/package.json +5 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = Button;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const lucide_react_1 = require("lucide-react");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
function Button({ title, onClick, className }) {
|
|
8
|
+
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
9
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
10
|
+
const handleClick = async () => {
|
|
11
|
+
if (loading)
|
|
12
|
+
return;
|
|
13
|
+
setLoading(true);
|
|
14
|
+
(async () => {
|
|
15
|
+
const p = onClick();
|
|
16
|
+
if (p instanceof Promise) {
|
|
17
|
+
await p;
|
|
18
|
+
}
|
|
19
|
+
})()
|
|
20
|
+
.catch(e => setError(e instanceof Error ? e.message : "Unknown error"))
|
|
21
|
+
.finally(() => setLoading(false));
|
|
22
|
+
};
|
|
23
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { onClick: handleClick, className: `cursor-pointer ${className}`, children: loading ? (0, jsx_runtime_1.jsx)(lucide_react_1.Loader2, { className: "w-4 h-4 animate-spin" }) : (0, jsx_runtime_1.jsx)("div", { children: title }) }), error && (0, jsx_runtime_1.jsx)("div", { className: "text-red-500", children: error })] }));
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type FormComponentPropsInput<Variants extends Record<string, string>> = {
|
|
2
|
+
label: string;
|
|
3
|
+
required: boolean;
|
|
4
|
+
key: string;
|
|
5
|
+
} & ({
|
|
6
|
+
type: "text" | "email" | "password" | "tel";
|
|
7
|
+
} | {
|
|
8
|
+
type: "select";
|
|
9
|
+
options: string[];
|
|
10
|
+
});
|
|
11
|
+
export type FormComponentProps<T extends Record<string, string>, Variants extends Record<string, string>> = {
|
|
12
|
+
inputs: FormComponentPropsInput<Variants>[];
|
|
13
|
+
onSubmit: (values: T) => void;
|
|
14
|
+
submitLabel: string;
|
|
15
|
+
value: T;
|
|
16
|
+
setValue: React.Dispatch<React.SetStateAction<T>>;
|
|
17
|
+
variants: Variants;
|
|
18
|
+
};
|
|
19
|
+
export default function FormComponent<T extends Record<string, string>, Variants extends Record<string, string>>(props: FormComponentProps<T, Variants>): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = FormComponent;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const button_1 = __importDefault(require("./button"));
|
|
9
|
+
const input_1 = __importDefault(require("./input"));
|
|
10
|
+
function FormComponent(props) {
|
|
11
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col gap-4", children: [props.inputs.map(input => (() => {
|
|
12
|
+
const value = props.value[input.key];
|
|
13
|
+
const setValue = (v) => props.setValue(prev => ({
|
|
14
|
+
...prev,
|
|
15
|
+
[input.key]: v
|
|
16
|
+
}));
|
|
17
|
+
switch (input.type) {
|
|
18
|
+
case "text":
|
|
19
|
+
case "email":
|
|
20
|
+
case "password":
|
|
21
|
+
case "tel":
|
|
22
|
+
return (0, jsx_runtime_1.jsx)(input_1.default, { label: input.label, value: value, setValue: setValue, variant: "form", variants: props.variants, children: ({ value, setValue, className }) => ((0, jsx_runtime_1.jsx)("input", { type: input.type, value: value, onChange: (e) => setValue(e.target.value), required: input.required, className: className, placeholder: input.label })) });
|
|
23
|
+
case "select":
|
|
24
|
+
return (0, jsx_runtime_1.jsx)(input_1.default, { label: input.label, value: value, setValue: setValue, variant: "form", variants: props.variants, required: input.required, validate: (v) => {
|
|
25
|
+
if (!input.options.includes(v))
|
|
26
|
+
throw new Error("Invalid option");
|
|
27
|
+
}, children: ({ value, setValue, className }) => ((0, jsx_runtime_1.jsx)("select", { value: value, onChange: (e) => setValue(e.target.value), required: input.required, className: className, children: input.options.map(option => ((0, jsx_runtime_1.jsx)("option", { value: option, children: option }, option))) })) });
|
|
28
|
+
default:
|
|
29
|
+
return (0, jsx_runtime_1.jsx)("div", { className: "text-red-500 bg-white", children: "Unknown input type" });
|
|
30
|
+
}
|
|
31
|
+
})()), (0, jsx_runtime_1.jsx)(button_1.default, { title: props.submitLabel, onClick: () => props.onSubmit(props.value) })] }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import BaseButton, { type ButtonProps as BaseButtonProps } from './button';
|
|
2
|
+
import BaseForm, { type FormComponentProps as BaseFormProps } from './form';
|
|
3
|
+
import BaseInput, { type InputComponentProps as BaseInputProps } from './input';
|
|
4
|
+
import BaseLoader, { type LoaderProps as BaseLoaderProps } from './loader';
|
|
5
|
+
import BaseTable, { type TableProps as BaseTableProps } from './table';
|
|
6
|
+
export { BaseButton, BaseButtonProps, BaseForm, BaseFormProps, BaseInput, BaseInputProps, BaseLoader, BaseLoaderProps, BaseTable, BaseTableProps };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BaseTable = exports.BaseLoader = exports.BaseInput = exports.BaseForm = exports.BaseButton = void 0;
|
|
7
|
+
const button_1 = __importDefault(require("./button"));
|
|
8
|
+
exports.BaseButton = button_1.default;
|
|
9
|
+
const form_1 = __importDefault(require("./form"));
|
|
10
|
+
exports.BaseForm = form_1.default;
|
|
11
|
+
const input_1 = __importDefault(require("./input"));
|
|
12
|
+
exports.BaseInput = input_1.default;
|
|
13
|
+
const loader_1 = __importDefault(require("./loader"));
|
|
14
|
+
exports.BaseLoader = loader_1.default;
|
|
15
|
+
const table_1 = __importDefault(require("./table"));
|
|
16
|
+
exports.BaseTable = table_1.default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type InputComponentProps<Variants extends Record<string, string>> = {
|
|
2
|
+
label?: string;
|
|
3
|
+
value: string;
|
|
4
|
+
setValue: (value: string) => void;
|
|
5
|
+
required?: boolean;
|
|
6
|
+
children: ({ value, setValue, className }: {
|
|
7
|
+
value: string;
|
|
8
|
+
setValue: (value: string) => void;
|
|
9
|
+
className: string;
|
|
10
|
+
}) => React.ReactNode;
|
|
11
|
+
copy?: boolean;
|
|
12
|
+
variant?: keyof Variants;
|
|
13
|
+
validate?: (value: string) => void;
|
|
14
|
+
variants: Variants;
|
|
15
|
+
};
|
|
16
|
+
export default function InputComponent<Variants extends Record<string, string>>(props: InputComponentProps<Variants>): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = InputComponent;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const utils_1 = require("@cripty2001/utils");
|
|
6
|
+
const lucide_react_1 = require("lucide-react");
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
function InputComponent(props) {
|
|
9
|
+
const variant = props.variant ?? "";
|
|
10
|
+
const baseClassName = props.variants[variant] ?? "";
|
|
11
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
12
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col gap-2", children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex flex-row justify-between items-center gap-2", children: [props.label &&
|
|
13
|
+
(0, jsx_runtime_1.jsx)("label", { className: "block mb-1 text-sm font-medium text-gray-700 dark:text-gray-300", children: props.label }), props.copy &&
|
|
14
|
+
(0, jsx_runtime_1.jsx)(lucide_react_1.CopyIcon, { className: "w-4 h-4 cursor-pointer", onClick: () => (0, utils_1.copyToClipboard)(props.value) })] }), (0, jsx_runtime_1.jsx)("div", { className: "relative", children: props.children({
|
|
15
|
+
value: props.value,
|
|
16
|
+
setValue: (v) => {
|
|
17
|
+
try {
|
|
18
|
+
if (props.required && v === "")
|
|
19
|
+
throw new Error("Required field is empty");
|
|
20
|
+
props.validate?.(v);
|
|
21
|
+
props.setValue(v);
|
|
22
|
+
setError(null);
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
setError(e.message);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
className: baseClassName
|
|
29
|
+
}) }), error &&
|
|
30
|
+
(0, jsx_runtime_1.jsx)("div", { className: "text-red-500 text-xs", children: error })] }));
|
|
31
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AppserverData } from "@cripty2001/utils/appserver/client";
|
|
2
|
+
import type { Dispatcher } from "@cripty2001/utils/dispatcher";
|
|
3
|
+
import type React from "react";
|
|
4
|
+
export type LoaderProps<T extends AppserverData> = {
|
|
5
|
+
data: Dispatcher<unknown, T>;
|
|
6
|
+
children: (data: T) => React.ReactNode;
|
|
7
|
+
};
|
|
8
|
+
export default function Loader<T extends AppserverData>(props: LoaderProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = Loader;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_whispr_1 = require("@cripty2001/utils/react-whispr");
|
|
6
|
+
function Loader(props) {
|
|
7
|
+
const data = (0, react_whispr_1.useWhisprValue)(props.data.data);
|
|
8
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "", children: (0, jsx_runtime_1.jsx)(Content, { data: data, children: (data) => props.children(data) }) }));
|
|
9
|
+
}
|
|
10
|
+
function Content({ data, children }) {
|
|
11
|
+
if (data.loading)
|
|
12
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "flex items-center justify-center h-screen", children: (0, jsx_runtime_1.jsx)("div", { className: "animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-gray-900 dark:border-white" }) }));
|
|
13
|
+
if (!data.ok)
|
|
14
|
+
return (0, jsx_runtime_1.jsx)("div", { className: "text-red-500", children: data.error.message });
|
|
15
|
+
return children(data.data);
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = Table;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
function Table({ value, headers }) {
|
|
6
|
+
if (value.length === 0) {
|
|
7
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "text-center py-8 text-gray-600 dark:text-gray-400", children: "No data found" }));
|
|
8
|
+
}
|
|
9
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "bg-white dark:bg-gray-800 rounded-lg shadow-md overflow-hidden", children: (0, jsx_runtime_1.jsx)("div", { className: "overflow-x-auto", children: (0, jsx_runtime_1.jsxs)("table", { className: "w-full", children: [(0, jsx_runtime_1.jsx)("thead", { className: "bg-gray-100 dark:bg-gray-700", children: (0, jsx_runtime_1.jsx)("tr", { children: headers.map((header, index) => ((0, jsx_runtime_1.jsx)("th", { className: "px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider", children: header }, index))) }) }), (0, jsx_runtime_1.jsx)("tbody", { className: "bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700", children: value.map((row, rowIndex) => ((0, jsx_runtime_1.jsx)("tr", { className: "hover:bg-gray-50 dark:hover:bg-gray-700", children: row.map((cell, cellIndex) => ((0, jsx_runtime_1.jsx)("td", { className: `px-6 py-4 whitespace-nowrap text-sm ${cellIndex === 0
|
|
10
|
+
? "font-medium text-gray-900 dark:text-white"
|
|
11
|
+
: "text-gray-600 dark:text-gray-400"}`, children: cell }, cellIndex))) }, rowIndex))) })] }) }) }));
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.101",
|
|
4
4
|
"description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
|
|
5
5
|
"homepage": "https://github.com/cripty2001/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -85,6 +85,10 @@
|
|
|
85
85
|
"./react": {
|
|
86
86
|
"import": "./dist/react/index.js",
|
|
87
87
|
"types": "./dist/react/index.d.ts"
|
|
88
|
+
},
|
|
89
|
+
"./react-ui": {
|
|
90
|
+
"import": "./dist/react-ui/index.js",
|
|
91
|
+
"types": "./dist/react-ui/index.d.ts"
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
94
|
}
|