@dimaan/ui 0.0.4 → 0.0.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.
package/dist/index.cjs CHANGED
@@ -5,6 +5,7 @@ var clsx = require('clsx');
5
5
  var tailwindMerge = require('tailwind-merge');
6
6
  var jsxRuntime = require('react/jsx-runtime');
7
7
  var lucideReact = require('lucide-react');
8
+ var reactHookForm = require('react-hook-form');
8
9
 
9
10
  // src/components/dashboard-layout/context.ts
10
11
  var DashboardLayoutContext = react.createContext(null);
@@ -744,6 +745,125 @@ var Checkbox = react.forwardRef(function Checkbox2({
744
745
  )
745
746
  ] });
746
747
  });
748
+ function Field(props) {
749
+ const formContext = reactHookForm.useFormContext();
750
+ if (props.name !== void 0) {
751
+ const { control: explicitControl, name, children: children2, ...layout2 } = props;
752
+ const control = explicitControl ?? formContext?.control;
753
+ if (!control) {
754
+ throw new Error(
755
+ '<Field name="..."> requires either a `control` prop OR being rendered inside <FormProvider>. Wrap your form with <FormProvider {...form}> from react-hook-form, or pass `control={form.control}` explicitly.'
756
+ );
757
+ }
758
+ return /* @__PURE__ */ jsxRuntime.jsx(
759
+ reactHookForm.Controller,
760
+ {
761
+ control,
762
+ name,
763
+ render: ({ field, fieldState }) => /* @__PURE__ */ jsxRuntime.jsx(FieldShell, { ...layout2, error: fieldState.error?.message, invalid: fieldState.invalid, children: cloneFieldChild(children2, {
764
+ name: field.name,
765
+ value: field.value ?? "",
766
+ onChange: field.onChange,
767
+ onBlur: field.onBlur,
768
+ ref: field.ref,
769
+ disabled: layout2.disabled ?? field.disabled
770
+ }) })
771
+ }
772
+ );
773
+ }
774
+ const { children, error, invalid, ...layout } = props;
775
+ return /* @__PURE__ */ jsxRuntime.jsx(FieldShell, { ...layout, error, invalid: invalid ?? Boolean(error), children });
776
+ }
777
+ function FieldShell({
778
+ label,
779
+ description,
780
+ error,
781
+ invalid = false,
782
+ required = false,
783
+ disabled = false,
784
+ fullWidth = true,
785
+ className,
786
+ children
787
+ }) {
788
+ const reactId = react.useId();
789
+ if (!react.isValidElement(children)) {
790
+ throw new Error("<Field> requires a single React element as its child.");
791
+ }
792
+ const childProps = children.props;
793
+ const id = childProps.id ?? reactId;
794
+ const descriptionId = `${id}-description`;
795
+ const errorId = `${id}-error`;
796
+ const showError = invalid && error !== void 0 && error !== null && error !== false;
797
+ const showDescription = !showError && description !== void 0 && description !== null;
798
+ const userDescribedBy = childProps["aria-describedby"];
799
+ const describedBy = [userDescribedBy, showDescription ? descriptionId : null, showError ? errorId : null].filter(Boolean).join(" ") || void 0;
800
+ const enhancedChild = cloneFieldChild(children, {
801
+ id,
802
+ "aria-invalid": childProps["aria-invalid"] ?? (invalid || void 0),
803
+ "aria-describedby": describedBy,
804
+ disabled: childProps.disabled ?? disabled,
805
+ required: childProps.required ?? required
806
+ });
807
+ return /* @__PURE__ */ jsxRuntime.jsxs(
808
+ "div",
809
+ {
810
+ "data-invalid": invalid || void 0,
811
+ "data-disabled": disabled || void 0,
812
+ className: cn("flex flex-col gap-1.5", fullWidth && "w-full", className),
813
+ children: [
814
+ label !== void 0 && label !== null && /* @__PURE__ */ jsxRuntime.jsxs(
815
+ "label",
816
+ {
817
+ htmlFor: id,
818
+ className: cn(
819
+ "text-sm font-medium select-none text-foreground",
820
+ disabled && "opacity-50",
821
+ invalid && "text-destructive"
822
+ ),
823
+ children: [
824
+ label,
825
+ required && /* @__PURE__ */ jsxRuntime.jsx("span", { "aria-hidden": "true", className: "ms-0.5 text-destructive", children: "*" })
826
+ ]
827
+ }
828
+ ),
829
+ enhancedChild,
830
+ showError ? /* @__PURE__ */ jsxRuntime.jsx(
831
+ "p",
832
+ {
833
+ id: errorId,
834
+ role: "alert",
835
+ "aria-live": "polite",
836
+ className: "text-xs font-medium text-destructive",
837
+ children: error
838
+ }
839
+ ) : showDescription ? /* @__PURE__ */ jsxRuntime.jsx("p", { id: descriptionId, className: "text-xs text-muted-foreground", children: description }) : null
840
+ ]
841
+ }
842
+ );
843
+ }
844
+ function cloneFieldChild(child, injected) {
845
+ const childProps = child.props;
846
+ const childRef = child.ref;
847
+ const merged = { ...injected };
848
+ for (const key of Object.keys(childProps)) {
849
+ const value = childProps[key];
850
+ if (value !== void 0) merged[key] = value;
851
+ }
852
+ if (injected.ref) {
853
+ merged.ref = mergeRefs(injected.ref, childRef);
854
+ } else if (childRef) {
855
+ merged.ref = childRef;
856
+ }
857
+ return react.cloneElement(child, merged);
858
+ }
859
+ function mergeRefs(...refs) {
860
+ return (instance) => {
861
+ for (const ref of refs) {
862
+ if (typeof ref === "function") ref(instance);
863
+ else if (ref && typeof ref === "object") ref.current = instance;
864
+ }
865
+ };
866
+ }
747
867
 
748
868
  // src/components/input/inputVariants.ts
749
869
  var inputVariantClass = {
@@ -1479,6 +1599,7 @@ exports.DashboardContent = DashboardContent;
1479
1599
  exports.DashboardHeader = DashboardHeader;
1480
1600
  exports.DashboardLayout = DashboardLayout;
1481
1601
  exports.DashboardMain = DashboardMain;
1602
+ exports.Field = Field;
1482
1603
  exports.HeaderActions = HeaderActions;
1483
1604
  exports.HeaderCollapseTrigger = HeaderCollapseTrigger;
1484
1605
  exports.HeaderMobileTrigger = HeaderMobileTrigger;