@alepha/ui 0.10.6 → 0.11.0

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,125 @@
1
+ import { type TObject, TypeBoxError } from "@alepha/core";
2
+ import type { InputField } from "@alepha/react-form";
3
+ import type { ReactNode } from "react";
4
+ import type { ControlProps } from "../components/Control.tsx";
5
+ import { getDefaultIcon } from "./icons.tsx";
6
+ import { prettyName } from "./string.ts";
7
+
8
+ export const parseInput = (
9
+ props: GenericControlProps,
10
+ form: {
11
+ error?: Error;
12
+ },
13
+ ): ControlInput => {
14
+ const disabled = false; // form.loading;
15
+ const id = props.input.props.id;
16
+ const label =
17
+ props.title ??
18
+ ("title" in props.input.schema &&
19
+ typeof props.input.schema.title === "string"
20
+ ? props.input.schema.title
21
+ : undefined) ??
22
+ prettyName(props.input.path);
23
+ const description =
24
+ props.description ??
25
+ ("description" in props.input.schema &&
26
+ typeof props.input.schema.description === "string"
27
+ ? props.input.schema.description
28
+ : undefined);
29
+ const error =
30
+ form.error && form.error instanceof TypeBoxError
31
+ ? form.error.value.message
32
+ : undefined;
33
+
34
+ // Auto-generate icon if not provided
35
+ const icon =
36
+ props.icon ??
37
+ getDefaultIcon({
38
+ type:
39
+ props.input.schema && "type" in props.input.schema
40
+ ? String(props.input.schema.type)
41
+ : undefined,
42
+ format:
43
+ props.input.schema &&
44
+ "format" in props.input.schema &&
45
+ typeof props.input.schema.format === "string"
46
+ ? props.input.schema.format
47
+ : undefined,
48
+ name: props.input.props.name,
49
+ isEnum:
50
+ props.input.schema &&
51
+ "enum" in props.input.schema &&
52
+ Boolean(props.input.schema.enum),
53
+ isArray:
54
+ props.input.schema &&
55
+ "type" in props.input.schema &&
56
+ props.input.schema.type === "array",
57
+ });
58
+
59
+ const format =
60
+ props.input.schema &&
61
+ "format" in props.input.schema &&
62
+ typeof props.input.schema.format === "string"
63
+ ? props.input.schema.format
64
+ : undefined;
65
+
66
+ const required = props.input.required;
67
+ const schema = props.input.schema as TObject & { $control?: ControlProps };
68
+
69
+ const inputProps: InputProps = {
70
+ label,
71
+ description,
72
+ error,
73
+ required,
74
+ disabled,
75
+ };
76
+
77
+ if ("minLength" in schema && typeof schema.minLength === "number") {
78
+ inputProps.minLength = schema.minLength;
79
+ }
80
+ if ("maxLength" in schema && typeof schema.maxLength === "number") {
81
+ inputProps.maxLength = schema.maxLength;
82
+ }
83
+ if ("minimum" in schema && typeof schema.minimum === "number") {
84
+ inputProps.minimum = schema.minimum;
85
+ }
86
+ if ("maximum" in schema && typeof schema.maximum === "number") {
87
+ inputProps.maximum = schema.maximum;
88
+ }
89
+
90
+ return {
91
+ id,
92
+ icon,
93
+ format,
94
+ schema: props.input.schema as TObject & { $control?: ControlProps },
95
+ inputProps,
96
+ };
97
+ };
98
+
99
+ export interface GenericControlProps {
100
+ input: InputField;
101
+ title?: string;
102
+ description?: string;
103
+ icon?: ReactNode;
104
+ }
105
+
106
+ export interface ControlInput {
107
+ id?: string;
108
+ icon: ReactNode;
109
+ format?: string;
110
+ schema: TObject & { $control?: ControlProps };
111
+ inputProps: InputProps;
112
+ }
113
+
114
+ export interface InputProps {
115
+ label: string;
116
+ description?: string;
117
+ error?: string;
118
+ required: boolean;
119
+ disabled: boolean;
120
+
121
+ minLength?: number;
122
+ maxLength?: number;
123
+ minimum?: number;
124
+ maximum?: number;
125
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Capitalizes the first letter of a string.
3
+ *
4
+ * @example
5
+ * capitalize("hello") // "Hello"
6
+ */
7
+ export const capitalize = (str: string): string => {
8
+ return str.charAt(0).toUpperCase() + str.slice(1);
9
+ };
10
+
11
+ /**
12
+ * Converts a path or identifier string into a pretty display name.
13
+ * Removes slashes and capitalizes the first letter.
14
+ *
15
+ * @example
16
+ * prettyName("/userName") // "UserName"
17
+ * prettyName("email") // "Email"
18
+ */
19
+ export const prettyName = (name: string): string => {
20
+ return capitalize(name.replaceAll("/", ""));
21
+ };