stackkit 0.3.5 → 0.3.6

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.
Files changed (192) hide show
  1. package/README.md +50 -42
  2. package/dist/cli/add.js +122 -56
  3. package/dist/cli/create.d.ts +2 -0
  4. package/dist/cli/create.js +271 -95
  5. package/dist/cli/doctor.js +1 -0
  6. package/dist/cli/list.d.ts +1 -1
  7. package/dist/cli/list.js +6 -4
  8. package/dist/index.js +234 -191
  9. package/dist/lib/constants.d.ts +4 -0
  10. package/dist/lib/constants.js +4 -0
  11. package/dist/lib/discovery/module-discovery.d.ts +4 -0
  12. package/dist/lib/discovery/module-discovery.js +56 -0
  13. package/dist/lib/generation/code-generator.d.ts +11 -2
  14. package/dist/lib/generation/code-generator.js +42 -3
  15. package/dist/lib/generation/generator-utils.js +3 -1
  16. package/dist/lib/pm/package-manager.js +16 -13
  17. package/dist/lib/ui/logger.js +3 -2
  18. package/dist/lib/utils/path-resolver.d.ts +2 -0
  19. package/dist/lib/utils/path-resolver.js +8 -0
  20. package/dist/meta.json +8312 -0
  21. package/modules/auth/better-auth/files/{shared → express}/config/env.ts +48 -50
  22. package/modules/auth/better-auth/files/express/middlewares/authorize.ts +20 -1
  23. package/modules/auth/better-auth/files/express/modules/auth.controller.ts +349 -0
  24. package/modules/auth/better-auth/files/express/modules/{auth/auth.route.ts → auth.route.ts} +9 -4
  25. package/modules/auth/better-auth/files/express/modules/auth.service.ts +664 -0
  26. package/modules/auth/better-auth/files/express/modules/{auth/auth.type.ts → auth.type.ts} +22 -9
  27. package/modules/auth/better-auth/files/{shared/mongoose/auth/helper.ts → express/mongo-modules/auth.helper.ts} +11 -1
  28. package/modules/auth/better-auth/files/express/types/express.d.ts +11 -0
  29. package/modules/auth/better-auth/files/nextjs/api-route.ts +74 -0
  30. package/modules/auth/better-auth/files/nextjs/dashboard/pages/(user)/page.tsx +6 -0
  31. package/modules/auth/better-auth/files/nextjs/dashboard/pages/admin/page.tsx +6 -0
  32. package/modules/auth/better-auth/files/nextjs/dashboard/pages/layout.tsx +48 -0
  33. package/modules/auth/better-auth/files/nextjs/dashboard/pages/my-profile/page.tsx +5 -0
  34. package/modules/auth/better-auth/files/nextjs/features/services/auth.service.ts +102 -0
  35. package/modules/auth/better-auth/files/nextjs/layout/layout.tsx +13 -0
  36. package/modules/auth/better-auth/files/nextjs/lib/axios/http.ts +158 -0
  37. package/modules/auth/better-auth/files/nextjs/lib/env.ts +35 -0
  38. package/modules/auth/better-auth/files/nextjs/lib/utils/auth.ts +75 -0
  39. package/modules/auth/better-auth/files/nextjs/lib/utils/cookie.ts +29 -0
  40. package/modules/auth/better-auth/files/nextjs/lib/utils/jwt.ts +28 -0
  41. package/modules/auth/better-auth/files/nextjs/lib/utils/token.ts +49 -0
  42. package/modules/auth/better-auth/files/nextjs/pages/forgot-password/page.tsx +5 -0
  43. package/modules/auth/better-auth/files/nextjs/pages/layout.tsx +11 -0
  44. package/modules/auth/better-auth/files/nextjs/pages/login/page.tsx +9 -0
  45. package/modules/auth/better-auth/files/nextjs/pages/register/page.tsx +5 -0
  46. package/modules/auth/better-auth/files/nextjs/pages/reset-password/page.tsx +10 -0
  47. package/modules/auth/better-auth/files/nextjs/pages/verify-email/page.tsx +10 -0
  48. package/modules/auth/better-auth/files/nextjs/proxy.ts +154 -42
  49. package/modules/auth/better-auth/files/nextjs/theme/providers/theme-provider.tsx +11 -0
  50. package/modules/auth/better-auth/files/nextjs/types/api.types.ts +18 -0
  51. package/modules/auth/better-auth/files/react/components/protected-route.tsx +39 -0
  52. package/modules/auth/better-auth/files/react/components/route-guards.tsx +13 -0
  53. package/modules/auth/better-auth/files/react/dashboard/admin/pages/overview.tsx +3 -0
  54. package/modules/auth/better-auth/files/react/dashboard/pages/overview.tsx +3 -0
  55. package/modules/auth/better-auth/files/react/features/pages/forgot-password.tsx +5 -0
  56. package/modules/auth/better-auth/files/react/features/pages/login.tsx +5 -0
  57. package/modules/auth/better-auth/files/react/features/pages/my-profile.tsx +5 -0
  58. package/modules/auth/better-auth/files/react/features/pages/oauth-callback.tsx +59 -0
  59. package/modules/auth/better-auth/files/react/features/pages/register.tsx +5 -0
  60. package/modules/auth/better-auth/files/react/features/pages/reset-password.tsx +10 -0
  61. package/modules/auth/better-auth/files/react/features/pages/verify-email.tsx +10 -0
  62. package/modules/auth/better-auth/files/react/layout/dashboard-layout.tsx +54 -0
  63. package/modules/auth/better-auth/files/react/lib/axios/http.ts +68 -0
  64. package/modules/auth/better-auth/files/react/lib/env.ts +25 -0
  65. package/modules/auth/better-auth/files/react/router.tsx +73 -0
  66. package/modules/auth/better-auth/files/react/theme/components/providers/theme-provider-context.ts +13 -0
  67. package/modules/auth/better-auth/files/react/theme/components/providers/theme-provider.tsx +51 -0
  68. package/modules/auth/better-auth/files/react/theme/hooks/use-theme.ts +8 -0
  69. package/modules/auth/better-auth/files/shared/features/components/change-password-dialog.tsx +113 -0
  70. package/modules/auth/better-auth/files/shared/features/components/forgot-password-form.tsx +84 -0
  71. package/modules/auth/better-auth/files/shared/features/components/login-form.tsx +134 -0
  72. package/modules/auth/better-auth/files/shared/features/components/my-profile.tsx +147 -0
  73. package/modules/auth/better-auth/files/shared/features/components/profile-form.tsx +205 -0
  74. package/modules/auth/better-auth/files/shared/features/components/register-form.tsx +100 -0
  75. package/modules/auth/better-auth/files/shared/features/components/reset-password-form.tsx +111 -0
  76. package/modules/auth/better-auth/files/shared/features/components/social-login-buttons.tsx +47 -0
  77. package/modules/auth/better-auth/files/shared/features/components/user-profile-menu.tsx +106 -0
  78. package/modules/auth/better-auth/files/shared/features/components/verify-email-form.tsx +110 -0
  79. package/modules/auth/better-auth/files/shared/features/queries/auth.mutations.tsx +312 -0
  80. package/modules/auth/better-auth/files/shared/features/queries/auth.querie.ts +19 -0
  81. package/modules/auth/better-auth/files/shared/features/services/auth.api.ts +81 -0
  82. package/modules/auth/better-auth/files/shared/features/types/auth.type.ts +47 -0
  83. package/modules/auth/better-auth/files/shared/features/validators/change-password.validator.ts +18 -0
  84. package/modules/auth/better-auth/files/shared/features/validators/forgot.validator.ts +7 -0
  85. package/modules/auth/better-auth/files/shared/features/validators/login.validator.ts +14 -0
  86. package/modules/auth/better-auth/files/shared/features/validators/profile.validator.ts +8 -0
  87. package/modules/auth/better-auth/files/shared/features/validators/register.validator.ts +9 -0
  88. package/modules/auth/better-auth/files/shared/features/validators/reset.validator.ts +9 -0
  89. package/modules/auth/better-auth/files/shared/features/validators/verify.validator.ts +8 -0
  90. package/modules/auth/better-auth/files/shared/lib/auth-client.ts +2 -1
  91. package/modules/auth/better-auth/files/shared/lib/auth.ts +5 -19
  92. package/modules/auth/better-auth/files/shared/lib/constant/dashboard.ts +90 -0
  93. package/modules/auth/better-auth/files/shared/theme/mode-toggle.tsx +30 -0
  94. package/modules/auth/better-auth/files/shared/ui/shadcn/components/dashboard/dashboard-header.tsx +94 -0
  95. package/modules/auth/better-auth/files/shared/ui/shadcn/components/dashboard/dashboard-sidebar.tsx +255 -0
  96. package/modules/auth/better-auth/files/shared/ui/shadcn/components/footer.tsx +35 -0
  97. package/modules/auth/better-auth/files/shared/ui/shadcn/components/navbar.tsx +145 -0
  98. package/modules/auth/better-auth/files/shared/ui/shadcn/form-field/input-field.tsx +440 -0
  99. package/modules/auth/better-auth/files/shared/utils/email.ts +2 -17
  100. package/modules/auth/better-auth/generator.json +172 -51
  101. package/modules/auth/better-auth/module.json +2 -2
  102. package/modules/components/files/shared/hooks/use-file-upload.ts +412 -0
  103. package/modules/components/files/shared/lib/utils/url-helpers.ts +110 -0
  104. package/modules/components/files/shared/shadcn/dashboard/data-table-column-selector.tsx +52 -0
  105. package/modules/components/files/shared/shadcn/dashboard/data-table-footer.tsx +156 -0
  106. package/modules/components/files/shared/shadcn/dashboard/data-table.tsx +405 -0
  107. package/modules/components/files/shared/shadcn/global/form-field/input-field.tsx +440 -0
  108. package/modules/components/files/shared/shadcn/global/form-field/media-uploader-field.tsx +745 -0
  109. package/modules/components/files/shared/shadcn/global/form-field/multi-select-field.tsx +207 -0
  110. package/modules/components/files/shared/shadcn/global/form-field/select-field.tsx +247 -0
  111. package/modules/components/files/shared/shadcn/global/form-field/textarea-field.tsx +277 -0
  112. package/modules/components/files/shared/shadcn/global/form-field/tiptap-editor-field.tsx +35 -0
  113. package/modules/components/files/shared/shadcn/global/no-results.tsx +41 -0
  114. package/modules/components/files/shared/shadcn/tiptap-editor/editor-menu-bar.tsx +217 -0
  115. package/modules/components/files/shared/shadcn/tiptap-editor/tiptap-editor.tsx +104 -0
  116. package/modules/components/files/shared/url/load-more.tsx +93 -0
  117. package/modules/components/files/shared/url/search-bar.tsx +131 -0
  118. package/modules/components/files/shared/url/sort-select.tsx +118 -0
  119. package/modules/components/files/shared/url/url-tabs.tsx +77 -0
  120. package/modules/components/generator.json +109 -0
  121. package/modules/components/module.json +11 -0
  122. package/modules/database/mongoose/generator.json +3 -14
  123. package/modules/database/mongoose/module.json +2 -2
  124. package/modules/database/prisma/generator.json +6 -12
  125. package/modules/database/prisma/module.json +2 -2
  126. package/modules/storage/cloudinary/files/express/config/env.ts +65 -0
  127. package/modules/storage/cloudinary/files/express/config/media.ts +103 -0
  128. package/modules/storage/cloudinary/files/express/modules/media/media.controller.ts +59 -0
  129. package/modules/storage/cloudinary/files/express/modules/media/media.route.ts +29 -0
  130. package/modules/storage/cloudinary/files/express/modules/media/media.service.ts +113 -0
  131. package/modules/storage/cloudinary/files/express/modules/media/media.type.ts +32 -0
  132. package/modules/storage/cloudinary/generator.json +34 -0
  133. package/modules/storage/cloudinary/module.json +11 -0
  134. package/modules/ui/shadcn/generator.json +21 -0
  135. package/modules/ui/shadcn/module.json +11 -0
  136. package/package.json +24 -26
  137. package/templates/express/README.md +11 -16
  138. package/templates/express/src/config/env.ts +7 -5
  139. package/templates/nextjs/README.md +13 -18
  140. package/templates/nextjs/app/favicon.ico +0 -0
  141. package/templates/nextjs/app/layout.tsx +6 -4
  142. package/templates/nextjs/components/providers/query-provider.tsx +3 -0
  143. package/templates/nextjs/env.example +3 -1
  144. package/templates/nextjs/lib/axios/http.ts +23 -0
  145. package/templates/nextjs/lib/env.ts +7 -5
  146. package/templates/nextjs/package.json +2 -1
  147. package/templates/nextjs/template.json +1 -2
  148. package/templates/react/README.md +9 -14
  149. package/templates/react/index.html +1 -1
  150. package/templates/react/package.json +1 -1
  151. package/templates/react/src/assets/favicon.ico +0 -0
  152. package/templates/react/src/components/providers/query-provider.tsx +38 -0
  153. package/templates/react/src/{shared/components → components}/seo.tsx +4 -8
  154. package/templates/react/src/lib/axios/http.ts +24 -0
  155. package/templates/react/src/main.tsx +8 -11
  156. package/templates/react/src/{features/about/pages → pages}/about.tsx +1 -1
  157. package/templates/react/src/{features/home/pages → pages}/home.tsx +1 -1
  158. package/templates/react/src/router.tsx +6 -6
  159. package/templates/react/src/vite-env.d.ts +2 -1
  160. package/templates/react/template.json +0 -1
  161. package/templates/react/tsconfig.app.json +6 -0
  162. package/templates/react/tsconfig.json +7 -1
  163. package/templates/react/vite.config.ts +12 -0
  164. package/modules/auth/authjs/files/nextjs/api/auth/[...nextauth]/route.ts +0 -3
  165. package/modules/auth/authjs/files/nextjs/proxy.ts +0 -1
  166. package/modules/auth/authjs/files/shared/lib/auth.ts +0 -119
  167. package/modules/auth/authjs/files/shared/prisma/schema.prisma +0 -61
  168. package/modules/auth/authjs/generator.json +0 -64
  169. package/modules/auth/authjs/module.json +0 -13
  170. package/modules/auth/better-auth/files/express/modules/auth/auth.controller.ts +0 -264
  171. package/modules/auth/better-auth/files/express/modules/auth/auth.service.ts +0 -549
  172. package/modules/auth/better-auth/files/express/templates/google-redirect.ejs +0 -24
  173. package/modules/auth/better-auth/files/nextjs/api/auth/[...all]/route.ts +0 -4
  174. package/modules/auth/better-auth/files/nextjs/lib/auth/auth-guards.ts +0 -31
  175. package/modules/auth/better-auth/files/nextjs/templates/email-otp.tsx +0 -74
  176. package/templates/nextjs/lib/api/http.ts +0 -40
  177. package/templates/react/public/vite.svg +0 -1
  178. package/templates/react/src/app/layouts/dashboard-layout.tsx +0 -8
  179. package/templates/react/src/app/layouts/public-layout.tsx +0 -5
  180. package/templates/react/src/app/providers.tsx +0 -20
  181. package/templates/react/src/app/router.tsx +0 -21
  182. package/templates/react/src/assets/react.svg +0 -1
  183. package/templates/react/src/shared/api/http.ts +0 -39
  184. package/templates/react/src/shared/components/loading.tsx +0 -8
  185. package/templates/react/src/shared/lib/query-client.ts +0 -12
  186. package/templates/react/src/utils/storage.ts +0 -35
  187. package/templates/react/src/utils/utils.ts +0 -3
  188. /package/modules/auth/better-auth/files/{shared/mongoose/auth/constants.ts → express/mongo-modules/auth.constants.ts} +0 -0
  189. /package/templates/nextjs/app/{page.tsx → (public)/(root)/page.tsx} +0 -0
  190. /package/templates/react/src/{shared/components → components}/error-boundary.tsx +0 -0
  191. /package/templates/react/src/{shared/components → components}/layout.tsx +0 -0
  192. /package/templates/react/src/{shared/pages → pages}/not-found.tsx +0 -0
