@nomos-ui/form 0.2.1 → 0.3.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.
- package/dist/components/form-switch.d.ts +45 -0
- package/dist/components/form-switch.d.ts.map +1 -0
- package/dist/components/form-switch.js +44 -0
- package/dist/components/form-switch.js.map +1 -0
- package/dist/components/switch.d.ts +23 -1
- package/dist/components/switch.d.ts.map +1 -1
- package/dist/components/switch.js +16 -0
- package/dist/components/switch.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Control, FieldValues, Path, PathValue } from "react-hook-form";
|
|
2
|
+
import { SwitchProps } from "./switch";
|
|
3
|
+
/**
|
|
4
|
+
* Props for the `FormSwitch` component with type-safe form integration.
|
|
5
|
+
*/
|
|
6
|
+
export type FormSwitchProps<TFieldValues extends FieldValues> = {
|
|
7
|
+
/** React Hook Form control instance */
|
|
8
|
+
control: Control<TFieldValues>;
|
|
9
|
+
/** The name of the field (type-safe, must match form schema) */
|
|
10
|
+
name: Path<TFieldValues>;
|
|
11
|
+
/** Default value of the switch */
|
|
12
|
+
defaultValue?: PathValue<TFieldValues, Path<TFieldValues>>;
|
|
13
|
+
} & Omit<SwitchProps, "checked" | "onCheckedChange">;
|
|
14
|
+
/**
|
|
15
|
+
* A form switch component that adds type-safe form functionality to Switch.
|
|
16
|
+
* Designed to integrate with React Hook Form with full TypeScript support.
|
|
17
|
+
*
|
|
18
|
+
* @component
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* type FormData = {
|
|
22
|
+
* notifications: boolean;
|
|
23
|
+
* darkMode: boolean;
|
|
24
|
+
* };
|
|
25
|
+
*
|
|
26
|
+
* const { control } = useForm<FormData>();
|
|
27
|
+
*
|
|
28
|
+
* <FormSwitch
|
|
29
|
+
* control={control}
|
|
30
|
+
* name="notifications"
|
|
31
|
+
* label="Enable notifications"
|
|
32
|
+
* required
|
|
33
|
+
* />
|
|
34
|
+
*
|
|
35
|
+
* <FormSwitch
|
|
36
|
+
* control={control}
|
|
37
|
+
* name="darkMode"
|
|
38
|
+
* label="Dark mode"
|
|
39
|
+
* labelPosition="start"
|
|
40
|
+
* size="sm"
|
|
41
|
+
* />
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export default function FormSwitch<TFieldValues extends FieldValues>({ control, name, defaultValue, required, ...props }: FormSwitchProps<TFieldValues>): import("react/jsx-runtime").JSX.Element;
|
|
45
|
+
//# sourceMappingURL=form-switch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form-switch.d.ts","sourceRoot":"","sources":["../../src/components/form-switch.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,OAAO,EACP,WAAW,EACX,IAAI,EACJ,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAe,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAG/C;;GAEG;AACH,MAAM,MAAM,eAAe,CAAC,YAAY,SAAS,WAAW,IAAI;IAC9D,uCAAuC;IACvC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IAC/B,gEAAgE;IAChE,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;IACzB,kCAAkC;IAClC,YAAY,CAAC,EAAE,SAAS,CAAC,YAAY,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;CAC5D,GAAG,IAAI,CAAC,WAAW,EAAE,SAAS,GAAG,iBAAiB,CAAC,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,YAAY,SAAS,WAAW,EAAE,EACnE,OAAO,EACP,IAAI,EACJ,YAAY,EACZ,QAAgB,EAChB,GAAG,KAAK,EACT,EAAE,eAAe,CAAC,YAAY,CAAC,2CAqB/B"}
|
|
@@ -0,0 +1,44 @@
|
|
|
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 = FormSwitch;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const react_hook_form_1 = require("react-hook-form");
|
|
9
|
+
const switch_1 = __importDefault(require("./switch"));
|
|
10
|
+
const form_helpers_1 = require("../utils/helpers/form.helpers");
|
|
11
|
+
/**
|
|
12
|
+
* A form switch component that adds type-safe form functionality to Switch.
|
|
13
|
+
* Designed to integrate with React Hook Form with full TypeScript support.
|
|
14
|
+
*
|
|
15
|
+
* @component
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* type FormData = {
|
|
19
|
+
* notifications: boolean;
|
|
20
|
+
* darkMode: boolean;
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* const { control } = useForm<FormData>();
|
|
24
|
+
*
|
|
25
|
+
* <FormSwitch
|
|
26
|
+
* control={control}
|
|
27
|
+
* name="notifications"
|
|
28
|
+
* label="Enable notifications"
|
|
29
|
+
* required
|
|
30
|
+
* />
|
|
31
|
+
*
|
|
32
|
+
* <FormSwitch
|
|
33
|
+
* control={control}
|
|
34
|
+
* name="darkMode"
|
|
35
|
+
* label="Dark mode"
|
|
36
|
+
* labelPosition="start"
|
|
37
|
+
* size="sm"
|
|
38
|
+
* />
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function FormSwitch({ control, name, defaultValue, required = false, ...props }) {
|
|
42
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "w-full", children: (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { control: control, name: name, defaultValue: defaultValue, rules: { required: required ? "Required" : false }, render: ({ field: { onChange, value }, formState: { errors } }) => ((0, jsx_runtime_1.jsx)(switch_1.default, { switchProps: { id: `form-switch-${name}` }, checked: !!value, onCheckedChange: onChange, required: required, error: (0, form_helpers_1.getNestedErrorMessage)(errors || {}, name), ...props })) }) }));
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=form-switch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form-switch.js","sourceRoot":"","sources":["../../src/components/form-switch.tsx"],"names":[],"mappings":";;;;;AAoDA,6BA2BC;;AA/ED,qDAMyB;AACzB,sDAA+C;AAC/C,gEAAsE;AActE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,SAAwB,UAAU,CAAmC,EACnE,OAAO,EACP,IAAI,EACJ,YAAY,EACZ,QAAQ,GAAG,KAAK,EAChB,GAAG,KAAK,EACsB;IAC9B,OAAO,CACL,gCAAK,SAAS,EAAC,QAAQ,YACrB,uBAAC,4BAAU,IACT,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,IAAI,EACV,YAAY,EAAE,YAAY,EAC1B,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,EAClD,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,CAAC,CACjE,uBAAC,gBAAM,IACL,WAAW,EAAE,EAAE,EAAE,EAAE,eAAe,IAAI,EAAE,EAAE,EAC1C,OAAO,EAAE,CAAC,CAAC,KAAK,EAChB,eAAe,EAAE,QAAQ,EACzB,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,IAAA,oCAAqB,EAAC,MAAM,IAAI,EAAE,EAAE,IAAI,CAAW,KACtD,KAAK,GACT,CACH,GACD,GACE,CACP,CAAC;AACJ,CAAC"}
|
|
@@ -1,2 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as SwitchPrimitive from "@radix-ui/react-switch";
|
|
3
|
+
export type SwitchProps = {
|
|
4
|
+
className?: string;
|
|
5
|
+
label?: string;
|
|
6
|
+
required?: boolean;
|
|
7
|
+
showRequiredSign?: boolean;
|
|
8
|
+
checked?: boolean;
|
|
9
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
10
|
+
size?: "sm" | "default";
|
|
11
|
+
labelProps?: React.ComponentProps<"label">;
|
|
12
|
+
switchProps?: Omit<React.ComponentProps<typeof SwitchPrimitive.Root>, "checked" | "onCheckedChange">;
|
|
13
|
+
/** Place the label before ("start") or after ("end") the switch. Defaults to "end". */
|
|
14
|
+
labelPosition?: "start" | "end";
|
|
15
|
+
error?: string;
|
|
16
|
+
tip?: string;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Switch with an optional label, tip, and error message.
|
|
21
|
+
* Mirrors the Select component's label + error + tip pattern.
|
|
22
|
+
*/
|
|
23
|
+
export default function Switch({ className, label, required, showRequiredSign, checked, onCheckedChange, size, labelProps, switchProps, labelPosition, error, tip, disabled, }: SwitchProps): import("react/jsx-runtime").JSX.Element;
|
|
2
24
|
//# sourceMappingURL=switch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../src/components/switch.tsx"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../src/components/switch.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,KAAK,eAAe,MAAM,wBAAwB,CAAC;AAK1D,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IAC7C,IAAI,CAAC,EAAE,IAAI,GAAG,SAAS,CAAC;IACxB,UAAU,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC3C,WAAW,CAAC,EAAE,IAAI,CAChB,KAAK,CAAC,cAAc,CAAC,OAAO,eAAe,CAAC,IAAI,CAAC,EACjD,SAAS,GAAG,iBAAiB,CAC9B,CAAC;IACF,uFAAuF;IACvF,aAAa,CAAC,EAAE,OAAO,GAAG,KAAK,CAAC;IAChC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAC7B,SAAS,EACT,KAAK,EACL,QAAgB,EAChB,gBAAwB,EACxB,OAAO,EACP,eAAe,EACf,IAAgB,EAChB,UAAU,EACV,WAAW,EACX,aAAqB,EACrB,KAAK,EACL,GAAG,EACH,QAAQ,GACT,EAAE,WAAW,2CAkDb"}
|
|
@@ -1,3 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = Switch;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const tailwind_merge_1 = require("tailwind-merge");
|
|
6
|
+
const lucide_react_1 = require("lucide-react");
|
|
7
|
+
const switch_1 = require("./shadcn-ui/switch");
|
|
8
|
+
/**
|
|
9
|
+
* Switch with an optional label, tip, and error message.
|
|
10
|
+
* Mirrors the Select component's label + error + tip pattern.
|
|
11
|
+
*/
|
|
12
|
+
function Switch({ className, label, required = false, showRequiredSign = false, checked, onCheckedChange, size = "default", labelProps, switchProps, labelPosition = "end", error, tip, disabled, }) {
|
|
13
|
+
const switchId = switchProps?.id ??
|
|
14
|
+
(label ? `switch-${label.toLowerCase().replace(/\s+/g, "-")}` : undefined);
|
|
15
|
+
const switchEl = ((0, jsx_runtime_1.jsx)(switch_1.Switch, { ...switchProps, id: switchId, size: size, checked: checked, onCheckedChange: onCheckedChange, disabled: disabled, "aria-invalid": !!error }));
|
|
16
|
+
const labelEl = label ? ((0, jsx_runtime_1.jsxs)("div", { className: "flex flex-row items-center gap-1", children: [(0, jsx_runtime_1.jsx)("label", { ...labelProps, htmlFor: labelProps?.htmlFor ?? switchId, className: (0, tailwind_merge_1.twMerge)("font-bold cursor-pointer", disabled && "opacity-50 cursor-not-allowed", labelProps?.className), children: label }), required && showRequiredSign && (0, jsx_runtime_1.jsx)(lucide_react_1.AsteriskIcon, { size: 12, color: "red" })] })) : null;
|
|
17
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: (0, tailwind_merge_1.twMerge)("gap-1 grid w-full", className), children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex flex-row items-center gap-2", children: [labelPosition === "start" && labelEl, switchEl, labelPosition === "end" && labelEl] }), tip && !error && ((0, jsx_runtime_1.jsx)("p", { className: "text-xs text-muted-foreground tip-message", children: tip })), error && ((0, jsx_runtime_1.jsxs)("p", { className: "text-xs text-destructive error-message", children: ["*", error] }))] }));
|
|
18
|
+
}
|
|
3
19
|
//# sourceMappingURL=switch.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switch.js","sourceRoot":"","sources":["../../src/components/switch.tsx"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"switch.js","sourceRoot":"","sources":["../../src/components/switch.tsx"],"names":[],"mappings":";;AA8BA,yBAgEC;;AA5FD,mDAAyC;AACzC,+CAA4C;AAC5C,+CAA4D;AAsB5D;;;GAGG;AACH,SAAwB,MAAM,CAAC,EAC7B,SAAS,EACT,KAAK,EACL,QAAQ,GAAG,KAAK,EAChB,gBAAgB,GAAG,KAAK,EACxB,OAAO,EACP,eAAe,EACf,IAAI,GAAG,SAAS,EAChB,UAAU,EACV,WAAW,EACX,a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