@skybin-tech/nebula-ui 0.0.1
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/cjs/components/Button/Button.cjs +37 -0
- package/dist/cjs/components/Button/Button.cjs.map +1 -0
- package/dist/cjs/components/Form/Checkbox.cjs +145 -0
- package/dist/cjs/components/Form/Checkbox.cjs.map +1 -0
- package/dist/cjs/components/Form/Form.cjs +93 -0
- package/dist/cjs/components/Form/Form.cjs.map +1 -0
- package/dist/cjs/components/Form/Radio.cjs +178 -0
- package/dist/cjs/components/Form/Radio.cjs.map +1 -0
- package/dist/cjs/components/Form/Select.cjs +168 -0
- package/dist/cjs/components/Form/Select.cjs.map +1 -0
- package/dist/cjs/components/Form/Switch.cjs +125 -0
- package/dist/cjs/components/Form/Switch.cjs.map +1 -0
- package/dist/cjs/components/Form/TextArea.cjs +168 -0
- package/dist/cjs/components/Form/TextArea.cjs.map +1 -0
- package/dist/cjs/components/Form/TextBox.cjs +211 -0
- package/dist/cjs/components/Form/TextBox.cjs.map +1 -0
- package/dist/cjs/components/Form/context.cjs +131 -0
- package/dist/cjs/components/Form/context.cjs.map +1 -0
- package/dist/cjs/components/Form/hooks.cjs +36 -0
- package/dist/cjs/components/Form/hooks.cjs.map +1 -0
- package/dist/cjs/components/Input/Input.cjs +44 -0
- package/dist/cjs/components/Input/Input.cjs.map +1 -0
- package/dist/cjs/components/ui/checkbox.cjs +45 -0
- package/dist/cjs/components/ui/checkbox.cjs.map +1 -0
- package/dist/cjs/components/ui/input.cjs +41 -0
- package/dist/cjs/components/ui/input.cjs.map +1 -0
- package/dist/cjs/components/ui/label.cjs +38 -0
- package/dist/cjs/components/ui/label.cjs.map +1 -0
- package/dist/cjs/components/ui/radio-group.cjs +53 -0
- package/dist/cjs/components/ui/radio-group.cjs.map +1 -0
- package/dist/cjs/components/ui/select.cjs +144 -0
- package/dist/cjs/components/ui/select.cjs.map +1 -0
- package/dist/cjs/components/ui/switch.cjs +45 -0
- package/dist/cjs/components/ui/switch.cjs.map +1 -0
- package/dist/cjs/components/ui/textarea.cjs +38 -0
- package/dist/cjs/components/ui/textarea.cjs.map +1 -0
- package/dist/cjs/hooks/useDebounce.cjs +17 -0
- package/dist/cjs/hooks/useDebounce.cjs.map +1 -0
- package/dist/cjs/hooks/useToggle.cjs +12 -0
- package/dist/cjs/hooks/useToggle.cjs.map +1 -0
- package/dist/cjs/index.cjs +65 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/nebula-ui.css +212 -0
- package/dist/cjs/utils/cn.cjs +9 -0
- package/dist/cjs/utils/cn.cjs.map +1 -0
- package/dist/components/Button/Button.d.ts +18 -0
- package/dist/components/Button/Button.d.ts.map +1 -0
- package/dist/components/Button/Button.js +37 -0
- package/dist/components/Button/Button.js.map +1 -0
- package/dist/components/Button/index.d.ts +3 -0
- package/dist/components/Button/index.d.ts.map +1 -0
- package/dist/components/Form/Checkbox.d.ts +32 -0
- package/dist/components/Form/Checkbox.d.ts.map +1 -0
- package/dist/components/Form/Checkbox.js +145 -0
- package/dist/components/Form/Checkbox.js.map +1 -0
- package/dist/components/Form/Form.d.ts +62 -0
- package/dist/components/Form/Form.d.ts.map +1 -0
- package/dist/components/Form/Form.js +93 -0
- package/dist/components/Form/Form.js.map +1 -0
- package/dist/components/Form/Radio.d.ts +80 -0
- package/dist/components/Form/Radio.d.ts.map +1 -0
- package/dist/components/Form/Radio.js +178 -0
- package/dist/components/Form/Radio.js.map +1 -0
- package/dist/components/Form/Select.d.ts +75 -0
- package/dist/components/Form/Select.d.ts.map +1 -0
- package/dist/components/Form/Select.js +168 -0
- package/dist/components/Form/Select.js.map +1 -0
- package/dist/components/Form/Switch.d.ts +34 -0
- package/dist/components/Form/Switch.d.ts.map +1 -0
- package/dist/components/Form/Switch.js +125 -0
- package/dist/components/Form/Switch.js.map +1 -0
- package/dist/components/Form/TextArea.d.ts +47 -0
- package/dist/components/Form/TextArea.d.ts.map +1 -0
- package/dist/components/Form/TextArea.js +168 -0
- package/dist/components/Form/TextArea.js.map +1 -0
- package/dist/components/Form/TextBox.d.ts +61 -0
- package/dist/components/Form/TextBox.d.ts.map +1 -0
- package/dist/components/Form/TextBox.js +211 -0
- package/dist/components/Form/TextBox.js.map +1 -0
- package/dist/components/Form/context.d.ts +107 -0
- package/dist/components/Form/context.d.ts.map +1 -0
- package/dist/components/Form/context.js +131 -0
- package/dist/components/Form/context.js.map +1 -0
- package/dist/components/Form/hooks.d.ts +21 -0
- package/dist/components/Form/hooks.d.ts.map +1 -0
- package/dist/components/Form/hooks.js +36 -0
- package/dist/components/Form/hooks.js.map +1 -0
- package/dist/components/Form/index.d.ts +18 -0
- package/dist/components/Form/index.d.ts.map +1 -0
- package/dist/components/Input/Input.d.ts +18 -0
- package/dist/components/Input/Input.d.ts.map +1 -0
- package/dist/components/Input/Input.js +44 -0
- package/dist/components/Input/Input.js.map +1 -0
- package/dist/components/Input/index.d.ts +3 -0
- package/dist/components/Input/index.d.ts.map +1 -0
- package/dist/components/ui/checkbox.d.ts +5 -0
- package/dist/components/ui/checkbox.d.ts.map +1 -0
- package/dist/components/ui/checkbox.js +28 -0
- package/dist/components/ui/checkbox.js.map +1 -0
- package/dist/components/ui/index.d.ts +16 -0
- package/dist/components/ui/index.d.ts.map +1 -0
- package/dist/components/ui/input.d.ts +11 -0
- package/dist/components/ui/input.d.ts.map +1 -0
- package/dist/components/ui/input.js +24 -0
- package/dist/components/ui/input.js.map +1 -0
- package/dist/components/ui/label.d.ts +6 -0
- package/dist/components/ui/label.d.ts.map +1 -0
- package/dist/components/ui/label.js +21 -0
- package/dist/components/ui/label.js.map +1 -0
- package/dist/components/ui/radio-group.d.ts +6 -0
- package/dist/components/ui/radio-group.d.ts.map +1 -0
- package/dist/components/ui/radio-group.js +36 -0
- package/dist/components/ui/radio-group.js.map +1 -0
- package/dist/components/ui/select.d.ts +14 -0
- package/dist/components/ui/select.d.ts.map +1 -0
- package/dist/components/ui/select.js +127 -0
- package/dist/components/ui/select.js.map +1 -0
- package/dist/components/ui/switch.d.ts +5 -0
- package/dist/components/ui/switch.d.ts.map +1 -0
- package/dist/components/ui/switch.js +28 -0
- package/dist/components/ui/switch.js.map +1 -0
- package/dist/components/ui/textarea.d.ts +11 -0
- package/dist/components/ui/textarea.d.ts.map +1 -0
- package/dist/components/ui/textarea.js +21 -0
- package/dist/components/ui/textarea.js.map +1 -0
- package/dist/hooks/useDebounce.d.ts +8 -0
- package/dist/hooks/useDebounce.d.ts.map +1 -0
- package/dist/hooks/useDebounce.js +17 -0
- package/dist/hooks/useDebounce.js.map +1 -0
- package/dist/hooks/useToggle.d.ts +7 -0
- package/dist/hooks/useToggle.d.ts.map +1 -0
- package/dist/hooks/useToggle.js +12 -0
- package/dist/hooks/useToggle.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/nebula-ui.css +212 -0
- package/dist/utils/cn.d.ts +7 -0
- package/dist/utils/cn.d.ts.map +1 -0
- package/dist/utils/cn.js +9 -0
- package/dist/utils/cn.js.map +1 -0
- package/package.json +201 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useContext, useEffect, forwardRef, createContext } from "react";
|
|
3
|
+
import { useFormContext, useController } from "react-hook-form";
|
|
4
|
+
import { cva } from "../../node_modules/class-variance-authority/dist/index.js";
|
|
5
|
+
import { cn } from "../../utils/cn.js";
|
|
6
|
+
import { FormConfigContext } from "./context.js";
|
|
7
|
+
import { RadioGroup as RadioGroup$1, RadioGroupItem } from "../ui/radio-group.js";
|
|
8
|
+
import { Label } from "../ui/label.js";
|
|
9
|
+
const radioSizeVariants = cva(
|
|
10
|
+
"",
|
|
11
|
+
{
|
|
12
|
+
variants: {
|
|
13
|
+
size: {
|
|
14
|
+
sm: "h-4 w-4",
|
|
15
|
+
md: "h-5 w-5",
|
|
16
|
+
lg: "h-6 w-6"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
defaultVariants: {
|
|
20
|
+
size: "md"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
const labelVariants = cva(
|
|
25
|
+
"text-sm font-medium cursor-pointer select-none",
|
|
26
|
+
{
|
|
27
|
+
variants: {
|
|
28
|
+
disabled: {
|
|
29
|
+
true: "cursor-not-allowed opacity-50",
|
|
30
|
+
false: ""
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
defaultVariants: {
|
|
34
|
+
disabled: false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
const RadioGroupContext = createContext(null);
|
|
39
|
+
function RadioGroup({
|
|
40
|
+
name,
|
|
41
|
+
label,
|
|
42
|
+
helperText,
|
|
43
|
+
showError = true,
|
|
44
|
+
error: customError,
|
|
45
|
+
size,
|
|
46
|
+
variant,
|
|
47
|
+
options = [],
|
|
48
|
+
direction = "vertical",
|
|
49
|
+
control: externalControl,
|
|
50
|
+
children,
|
|
51
|
+
disabled,
|
|
52
|
+
className,
|
|
53
|
+
// Validation props
|
|
54
|
+
required,
|
|
55
|
+
validate
|
|
56
|
+
}) {
|
|
57
|
+
const generatedId = useId();
|
|
58
|
+
const formConfigContext = useContext(FormConfigContext);
|
|
59
|
+
const formConfig = formConfigContext ?? {};
|
|
60
|
+
const rhfContext = useFormContext();
|
|
61
|
+
const control = externalControl ?? rhfContext?.control;
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (formConfigContext?.registerFieldValidation) {
|
|
64
|
+
const rules = {};
|
|
65
|
+
if (required !== void 0) rules.required = required;
|
|
66
|
+
if (validate !== void 0) rules.validate = validate;
|
|
67
|
+
formConfigContext.registerFieldValidation({
|
|
68
|
+
name,
|
|
69
|
+
type: "string",
|
|
70
|
+
rules
|
|
71
|
+
});
|
|
72
|
+
return () => {
|
|
73
|
+
formConfigContext.unregisterFieldValidation(name);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}, [formConfigContext, name, required, validate]);
|
|
77
|
+
const { field, fieldState } = useController({
|
|
78
|
+
name,
|
|
79
|
+
control
|
|
80
|
+
});
|
|
81
|
+
const fieldError = fieldState.error?.message;
|
|
82
|
+
const errorMessage = customError ?? fieldError;
|
|
83
|
+
const hasError = !!errorMessage;
|
|
84
|
+
const effectiveSize = size ?? formConfig.size ?? "md";
|
|
85
|
+
const effectiveDisabled = disabled ?? formConfig.disabled;
|
|
86
|
+
const effectiveVariant = hasError ? "error" : variant;
|
|
87
|
+
const contextValue = {
|
|
88
|
+
name,
|
|
89
|
+
value: field.value,
|
|
90
|
+
onChange: field.onChange,
|
|
91
|
+
disabled: effectiveDisabled,
|
|
92
|
+
size: effectiveSize
|
|
93
|
+
};
|
|
94
|
+
return /* @__PURE__ */ jsxs("div", { className: cn("space-y-2", className), children: [
|
|
95
|
+
label && /* @__PURE__ */ jsxs(Label, { className: cn(
|
|
96
|
+
"block",
|
|
97
|
+
required && "after:content-['*'] after:ml-0.5 after:text-destructive"
|
|
98
|
+
), children: [
|
|
99
|
+
label,
|
|
100
|
+
formConfig.colon && ":"
|
|
101
|
+
] }),
|
|
102
|
+
/* @__PURE__ */ jsx(RadioGroupContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(
|
|
103
|
+
RadioGroup$1,
|
|
104
|
+
{
|
|
105
|
+
value: field.value?.toString(),
|
|
106
|
+
onValueChange: (value) => field.onChange(value),
|
|
107
|
+
disabled: effectiveDisabled,
|
|
108
|
+
"aria-labelledby": label ? `${generatedId}-label` : void 0,
|
|
109
|
+
className: cn(
|
|
110
|
+
direction === "horizontal" && "flex flex-row gap-4",
|
|
111
|
+
direction === "vertical" && "flex flex-col gap-2"
|
|
112
|
+
),
|
|
113
|
+
children: children ?? options.map((option) => /* @__PURE__ */ jsx(
|
|
114
|
+
RadioItem,
|
|
115
|
+
{
|
|
116
|
+
value: option.value,
|
|
117
|
+
disabled: option.disabled,
|
|
118
|
+
size: effectiveSize,
|
|
119
|
+
variant: effectiveVariant,
|
|
120
|
+
children: option.label
|
|
121
|
+
},
|
|
122
|
+
option.value
|
|
123
|
+
))
|
|
124
|
+
}
|
|
125
|
+
) }),
|
|
126
|
+
showError && hasError && /* @__PURE__ */ jsx("p", { className: "text-sm text-destructive", role: "alert", children: errorMessage }),
|
|
127
|
+
helperText && !hasError && /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: helperText })
|
|
128
|
+
] });
|
|
129
|
+
}
|
|
130
|
+
RadioGroup.displayName = "RadioGroup";
|
|
131
|
+
function RadioItemInner({
|
|
132
|
+
value,
|
|
133
|
+
children,
|
|
134
|
+
size,
|
|
135
|
+
variant,
|
|
136
|
+
className,
|
|
137
|
+
disabled,
|
|
138
|
+
id: providedId,
|
|
139
|
+
...props
|
|
140
|
+
}, ref) {
|
|
141
|
+
const generatedId = useId();
|
|
142
|
+
const inputId = providedId ?? generatedId;
|
|
143
|
+
const groupContext = useContext(RadioGroupContext);
|
|
144
|
+
const effectiveSize = size ?? groupContext?.size ?? "md";
|
|
145
|
+
const effectiveDisabled = disabled ?? groupContext?.disabled;
|
|
146
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
147
|
+
/* @__PURE__ */ jsx(
|
|
148
|
+
RadioGroupItem,
|
|
149
|
+
{
|
|
150
|
+
...props,
|
|
151
|
+
ref,
|
|
152
|
+
id: inputId,
|
|
153
|
+
value: value.toString(),
|
|
154
|
+
disabled: effectiveDisabled,
|
|
155
|
+
className: cn(
|
|
156
|
+
radioSizeVariants({ size: effectiveSize }),
|
|
157
|
+
variant === "error" && "border-destructive",
|
|
158
|
+
className
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
),
|
|
162
|
+
children && /* @__PURE__ */ jsx(
|
|
163
|
+
Label,
|
|
164
|
+
{
|
|
165
|
+
htmlFor: inputId,
|
|
166
|
+
className: labelVariants({ disabled: effectiveDisabled }),
|
|
167
|
+
children
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
] });
|
|
171
|
+
}
|
|
172
|
+
const RadioItem = forwardRef(RadioItemInner);
|
|
173
|
+
RadioItem.displayName = "RadioItem";
|
|
174
|
+
export {
|
|
175
|
+
RadioGroup,
|
|
176
|
+
RadioItem
|
|
177
|
+
};
|
|
178
|
+
//# sourceMappingURL=Radio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Radio.js","sources":["../../../src/components/Form/Radio.tsx"],"sourcesContent":["import { forwardRef, useId, useContext, createContext, useEffect } from \"react\";\r\nimport type { ReactNode, ComponentPropsWithoutRef } from \"react\";\r\nimport { useController, useFormContext as useRHFFormContext, type FieldValues, type FieldPath, type Control } from \"react-hook-form\";\r\nimport { cva, type VariantProps } from \"class-variance-authority\";\r\nimport { cn } from \"../../utils/cn\";\r\nimport { FormConfigContext, type FormConfig, type FieldValidationRules } from \"../Form/context\";\r\nimport { RadioGroup as ShadcnRadioGroup, RadioGroupItem as ShadcnRadioGroupItem } from \"../ui/radio-group\";\r\nimport { Label } from \"../ui/label\";\r\nimport type * as RadioGroupPrimitive from \"@radix-ui/react-radio-group\";\r\n\r\nconst radioSizeVariants = cva(\r\n \"\",\r\n {\r\n variants: {\r\n size: {\r\n sm: \"h-4 w-4\",\r\n md: \"h-5 w-5\",\r\n lg: \"h-6 w-6\",\r\n },\r\n },\r\n defaultVariants: {\r\n size: \"md\",\r\n },\r\n }\r\n);\r\n\r\nconst labelVariants = cva(\r\n \"text-sm font-medium cursor-pointer select-none\",\r\n {\r\n variants: {\r\n disabled: {\r\n true: \"cursor-not-allowed opacity-50\",\r\n false: \"\",\r\n },\r\n },\r\n defaultVariants: {\r\n disabled: false,\r\n },\r\n }\r\n);\r\n\r\n// Radio Group Context\r\ninterface RadioGroupContextValue {\r\n name: string;\r\n value: string | number | undefined;\r\n onChange: (value: string | number) => void;\r\n disabled?: boolean;\r\n size?: \"sm\" | \"md\" | \"lg\";\r\n}\r\n\r\nconst RadioGroupContext = createContext<RadioGroupContextValue | null>(null);\r\n\r\nexport interface RadioOption {\r\n label: ReactNode;\r\n value: string | number;\r\n disabled?: boolean;\r\n}\r\n\r\nexport interface RadioGroupProps<\r\n TFieldValues extends FieldValues = FieldValues,\r\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>\r\n> extends VariantProps<typeof radioSizeVariants> {\r\n /** Field name - required for form integration */\r\n name: TName;\r\n /** Label text for the radio group */\r\n label?: string;\r\n /** Helper text displayed below the radio group */\r\n helperText?: string;\r\n /** Whether to show the error message */\r\n showError?: boolean;\r\n /** Custom error message (overrides form error) */\r\n error?: string;\r\n /** Options for the radio group */\r\n options?: RadioOption[];\r\n /** Layout direction */\r\n direction?: \"horizontal\" | \"vertical\";\r\n /** External control (for use outside Form) */\r\n control?: Control<TFieldValues>;\r\n /** Children (alternative to options prop) */\r\n children?: ReactNode;\r\n /** Disabled state */\r\n disabled?: boolean;\r\n /** Additional class name */\r\n className?: string;\r\n /** Variant style */\r\n variant?: \"default\" | \"error\";\r\n \r\n // Validation props\r\n /** Field is required */\r\n required?: boolean | string;\r\n /** Custom validation function */\r\n validate?: (value: unknown) => boolean | string | Promise<boolean | string>;\r\n}\r\n\r\n/**\r\n * RadioGroup component with form integration\r\n * \r\n * This is a wrapper around the shadcn/ui RadioGroup primitive that adds:\r\n * - Form integration with react-hook-form\r\n * - Automatic validation registration\r\n * - Label, helper text, and error message support\r\n * \r\n * @example\r\n * ```tsx\r\n * // Inside a Form component\r\n * <RadioGroup \r\n * name=\"gender\" \r\n * label=\"Gender\"\r\n * required\r\n * options={[\r\n * { label: \"Male\", value: \"male\" },\r\n * { label: \"Female\", value: \"female\" },\r\n * { label: \"Other\", value: \"other\" },\r\n * ]} \r\n * />\r\n * ```\r\n */\r\nexport function RadioGroup<\r\n TFieldValues extends FieldValues = FieldValues,\r\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>\r\n>({\r\n name,\r\n label,\r\n helperText,\r\n showError = true,\r\n error: customError,\r\n size,\r\n variant,\r\n options = [],\r\n direction = \"vertical\",\r\n control: externalControl,\r\n children,\r\n disabled,\r\n className,\r\n // Validation props\r\n required,\r\n validate,\r\n}: RadioGroupProps<TFieldValues, TName>) {\r\n const generatedId = useId();\r\n \r\n // Try to get form context\r\n const formConfigContext = useContext(FormConfigContext);\r\n const formConfig: FormConfig = formConfigContext ?? {};\r\n \r\n // Get form context from react-hook-form\r\n const rhfContext = useRHFFormContext<TFieldValues>();\r\n const control = externalControl ?? rhfContext?.control;\r\n\r\n // Register validation rules with the form\r\n useEffect(() => {\r\n if (formConfigContext?.registerFieldValidation) {\r\n const rules: FieldValidationRules = {};\r\n \r\n if (required !== undefined) rules.required = required;\r\n if (validate !== undefined) rules.validate = validate;\r\n\r\n formConfigContext.registerFieldValidation({\r\n name: name as string,\r\n type: \"string\",\r\n rules,\r\n });\r\n\r\n return () => {\r\n formConfigContext.unregisterFieldValidation(name as string);\r\n };\r\n }\r\n }, [formConfigContext, name, required, validate]);\r\n\r\n // Use controller for form integration\r\n const { field, fieldState } = useController<TFieldValues, TName>({\r\n name,\r\n control,\r\n });\r\n\r\n const fieldError = fieldState.error?.message;\r\n const errorMessage = customError ?? fieldError;\r\n const hasError = !!errorMessage;\r\n \r\n // Merge sizes - prop takes precedence over form config\r\n const effectiveSize = size ?? formConfig.size ?? \"md\";\r\n const effectiveDisabled = disabled ?? formConfig.disabled;\r\n const effectiveVariant = hasError ? \"error\" : variant;\r\n\r\n const contextValue: RadioGroupContextValue = {\r\n name: name as string,\r\n value: field.value,\r\n onChange: field.onChange,\r\n disabled: effectiveDisabled,\r\n size: effectiveSize,\r\n };\r\n\r\n return (\r\n <div className={cn(\"space-y-2\", className)}>\r\n {label && (\r\n <Label className={cn(\r\n \"block\",\r\n required && \"after:content-['*'] after:ml-0.5 after:text-destructive\"\r\n )}>\r\n {label}\r\n {formConfig.colon && \":\"}\r\n </Label>\r\n )}\r\n \r\n <RadioGroupContext.Provider value={contextValue}>\r\n <ShadcnRadioGroup\r\n value={field.value?.toString()}\r\n onValueChange={(value) => field.onChange(value)}\r\n disabled={effectiveDisabled}\r\n aria-labelledby={label ? `${generatedId}-label` : undefined}\r\n className={cn(\r\n direction === \"horizontal\" && \"flex flex-row gap-4\",\r\n direction === \"vertical\" && \"flex flex-col gap-2\"\r\n )}\r\n >\r\n {children ?? options.map((option) => (\r\n <RadioItem\r\n key={option.value}\r\n value={option.value}\r\n disabled={option.disabled}\r\n size={effectiveSize}\r\n variant={effectiveVariant}\r\n >\r\n {option.label}\r\n </RadioItem>\r\n ))}\r\n </ShadcnRadioGroup>\r\n </RadioGroupContext.Provider>\r\n\r\n {showError && hasError && (\r\n <p className=\"text-sm text-destructive\" role=\"alert\">\r\n {errorMessage}\r\n </p>\r\n )}\r\n \r\n {helperText && !hasError && (\r\n <p className=\"text-sm text-muted-foreground\">\r\n {helperText}\r\n </p>\r\n )}\r\n </div>\r\n );\r\n}\r\n\r\nRadioGroup.displayName = \"RadioGroup\";\r\n\r\nexport interface RadioItemProps extends Omit<ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>, \"value\">,\r\n VariantProps<typeof radioSizeVariants> {\r\n /** Value for this radio option */\r\n value: string | number;\r\n /** Label for this radio option */\r\n children?: ReactNode;\r\n /** Variant style */\r\n variant?: \"default\" | \"error\";\r\n}\r\n\r\n/**\r\n * RadioItem component - must be used within RadioGroup\r\n * \r\n * This is a wrapper around the shadcn/ui RadioGroupItem primitive\r\n */\r\nfunction RadioItemInner(\r\n {\r\n value,\r\n children,\r\n size,\r\n variant,\r\n className,\r\n disabled,\r\n id: providedId,\r\n ...props\r\n }: RadioItemProps,\r\n ref: React.ForwardedRef<React.ElementRef<typeof RadioGroupPrimitive.Item>>\r\n) {\r\n const generatedId = useId();\r\n const inputId = providedId ?? generatedId;\r\n \r\n const groupContext = useContext(RadioGroupContext);\r\n \r\n const effectiveSize = size ?? groupContext?.size ?? \"md\";\r\n const effectiveDisabled = disabled ?? groupContext?.disabled;\r\n\r\n return (\r\n <div className=\"flex items-center gap-2\">\r\n <ShadcnRadioGroupItem\r\n {...props}\r\n ref={ref}\r\n id={inputId}\r\n value={value.toString()}\r\n disabled={effectiveDisabled}\r\n className={cn(\r\n radioSizeVariants({ size: effectiveSize }),\r\n variant === \"error\" && \"border-destructive\",\r\n className\r\n )}\r\n />\r\n \r\n {children && (\r\n <Label\r\n htmlFor={inputId}\r\n className={labelVariants({ disabled: effectiveDisabled })}\r\n >\r\n {children}\r\n </Label>\r\n )}\r\n </div>\r\n );\r\n}\r\n\r\nexport const RadioItem = forwardRef(RadioItemInner);\r\nRadioItem.displayName = \"RadioItem\";\r\n"],"names":["useRHFFormContext","ShadcnRadioGroup","ShadcnRadioGroupItem"],"mappings":";;;;;;;;AAUA,MAAM,oBAAoB;AAAA,EACxB;AAAA,EACA;AAAA,IACE,UAAU;AAAA,MACR,MAAM;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,MAAA;AAAA,IACN;AAAA,IAEF,iBAAiB;AAAA,MACf,MAAM;AAAA,IAAA;AAAA,EACR;AAEJ;AAEA,MAAM,gBAAgB;AAAA,EACpB;AAAA,EACA;AAAA,IACE,UAAU;AAAA,MACR,UAAU;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,MAAA;AAAA,IACT;AAAA,IAEF,iBAAiB;AAAA,MACf,UAAU;AAAA,IAAA;AAAA,EACZ;AAEJ;AAWA,MAAM,oBAAoB,cAA6C,IAAI;AAmEpE,SAAS,WAGd;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,OAAO;AAAA,EACP;AAAA,EACA;AAAA,EACA,UAAU,CAAA;AAAA,EACV,YAAY;AAAA,EACZ,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAEA;AAAA,EACA;AACF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AA,MACF;AAAA,IAAA;AAAA,IAGD,YACC;AAAA,MAAC;AAAA,MAAA;AAAA,QACC,SAAS;AAAA,QACT,WAAW,cAAc,EAAE,UAAU,mBAAmB;AAAA,QAEvD;AAAA,MAAA;AAAA,IAAA;AAAA,EACH,GAEJ;AAEJ;AAEO,MAAM,YAAY,WAAW,cAAc;AAClD,UAAU,cAAc;"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { FieldValues, FieldPath, Control } from 'react-hook-form';
|
|
3
|
+
import { VariantProps } from 'class-variance-authority';
|
|
4
|
+
declare const selectSizeVariants: (props?: ({
|
|
5
|
+
size?: "sm" | "md" | "lg" | null | undefined;
|
|
6
|
+
} & import('class-variance-authority/types').ClassProp) | undefined) => string;
|
|
7
|
+
export interface SelectOption {
|
|
8
|
+
label: string;
|
|
9
|
+
value: string | number;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface SelectProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends VariantProps<typeof selectSizeVariants> {
|
|
13
|
+
/** Field name - required for form integration */
|
|
14
|
+
name: TName;
|
|
15
|
+
/** Label text for the select */
|
|
16
|
+
label?: string;
|
|
17
|
+
/** Helper text displayed below the select */
|
|
18
|
+
helperText?: string;
|
|
19
|
+
/** Whether to show the error message */
|
|
20
|
+
showError?: boolean;
|
|
21
|
+
/** Custom error message (overrides form error) */
|
|
22
|
+
error?: string;
|
|
23
|
+
/** Whether the select should take full width */
|
|
24
|
+
fullWidth?: boolean;
|
|
25
|
+
/** Options for the select */
|
|
26
|
+
options?: SelectOption[];
|
|
27
|
+
/** Placeholder text */
|
|
28
|
+
placeholder?: string;
|
|
29
|
+
/** Allow clear selection */
|
|
30
|
+
allowClear?: boolean;
|
|
31
|
+
/** External control (for use outside Form) */
|
|
32
|
+
control?: Control<TFieldValues>;
|
|
33
|
+
/** Children (alternative to options prop) */
|
|
34
|
+
children?: ReactNode;
|
|
35
|
+
/** Disabled state */
|
|
36
|
+
disabled?: boolean;
|
|
37
|
+
/** Additional class name */
|
|
38
|
+
className?: string;
|
|
39
|
+
/** ID for the select */
|
|
40
|
+
id?: string;
|
|
41
|
+
/** Variant style */
|
|
42
|
+
variant?: "default" | "error" | "success";
|
|
43
|
+
/** Field is required */
|
|
44
|
+
required?: boolean | string;
|
|
45
|
+
/** Custom validation function */
|
|
46
|
+
validate?: (value: unknown) => boolean | string | Promise<boolean | string>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Select component with form integration
|
|
50
|
+
*
|
|
51
|
+
* This is a wrapper around the shadcn/ui Select primitive that adds:
|
|
52
|
+
* - Form integration with react-hook-form
|
|
53
|
+
* - Automatic validation registration
|
|
54
|
+
* - Label, helper text, and error message support
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* // Inside a Form component
|
|
59
|
+
* <Select
|
|
60
|
+
* name="country"
|
|
61
|
+
* label="Country"
|
|
62
|
+
* required
|
|
63
|
+
* options={[
|
|
64
|
+
* { label: "USA", value: "us" },
|
|
65
|
+
* { label: "Canada", value: "ca" },
|
|
66
|
+
* ]}
|
|
67
|
+
* />
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function Select<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ name, label, helperText, showError, error: customError, size, variant, fullWidth, className, disabled, options, placeholder, allowClear, id: providedId, control: externalControl, children, required, validate, }: SelectProps<TFieldValues, TName>): import("react/jsx-runtime").JSX.Element;
|
|
71
|
+
export declare namespace Select {
|
|
72
|
+
var displayName: string;
|
|
73
|
+
}
|
|
74
|
+
export {};
|
|
75
|
+
//# sourceMappingURL=Select.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Select.d.ts","sourceRoot":"","sources":["../../../src/components/Form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AA4B;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wBAAwB;IACxB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,oBAAoB;IACpB,OAAO,CAAC,EAAE,SAAS,GAAG,OAAO,GAAG,SAAS,CAAC;IAG1C,wBAAwB;IACxB,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,iCAAiC;IACjC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;CAC7E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CACpB,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,KAAK,SAAS,SAAS,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC,YAAY,CAAC,EAC/D,EACA,IAAI,EACJ,KAAK,EACL,UAAU,EACV,SAAgB,EAChB,KAAK,EAAE,WAAW,EAClB,IAAI,EACJ,OAAO,EACP,SAAgB,EAChB,SAAS,EACT,QAAQ,EACR,OAAY,EACZ,WAAW,EACX,UAAU,EACV,EAAE,EAAE,UAAU,EACd,OAAO,EAAE,eAAe,EACxB,QAAQ,EAER,QAAQ,EACR,QAAQ,GACT,EAAE,WAAW,CAAC,YAAY,EAAE,KAAK,CAAC,2CAkIlC;yBAzJe,MAAM"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useContext, useEffect } from "react";
|
|
3
|
+
import { useFormContext, useController } from "react-hook-form";
|
|
4
|
+
import { cva } from "../../node_modules/class-variance-authority/dist/index.js";
|
|
5
|
+
import { cn } from "../../utils/cn.js";
|
|
6
|
+
import { FormConfigContext } from "./context.js";
|
|
7
|
+
import { Select as Select$1, SelectTrigger, SelectValue, SelectContent, SelectItem } from "../ui/select.js";
|
|
8
|
+
import { Label } from "../ui/label.js";
|
|
9
|
+
const selectSizeVariants = cva(
|
|
10
|
+
"",
|
|
11
|
+
{
|
|
12
|
+
variants: {
|
|
13
|
+
size: {
|
|
14
|
+
sm: "h-8 text-xs",
|
|
15
|
+
md: "h-10 text-sm",
|
|
16
|
+
lg: "h-12 text-base"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
defaultVariants: {
|
|
20
|
+
size: "md"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
const labelVariants = cva(
|
|
25
|
+
"block text-sm font-medium mb-1.5",
|
|
26
|
+
{
|
|
27
|
+
variants: {
|
|
28
|
+
required: {
|
|
29
|
+
true: "after:content-['*'] after:ml-0.5 after:text-destructive",
|
|
30
|
+
false: ""
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
defaultVariants: {
|
|
34
|
+
required: false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
function Select({
|
|
39
|
+
name,
|
|
40
|
+
label,
|
|
41
|
+
helperText,
|
|
42
|
+
showError = true,
|
|
43
|
+
error: customError,
|
|
44
|
+
size,
|
|
45
|
+
variant,
|
|
46
|
+
fullWidth = true,
|
|
47
|
+
className,
|
|
48
|
+
disabled,
|
|
49
|
+
options = [],
|
|
50
|
+
placeholder,
|
|
51
|
+
allowClear,
|
|
52
|
+
id: providedId,
|
|
53
|
+
control: externalControl,
|
|
54
|
+
children,
|
|
55
|
+
// Validation props
|
|
56
|
+
required,
|
|
57
|
+
validate
|
|
58
|
+
}) {
|
|
59
|
+
const generatedId = useId();
|
|
60
|
+
const inputId = providedId ?? generatedId;
|
|
61
|
+
const formConfigContext = useContext(FormConfigContext);
|
|
62
|
+
const formConfig = formConfigContext ?? {};
|
|
63
|
+
const rhfContext = useFormContext();
|
|
64
|
+
const control = externalControl ?? rhfContext?.control;
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (formConfigContext?.registerFieldValidation) {
|
|
67
|
+
const rules = {};
|
|
68
|
+
if (required !== void 0) rules.required = required;
|
|
69
|
+
if (validate !== void 0) rules.validate = validate;
|
|
70
|
+
formConfigContext.registerFieldValidation({
|
|
71
|
+
name,
|
|
72
|
+
type: "string",
|
|
73
|
+
rules
|
|
74
|
+
});
|
|
75
|
+
return () => {
|
|
76
|
+
formConfigContext.unregisterFieldValidation(name);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}, [formConfigContext, name, required, validate]);
|
|
80
|
+
const { field, fieldState } = useController({
|
|
81
|
+
name,
|
|
82
|
+
control
|
|
83
|
+
});
|
|
84
|
+
const fieldError = fieldState.error?.message;
|
|
85
|
+
const errorMessage = customError ?? fieldError;
|
|
86
|
+
const hasError = !!errorMessage;
|
|
87
|
+
const effectiveSize = size ?? formConfig.size ?? "md";
|
|
88
|
+
const effectiveDisabled = disabled ?? formConfig.disabled;
|
|
89
|
+
const effectiveVariant = hasError ? "error" : variant;
|
|
90
|
+
return /* @__PURE__ */ jsxs("div", { className: cn("space-y-1.5", fullWidth && "w-full"), children: [
|
|
91
|
+
label && /* @__PURE__ */ jsxs(
|
|
92
|
+
Label,
|
|
93
|
+
{
|
|
94
|
+
htmlFor: inputId,
|
|
95
|
+
className: labelVariants({ required: !!required }),
|
|
96
|
+
children: [
|
|
97
|
+
label,
|
|
98
|
+
formConfig.colon && ":"
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
),
|
|
102
|
+
/* @__PURE__ */ jsxs(
|
|
103
|
+
Select$1,
|
|
104
|
+
{
|
|
105
|
+
value: field.value?.toString() ?? "",
|
|
106
|
+
onValueChange: (value) => {
|
|
107
|
+
if (value === "" && allowClear) {
|
|
108
|
+
field.onChange("");
|
|
109
|
+
} else {
|
|
110
|
+
field.onChange(value);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
disabled: effectiveDisabled,
|
|
114
|
+
children: [
|
|
115
|
+
/* @__PURE__ */ jsx(
|
|
116
|
+
SelectTrigger,
|
|
117
|
+
{
|
|
118
|
+
id: inputId,
|
|
119
|
+
"aria-invalid": hasError,
|
|
120
|
+
"aria-describedby": hasError ? `${inputId}-error` : helperText ? `${inputId}-helper` : void 0,
|
|
121
|
+
className: cn(
|
|
122
|
+
selectSizeVariants({ size: effectiveSize }),
|
|
123
|
+
effectiveVariant === "error" && "border-destructive focus:ring-destructive",
|
|
124
|
+
effectiveVariant === "success" && "border-green-500 focus:ring-green-500",
|
|
125
|
+
className
|
|
126
|
+
),
|
|
127
|
+
children: /* @__PURE__ */ jsx(SelectValue, { placeholder })
|
|
128
|
+
}
|
|
129
|
+
),
|
|
130
|
+
/* @__PURE__ */ jsxs(SelectContent, { children: [
|
|
131
|
+
allowClear && field.value && /* @__PURE__ */ jsx(SelectItem, { value: "", children: /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "Clear selection" }) }),
|
|
132
|
+
children ?? options.map((option) => /* @__PURE__ */ jsx(
|
|
133
|
+
SelectItem,
|
|
134
|
+
{
|
|
135
|
+
value: option.value.toString(),
|
|
136
|
+
disabled: option.disabled,
|
|
137
|
+
children: option.label
|
|
138
|
+
},
|
|
139
|
+
option.value
|
|
140
|
+
))
|
|
141
|
+
] })
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
),
|
|
145
|
+
showError && hasError && /* @__PURE__ */ jsx(
|
|
146
|
+
"p",
|
|
147
|
+
{
|
|
148
|
+
id: `${inputId}-error`,
|
|
149
|
+
className: "text-sm text-destructive",
|
|
150
|
+
role: "alert",
|
|
151
|
+
children: errorMessage
|
|
152
|
+
}
|
|
153
|
+
),
|
|
154
|
+
helperText && !hasError && /* @__PURE__ */ jsx(
|
|
155
|
+
"p",
|
|
156
|
+
{
|
|
157
|
+
id: `${inputId}-helper`,
|
|
158
|
+
className: "text-sm text-muted-foreground",
|
|
159
|
+
children: helperText
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
] });
|
|
163
|
+
}
|
|
164
|
+
Select.displayName = "Select";
|
|
165
|
+
export {
|
|
166
|
+
Select
|
|
167
|
+
};
|
|
168
|
+
//# sourceMappingURL=Select.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Select.js","sources":["../../../src/components/Form/Select.tsx"],"sourcesContent":["import { useId, useContext, useEffect } from \"react\";\r\nimport type { ReactNode } from \"react\";\r\nimport { useController, useFormContext as useRHFFormContext, type FieldValues, type FieldPath, type Control } from \"react-hook-form\";\r\nimport { cva, type VariantProps } from \"class-variance-authority\";\r\nimport { cn } from \"../../utils/cn\";\r\nimport { FormConfigContext, type FormConfig, type FieldValidationRules } from \"../Form/context\";\r\nimport {\r\n Select as ShadcnSelect,\r\n SelectContent,\r\n SelectItem,\r\n SelectTrigger,\r\n SelectValue,\r\n} from \"../ui/select\";\r\nimport { Label } from \"../ui/label\";\r\n\r\nconst selectSizeVariants = cva(\r\n \"\",\r\n {\r\n variants: {\r\n size: {\r\n sm: \"h-8 text-xs\",\r\n md: \"h-10 text-sm\",\r\n lg: \"h-12 text-base\",\r\n },\r\n },\r\n defaultVariants: {\r\n size: \"md\",\r\n },\r\n }\r\n);\r\n\r\nconst labelVariants = cva(\r\n \"block text-sm font-medium mb-1.5\",\r\n {\r\n variants: {\r\n required: {\r\n true: \"after:content-['*'] after:ml-0.5 after:text-destructive\",\r\n false: \"\",\r\n },\r\n },\r\n defaultVariants: {\r\n required: false,\r\n },\r\n }\r\n);\r\n\r\nexport interface SelectOption {\r\n label: string;\r\n value: string | number;\r\n disabled?: boolean;\r\n}\r\n\r\nexport interface SelectProps<\r\n TFieldValues extends FieldValues = FieldValues,\r\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>\r\n> extends VariantProps<typeof selectSizeVariants> {\r\n /** Field name - required for form integration */\r\n name: TName;\r\n /** Label text for the select */\r\n label?: string;\r\n /** Helper text displayed below the select */\r\n helperText?: string;\r\n /** Whether to show the error message */\r\n showError?: boolean;\r\n /** Custom error message (overrides form error) */\r\n error?: string;\r\n /** Whether the select should take full width */\r\n fullWidth?: boolean;\r\n /** Options for the select */\r\n options?: SelectOption[];\r\n /** Placeholder text */\r\n placeholder?: string;\r\n /** Allow clear selection */\r\n allowClear?: boolean;\r\n /** External control (for use outside Form) */\r\n control?: Control<TFieldValues>;\r\n /** Children (alternative to options prop) */\r\n children?: ReactNode;\r\n /** Disabled state */\r\n disabled?: boolean;\r\n /** Additional class name */\r\n className?: string;\r\n /** ID for the select */\r\n id?: string;\r\n /** Variant style */\r\n variant?: \"default\" | \"error\" | \"success\";\r\n \r\n // Validation props\r\n /** Field is required */\r\n required?: boolean | string;\r\n /** Custom validation function */\r\n validate?: (value: unknown) => boolean | string | Promise<boolean | string>;\r\n}\r\n\r\n/**\r\n * Select component with form integration\r\n * \r\n * This is a wrapper around the shadcn/ui Select primitive that adds:\r\n * - Form integration with react-hook-form\r\n * - Automatic validation registration\r\n * - Label, helper text, and error message support\r\n * \r\n * @example\r\n * ```tsx\r\n * // Inside a Form component\r\n * <Select \r\n * name=\"country\" \r\n * label=\"Country\" \r\n * required\r\n * options={[\r\n * { label: \"USA\", value: \"us\" },\r\n * { label: \"Canada\", value: \"ca\" },\r\n * ]} \r\n * />\r\n * ```\r\n */\r\nexport function Select<\r\n TFieldValues extends FieldValues = FieldValues,\r\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>\r\n>({\r\n name,\r\n label,\r\n helperText,\r\n showError = true,\r\n error: customError,\r\n size,\r\n variant,\r\n fullWidth = true,\r\n className,\r\n disabled,\r\n options = [],\r\n placeholder,\r\n allowClear,\r\n id: providedId,\r\n control: externalControl,\r\n children,\r\n // Validation props\r\n required,\r\n validate,\r\n}: SelectProps<TFieldValues, TName>) {\r\n const generatedId = useId();\r\n const inputId = providedId ?? generatedId;\r\n \r\n // Try to get form context\r\n const formConfigContext = useContext(FormConfigContext);\r\n const formConfig: FormConfig = formConfigContext ?? {};\r\n \r\n // Get form context from react-hook-form\r\n const rhfContext = useRHFFormContext<TFieldValues>();\r\n const control = externalControl ?? rhfContext?.control;\r\n\r\n // Register validation rules with the form\r\n useEffect(() => {\r\n if (formConfigContext?.registerFieldValidation) {\r\n const rules: FieldValidationRules = {};\r\n \r\n if (required !== undefined) rules.required = required;\r\n if (validate !== undefined) rules.validate = validate;\r\n\r\n formConfigContext.registerFieldValidation({\r\n name: name as string,\r\n type: \"string\",\r\n rules,\r\n });\r\n\r\n return () => {\r\n formConfigContext.unregisterFieldValidation(name as string);\r\n };\r\n }\r\n }, [formConfigContext, name, required, validate]);\r\n\r\n // Use controller for form integration\r\n const { field, fieldState } = useController<TFieldValues, TName>({\r\n name,\r\n control,\r\n });\r\n\r\n const fieldError = fieldState.error?.message;\r\n const errorMessage = customError ?? fieldError;\r\n const hasError = !!errorMessage;\r\n \r\n // Merge sizes - prop takes precedence over form config\r\n const effectiveSize = size ?? formConfig.size ?? \"md\";\r\n const effectiveDisabled = disabled ?? formConfig.disabled;\r\n \r\n // Determine variant based on error state\r\n const effectiveVariant = hasError ? \"error\" : variant;\r\n\r\n return (\r\n <div className={cn(\"space-y-1.5\", fullWidth && \"w-full\")}>\r\n {label && (\r\n <Label\r\n htmlFor={inputId}\r\n className={labelVariants({ required: !!required })}\r\n >\r\n {label}\r\n {formConfig.colon && \":\"}\r\n </Label>\r\n )}\r\n \r\n <ShadcnSelect\r\n value={field.value?.toString() ?? \"\"}\r\n onValueChange={(value) => {\r\n // Handle clear selection\r\n if (value === \"\" && allowClear) {\r\n field.onChange(\"\");\r\n } else {\r\n field.onChange(value);\r\n }\r\n }}\r\n disabled={effectiveDisabled}\r\n >\r\n <SelectTrigger\r\n id={inputId}\r\n aria-invalid={hasError}\r\n aria-describedby={\r\n hasError\r\n ? `${inputId}-error`\r\n : helperText\r\n ? `${inputId}-helper`\r\n : undefined\r\n }\r\n className={cn(\r\n selectSizeVariants({ size: effectiveSize }),\r\n effectiveVariant === \"error\" && \"border-destructive focus:ring-destructive\",\r\n effectiveVariant === \"success\" && \"border-green-500 focus:ring-green-500\",\r\n className\r\n )}\r\n >\r\n <SelectValue placeholder={placeholder} />\r\n </SelectTrigger>\r\n <SelectContent>\r\n {allowClear && field.value && (\r\n <SelectItem value=\"\">\r\n <span className=\"text-muted-foreground\">Clear selection</span>\r\n </SelectItem>\r\n )}\r\n {children ?? options.map((option) => (\r\n <SelectItem\r\n key={option.value}\r\n value={option.value.toString()}\r\n disabled={option.disabled}\r\n >\r\n {option.label}\r\n </SelectItem>\r\n ))}\r\n </SelectContent>\r\n </ShadcnSelect>\r\n\r\n {showError && hasError && (\r\n <p\r\n id={`${inputId}-error`}\r\n className=\"text-sm text-destructive\"\r\n role=\"alert\"\r\n >\r\n {errorMessage}\r\n </p>\r\n )}\r\n \r\n {helperText && !hasError && (\r\n <p\r\n id={`${inputId}-helper`}\r\n className=\"text-sm text-muted-foreground\"\r\n >\r\n {helperText}\r\n </p>\r\n )}\r\n </div>\r\n );\r\n}\r\n\r\nSelect.displayName = \"Select\";\r\n"],"names":["useRHFFormContext","ShadcnSelect"],"mappings":";;;;;;;;AAeA,MAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA,IACE,UAAU;AAAA,MACR,MAAM;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,MAAA;AAAA,IACN;AAAA,IAEF,iBAAiB;AAAA,MACf,MAAM;AAAA,IAAA;AAAA,EACR;AAEJ;AAEA,MAAM,gBAAgB;AAAA,EACpB;AAAA,EACA;AAAA,IACE,UAAU;AAAA,MACR,UAAU;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA,MAAA;AAAA,IACT;AAAA,IAEF,iBAAiB;AAAA,MACf,UAAU;AAAA,IAAA;AAAA,EACZ;AAEJ;AAwEO,SAAS,OAGd;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,OAAO;AAAA,EACP;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EACA,UAAU,CAAA;AAAA,EACV;AAAA,EACA;AAAA,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AAAA,IAAA;AAAA,EACH,GAEJ;AAEJ;AAEA,OAAO,cAAc;"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ReactNode, ComponentPropsWithoutRef } from 'react';
|
|
2
|
+
import { FieldValues, FieldPath, Control } from 'react-hook-form';
|
|
3
|
+
import { VariantProps } from 'class-variance-authority';
|
|
4
|
+
import type * as SwitchPrimitives from "@radix-ui/react-switch";
|
|
5
|
+
declare const switchSizeVariants: (props?: ({
|
|
6
|
+
size?: "sm" | "md" | "lg" | null | undefined;
|
|
7
|
+
} & import('class-variance-authority/types').ClassProp) | undefined) => string;
|
|
8
|
+
export interface SwitchProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> extends Omit<ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>, "name" | "checked" | "onCheckedChange" | "required">, VariantProps<typeof switchSizeVariants> {
|
|
9
|
+
/** Field name - required for form integration */
|
|
10
|
+
name: TName;
|
|
11
|
+
/** Label text for the switch */
|
|
12
|
+
label?: ReactNode;
|
|
13
|
+
/** Helper text displayed below the switch */
|
|
14
|
+
helperText?: string;
|
|
15
|
+
/** Whether to show the error message */
|
|
16
|
+
showError?: boolean;
|
|
17
|
+
/** Custom error message (overrides form error) */
|
|
18
|
+
error?: string;
|
|
19
|
+
/** External control (for use outside Form) */
|
|
20
|
+
control?: Control<TFieldValues>;
|
|
21
|
+
/** Text to show when checked */
|
|
22
|
+
checkedText?: ReactNode;
|
|
23
|
+
/** Text to show when unchecked */
|
|
24
|
+
uncheckedText?: ReactNode;
|
|
25
|
+
/** Field is required (must be on) */
|
|
26
|
+
required?: boolean | string;
|
|
27
|
+
/** Custom validation function */
|
|
28
|
+
validate?: (value: unknown) => boolean | string | Promise<boolean | string>;
|
|
29
|
+
}
|
|
30
|
+
export declare const Switch: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(props: SwitchProps<TFieldValues, TName> & {
|
|
31
|
+
ref?: React.ForwardedRef<React.ElementRef<typeof SwitchPrimitives.Root>>;
|
|
32
|
+
}) => React.ReactElement;
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=Switch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Switch.d.ts","sourceRoot":"","sources":["../../../src/components/Form/Switch.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,OAAO,CAAC;AACjE,OAAO,EAAsD,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACrI,OAAO,EAAO,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAKlE,OAAO,KAAK,KAAK,gBAAgB,MAAM,wBAAwB,CAAC;AAEhE,QAAA,MAAM,kBAAkB;;8EAcvB,CAAC;AAiBF,MAAM,WAAW,WAAW,CAC1B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,KAAK,SAAS,SAAS,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC,YAAY,CAAC,CAC/D,SAAQ,IAAI,CAAC,wBAAwB,CAAC,OAAO,gBAAgB,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,iBAAiB,GAAG,UAAU,CAAC,EACvH,YAAY,CAAC,OAAO,kBAAkB,CAAC;IACzC,iDAAiD;IACjD,IAAI,EAAE,KAAK,CAAC;IACZ,gCAAgC;IAChC,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,wCAAwC;IACxC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,kDAAkD;IAClD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,OAAO,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IAChC,gCAAgC;IAChC,WAAW,CAAC,EAAE,SAAS,CAAC;IACxB,kCAAkC;IAClC,aAAa,CAAC,EAAE,SAAS,CAAC;IAG1B,qCAAqC;IACrC,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,iCAAiC;IACjC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;CAC7E;AA2ID,eAAO,MAAM,MAAM,EAA8B,CAC/C,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,KAAK,SAAS,SAAS,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC,YAAY,CAAC,EAE/D,KAAK,EAAE,WAAW,CAAC,YAAY,EAAE,KAAK,CAAC,GAAG;IAAE,GAAG,CAAC,EAAE,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAA;CAAE,KACnH,KAAK,CAAC,YAAY,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useId, useContext, useEffect } from "react";
|
|
3
|
+
import { useFormContext, useController } from "react-hook-form";
|
|
4
|
+
import { cva } from "../../node_modules/class-variance-authority/dist/index.js";
|
|
5
|
+
import { cn } from "../../utils/cn.js";
|
|
6
|
+
import { FormConfigContext } from "./context.js";
|
|
7
|
+
import { Switch as Switch$1 } from "../ui/switch.js";
|
|
8
|
+
import { Label } from "../ui/label.js";
|
|
9
|
+
const switchSizeVariants = cva(
|
|
10
|
+
"",
|
|
11
|
+
{
|
|
12
|
+
variants: {
|
|
13
|
+
size: {
|
|
14
|
+
sm: "h-5 w-9",
|
|
15
|
+
md: "h-6 w-11",
|
|
16
|
+
lg: "h-7 w-14"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
defaultVariants: {
|
|
20
|
+
size: "md"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
const labelVariants = cva(
|
|
25
|
+
"text-sm font-medium cursor-pointer select-none",
|
|
26
|
+
{
|
|
27
|
+
variants: {
|
|
28
|
+
disabled: {
|
|
29
|
+
true: "cursor-not-allowed opacity-50",
|
|
30
|
+
false: ""
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
defaultVariants: {
|
|
34
|
+
disabled: false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
function SwitchInner({
|
|
39
|
+
name,
|
|
40
|
+
label,
|
|
41
|
+
helperText,
|
|
42
|
+
showError = true,
|
|
43
|
+
error: customError,
|
|
44
|
+
size,
|
|
45
|
+
className,
|
|
46
|
+
disabled,
|
|
47
|
+
id: providedId,
|
|
48
|
+
control: externalControl,
|
|
49
|
+
checkedText,
|
|
50
|
+
uncheckedText,
|
|
51
|
+
// Validation props
|
|
52
|
+
required,
|
|
53
|
+
validate,
|
|
54
|
+
...props
|
|
55
|
+
}, ref) {
|
|
56
|
+
const generatedId = useId();
|
|
57
|
+
const inputId = providedId ?? generatedId;
|
|
58
|
+
const formConfigContext = useContext(FormConfigContext);
|
|
59
|
+
const formConfig = formConfigContext ?? {};
|
|
60
|
+
const rhfContext = useFormContext();
|
|
61
|
+
const control = externalControl ?? rhfContext?.control;
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (formConfigContext?.registerFieldValidation) {
|
|
64
|
+
const rules = {};
|
|
65
|
+
if (required !== void 0) rules.required = required;
|
|
66
|
+
if (validate !== void 0) rules.validate = validate;
|
|
67
|
+
formConfigContext.registerFieldValidation({
|
|
68
|
+
name,
|
|
69
|
+
type: "boolean",
|
|
70
|
+
rules
|
|
71
|
+
});
|
|
72
|
+
return () => {
|
|
73
|
+
formConfigContext.unregisterFieldValidation(name);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}, [formConfigContext, name, required, validate]);
|
|
77
|
+
const { field, fieldState } = useController({
|
|
78
|
+
name,
|
|
79
|
+
control
|
|
80
|
+
});
|
|
81
|
+
const fieldError = fieldState.error?.message;
|
|
82
|
+
const errorMessage = customError ?? fieldError;
|
|
83
|
+
const hasError = !!errorMessage;
|
|
84
|
+
const effectiveSize = size ?? formConfig.size ?? "md";
|
|
85
|
+
const effectiveDisabled = disabled ?? formConfig.disabled;
|
|
86
|
+
const isChecked = field.value ?? false;
|
|
87
|
+
return /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
|
|
88
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
|
|
89
|
+
/* @__PURE__ */ jsx(
|
|
90
|
+
Switch$1,
|
|
91
|
+
{
|
|
92
|
+
...props,
|
|
93
|
+
ref,
|
|
94
|
+
id: inputId,
|
|
95
|
+
checked: isChecked,
|
|
96
|
+
onCheckedChange: (checked) => field.onChange(checked),
|
|
97
|
+
disabled: effectiveDisabled,
|
|
98
|
+
"aria-labelledby": label ? `${inputId}-label` : void 0,
|
|
99
|
+
className: cn(
|
|
100
|
+
switchSizeVariants({ size: effectiveSize }),
|
|
101
|
+
className
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
),
|
|
105
|
+
(checkedText || uncheckedText) && /* @__PURE__ */ jsx("span", { className: "text-sm text-muted-foreground", children: isChecked ? checkedText : uncheckedText }),
|
|
106
|
+
label && /* @__PURE__ */ jsx(
|
|
107
|
+
Label,
|
|
108
|
+
{
|
|
109
|
+
id: `${inputId}-label`,
|
|
110
|
+
htmlFor: inputId,
|
|
111
|
+
className: labelVariants({ disabled: effectiveDisabled }),
|
|
112
|
+
children: label
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
] }),
|
|
116
|
+
showError && hasError && /* @__PURE__ */ jsx("p", { className: "text-sm text-destructive", role: "alert", children: errorMessage }),
|
|
117
|
+
helperText && !hasError && /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: helperText })
|
|
118
|
+
] });
|
|
119
|
+
}
|
|
120
|
+
const Switch = forwardRef(SwitchInner);
|
|
121
|
+
Switch.displayName = "Switch";
|
|
122
|
+
export {
|
|
123
|
+
Switch
|
|
124
|
+
};
|
|
125
|
+
//# sourceMappingURL=Switch.js.map
|