@@ -0,0 +1,207 @@
1
+ "use client";
2
+
3
+ import { Badge } from "@/components/ui/badge";
4
+ import { Button } from "@/components/ui/button";
5
+ import { Checkbox } from "@/components/ui/checkbox";
6
+ import {
7
+ Command,
8
+ CommandEmpty,
9
+ CommandGroup,
10
+ CommandInput,
11
+ CommandItem,
12
+ CommandList,
13
+ } from "@/components/ui/command";
14
+ import {
15
+ Field,
16
+ FieldContent,
17
+ FieldError,
18
+ FieldLabel,
19
+ } from "@/components/ui/field";
20
+ import {
21
+ Popover,
22
+ PopoverContent,
23
+ PopoverTrigger,
24
+ } from "@/components/ui/popover";
25
+ import { ScrollArea } from "@/components/ui/scroll-area";
26
+ import clsx from "clsx";
27
+ import { Check, ChevronsUpDown } from "lucide-react";
28
+ import * as React from "react";
29
+ import { Controller } from "react-hook-form";
30
+
31
+ export type MultiOption = { label: string; value: string };
32
+
33
+ type Props = {
34
+ name: string;
35
+ label?: string;
36
+ options: MultiOption[];
37
+ placeholder?: string;
38
+ disabled?: boolean;
39
+ className?: string;
40
+ renderCreate?: React.ReactNode;
41
+ summaryFormatter?: (
42
+ selected: string[],
43
+ all: MultiOption[],
44
+ ) => React.ReactNode;
45
+ listMaxHeight?: number;
46
+ showChips?: boolean;
47
+ };
48
+
49
+ export default function MultiSelectField({
50
+ name,
51
+ label,
52
+ options,
53
+ placeholder = "Select options",
54
+ disabled = false,
55
+ className,
56
+ renderCreate,
57
+ summaryFormatter,
58
+ listMaxHeight = 260,
59
+ showChips = true,
60
+ }: Props) {
61
+ const [open, setOpen] = React.useState(false);
62
+
63
+ return (
64
+ <Controller
65
+ name={name}
66
+ render={({ field, fieldState }) => {
67
+ const value: string[] = Array.isArray(field.value) ? field.value : [];
68
+
69
+ const toggle = (v: string) => {
70
+ const set = new Set(value);
71
+ if (set.has(v)) {
72
+ set.delete(v);
73
+ } else {
74
+ set.add(v);
75
+ }
76
+ field.onChange(Array.from(set));
77
+ };
78
+
79
+ const clearAll = () => field.onChange([]);
80
+ const selectAll = () => field.onChange(options.map((o) => o.value));
81
+
82
+ const allSelected =
83
+ options.length > 0 && value.length === options.length;
84
+
85
+ const defaultSummary = () => {
86
+ if (!value.length)
87
+ return <span className="text-muted-foreground">{placeholder}</span>;
88
+
89
+ const labels = value
90
+ .map((v) => options.find((o) => o.value === v)?.label || v)
91
+ .filter(Boolean);
92
+
93
+ if (!showChips) {
94
+ return (
95
+ <span>
96
+ {labels.length > 2
97
+ ? `${labels.length} selected`
98
+ : labels.join(", ")}
99
+ </span>
100
+ );
101
+ }
102
+ return (
103
+ <div className="flex gap-1">
104
+ {labels.slice(0, 3).map((lab, i) => {
105
+ const val = value[i];
106
+ return (
107
+ <Badge
108
+ key={val}
109
+ variant="secondary"
110
+ className="max-w-[100px] shrink-0 cursor-pointer px-2"
111
+ onClick={(e) => {
112
+ e.stopPropagation();
113
+ field.onChange(value.filter((v) => v !== val));
114
+ }}
115
+ >
116
+ <span className="block truncate" title={lab}>
117
+ {lab}
118
+ </span>
119
+ </Badge>
120
+ );
121
+ })}
122
+ {labels.length > 3 && (
123
+ <Badge variant="outline" className="shrink-0">
124
+ +{labels.length - 3}
125
+ </Badge>
126
+ )}
127
+ </div>
128
+ );
129
+ };
130
+
131
+ return (
132
+ <Field className={clsx("space-y-2", className)} data-invalid={fieldState.invalid}>
133
+ {label && <FieldLabel>{label}</FieldLabel>}
134
+ <FieldContent>
135
+ <Popover open={open} onOpenChange={setOpen}>
136
+ <PopoverTrigger className="overflow-hidden">
137
+ <Button
138
+ type="button"
139
+ variant="outline"
140
+ role="combobox"
141
+ aria-expanded={open}
142
+ className="w-full justify-between"
143
+ disabled={disabled}
144
+ >
145
+ {summaryFormatter
146
+ ? summaryFormatter(value, options)
147
+ : defaultSummary()}
148
+ <ChevronsUpDown className="h-4 w-4 opacity-50" />
149
+ </Button>
150
+ </PopoverTrigger>
151
+
152
+ {/* Use width override here since you can't change the Popover wrapper */}
153
+ <PopoverContent className="w-[--radix-popover-trigger-width] p-0">
154
+ {renderCreate ? (
155
+ <div className="border-b p-2">{renderCreate}</div>
156
+ ) : null}
157
+
158
+ <Command>
159
+ {/* MUST live inside <Command> */}
160
+ <div className="flex items-center gap-2 px-2 pt-2">
161
+ <CommandInput placeholder="Search..." />
162
+ <div className="ml-auto flex items-center gap-1">
163
+ <Button
164
+ type="button"
165
+ variant="ghost"
166
+ size="sm"
167
+ onClick={allSelected ? clearAll : selectAll}
168
+ disabled={!options.length}
169
+ >
170
+ {allSelected ? "Clear all" : "Select all"}
171
+ </Button>
172
+ </div>
173
+ </div>
174
+
175
+ <CommandList>
176
+ <CommandEmpty>No option found.</CommandEmpty>
177
+ <ScrollArea style={{ maxHeight: listMaxHeight }}>
178
+ <CommandGroup>
179
+ {options.map((opt) => {
180
+ const checked = value.includes(opt.value);
181
+ return (
182
+ <CommandItem
183
+ key={opt.value}
184
+ value={`${opt.label} ${opt.value}`}
185
+ className="cursor-pointer"
186
+ onSelect={() => toggle(opt.value)}
187
+ >
188
+ <Checkbox checked={checked} className="mr-2" />
189
+ <span className="flex-1">{opt.label}</span>
190
+ {checked && <Check className="h-4 w-4" />}
191
+ </CommandItem>
192
+ );
193
+ })}
194
+ </CommandGroup>
195
+ </ScrollArea>
196
+ </CommandList>
197
+ </Command>
198
+ </PopoverContent>
199
+ </Popover>
200
+ </FieldContent>
201
+ {fieldState.invalid && <FieldError errors={[fieldState.error]} />}
202
+ </Field>
203
+ );
204
+ }}
205
+ />
206
+ );
207
+ }
@@ -0,0 +1,247 @@
1
+ "use client";
2
+
3
+ import {
4
+ Field,
5
+ FieldContent,
6
+ FieldDescription,
7
+ FieldError,
8
+ FieldLabel,
9
+ } from "@/components/ui/field";
10
+ import {
11
+ Select,
12
+ SelectContent,
13
+ SelectItem,
14
+ SelectTrigger,
15
+ SelectValue,
16
+ } from "@/components/ui/select";
17
+ import { cn } from "@/lib/utils";
18
+ import type { LucideIcon } from "lucide-react";
19
+ import * as React from "react";
20
+ import type { FieldPath, FieldValues } from "react-hook-form";
21
+ import { Controller } from "react-hook-form";
22
+
23
+ /** Types */
24
+ type Option = {
25
+ label: React.ReactNode;
26
+ value: string | number;
27
+ disabled?: boolean;
28
+ icon?: LucideIcon;
29
+ };
30
+
31
+ type BaseProps = {
32
+ id?: string;
33
+ icon?: LucideIcon;
34
+ label?: React.ReactNode;
35
+ options: Option[];
36
+ placeholder?: string;
37
+ description?: React.ReactNode;
38
+ className?: string;
39
+ triggerClassName?: string;
40
+ contentClassName?: string;
41
+ disabled?: boolean;
42
+ required?: boolean;
43
+ allowClear?: boolean;
44
+ clearLabel?: string;
45
+ value?: string | number | undefined;
46
+ onValueChange?: (value: string | number | undefined) => void;
47
+ defaultValue?: string | number | undefined;
48
+ valueAsNumber?: boolean;
49
+ error?: React.ReactNode;
50
+ };
51
+
52
+ export type SelectFieldProps<
53
+ TFieldValues extends FieldValues = FieldValues,
54
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
55
+ > = BaseProps & {
56
+ name?: TName;
57
+ };
58
+
59
+ /** Helpers */
60
+ const CLEAR_TOKEN = "__CLEAR__";
61
+
62
+ function normalizeOut(
63
+ raw: string,
64
+ { valueAsNumber }: { valueAsNumber?: boolean },
65
+ ): string | number | undefined {
66
+ if (raw === CLEAR_TOKEN) return undefined;
67
+ if (!valueAsNumber) return raw;
68
+ const n = Number(raw);
69
+ return Number.isFinite(n) ? n : undefined;
70
+ }
71
+
72
+ function normalizeIn(v: string | number | undefined | null): string {
73
+ if (v === null || v === undefined) return "";
74
+ return String(v);
75
+ }
76
+
77
+ export default function SelectField<
78
+ TFieldValues extends FieldValues,
79
+ TName extends FieldPath<TFieldValues>,
80
+ >({
81
+ name,
82
+ id,
83
+ icon: Icon,
84
+ label,
85
+ options,
86
+ placeholder = "Select an option",
87
+ description,
88
+ className,
89
+ triggerClassName,
90
+ contentClassName,
91
+ disabled = false,
92
+ required = false,
93
+ allowClear = false,
94
+ clearLabel = "Clear",
95
+ valueAsNumber = false,
96
+
97
+ value: externalValue,
98
+ onValueChange,
99
+ defaultValue,
100
+ error,
101
+ }: SelectFieldProps<TFieldValues, TName>) {
102
+ const autoId = React.useId();
103
+ const triggerId = id ?? autoId;
104
+
105
+ const [internal, setInternal] = React.useState<string | number | undefined>(
106
+ defaultValue,
107
+ );
108
+
109
+ // Renderers
110
+ const renderStandalone = () => {
111
+ const current = externalValue !== undefined ? externalValue : internal;
112
+ const selectValue = normalizeIn(current);
113
+
114
+ const handleChange = (raw: string | null) => {
115
+ const next = raw ? normalizeOut(raw, { valueAsNumber }) : undefined;
116
+ if (onValueChange) onValueChange(next);
117
+ else setInternal(next);
118
+ };
119
+
120
+ return (
121
+ <div className={className}>
122
+ {label ? (
123
+ <FieldLabel htmlFor={triggerId} className="mb-2 inline-flex items-center gap-2">
124
+ {Icon && <Icon className="h-4 w-4" />}
125
+ {label}
126
+ {required && <span className="ml-0.5 text-destructive">*</span>}
127
+ </FieldLabel>
128
+ ) : null}
129
+
130
+ <div className="overflow-hidden rounded-md dark:bg-transparent">
131
+ <Select
132
+ value={selectValue}
133
+ onValueChange={handleChange}
134
+ disabled={disabled}
135
+ >
136
+ <SelectTrigger
137
+ id={triggerId}
138
+ className={cn("w-full", triggerClassName)}
139
+ >
140
+ <SelectValue placeholder={placeholder} />
141
+ </SelectTrigger>
142
+
143
+ <SelectContent className={contentClassName}>
144
+ {allowClear && (
145
+ <SelectItem value={CLEAR_TOKEN}>{clearLabel}</SelectItem>
146
+ )}
147
+ {options.map((opt) => {
148
+ const IconComp = opt.icon;
149
+ return (
150
+ <SelectItem
151
+ key={String(opt.value)}
152
+ value={String(opt.value)}
153
+ disabled={opt.disabled}
154
+ >
155
+ <span className="flex items-center gap-2">
156
+ {IconComp && <IconComp className="h-4 w-4" />}
157
+ {opt.label}
158
+ </span>
159
+ </SelectItem>
160
+ );
161
+ })}
162
+ </SelectContent>
163
+ </Select>
164
+ </div>
165
+
166
+ {description ? (
167
+ <FieldDescription>{description}</FieldDescription>
168
+ ) : null}
169
+ {error ? (
170
+ <p className="text-[0.8rem] font-medium text-destructive">{error}</p>
171
+ ) : null}
172
+ </div>
173
+ );
174
+ };
175
+
176
+ const renderFormControlled = () => (
177
+ <Controller
178
+ name={name as TName}
179
+ render={({ field, fieldState }) => {
180
+ const effectiveDisabled = disabled || field.disabled;
181
+ const current =
182
+ externalValue !== undefined
183
+ ? externalValue
184
+ : (field.value as string | number | undefined);
185
+ const selectValue = normalizeIn(current);
186
+
187
+ const handleChange = (raw: string | null) => {
188
+ const next = raw ? normalizeOut(raw, { valueAsNumber }) : undefined;
189
+ field.onChange(next);
190
+ onValueChange?.(next);
191
+ };
192
+
193
+ return (
194
+ <Field className={className} data-invalid={fieldState.invalid}>
195
+ {label ? (
196
+ <FieldLabel className="mb-2 inline-flex items-center gap-2" htmlFor={triggerId}>
197
+ {Icon && <Icon className="h-4 w-4" />}
198
+ {label}
199
+ {required && <span className="ml-0.5 text-destructive">*</span>}
200
+ </FieldLabel>
201
+ ) : null}
202
+
203
+ <FieldContent>
204
+ <div className="overflow-hidden rounded-md dark:bg-transparent">
205
+ <Select
206
+ value={selectValue}
207
+ onValueChange={handleChange}
208
+ disabled={effectiveDisabled}
209
+ >
210
+ <SelectTrigger
211
+ id={triggerId}
212
+ className={cn("w-full", triggerClassName)}
213
+ >
214
+ <SelectValue placeholder={placeholder} />
215
+ </SelectTrigger>
216
+
217
+ <SelectContent className={contentClassName}>
218
+ {allowClear && (
219
+ <SelectItem value={CLEAR_TOKEN}>{clearLabel}</SelectItem>
220
+ )}
221
+ {options.map((opt) => (
222
+ <SelectItem
223
+ key={String(opt.value)}
224
+ value={String(opt.value)}
225
+ disabled={opt.disabled}
226
+ >
227
+ {opt.label}
228
+ </SelectItem>
229
+ ))}
230
+ </SelectContent>
231
+ </Select>
232
+ </div>
233
+ </FieldContent>
234
+
235
+ {description ? (
236
+ <FieldDescription>{description}</FieldDescription>
237
+ ) : null}
238
+ {fieldState.invalid && <FieldError errors={[fieldState.error]} />}
239
+ </Field>
240
+ );
241
+ }}
242
+ />
243
+ );
244
+
245
+ const content = name ? renderFormControlled() : renderStandalone();
246
+ return content;
247
+ }