@easy-shadcn/react
Version:
Use shadcn/ui easy&enhance like component library
133 lines (129 loc) • 4.13 kB
JavaScript
import { Label } from './chunk-MK57TVGY.mjs';
import * as ReactHookForm from 'react-hook-form';
import { useFormContext, Controller, FormProvider } from 'react-hook-form';
import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
import { cn } from '@easy-shadcn/utils';
import { jsx, jsxs } from 'react/jsx-runtime';
var Form = FormProvider;
var FormFieldContext = React.createContext(
{}
);
var FormField = ({
...props
}) => {
return /* @__PURE__ */ jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
};
var useFormField = () => {
const fieldContext = React.useContext(FormFieldContext);
const itemContext = React.useContext(FormItemContext);
const { getFieldState, formState } = useFormContext();
const fieldState = getFieldState(fieldContext.name, formState);
if (!fieldContext) {
throw new Error("useFormField should be used within <FormField>");
}
const { id } = itemContext;
return {
id,
name: fieldContext.name,
formItemId: `${id}-form-item`,
formDescriptionId: `${id}-form-item-description`,
formMessageId: `${id}-form-item-message`,
...fieldState
};
};
var FormItemContext = React.createContext(
{}
);
var FormItem = React.forwardRef(({ className, ...props }, ref) => {
const id = React.useId();
return /* @__PURE__ */ jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { ref, className: cn("space-y-2", className), ...props }) });
});
FormItem.displayName = "FormItem";
var FormLabel = React.forwardRef(({ className, ...props }, ref) => {
const { error, formItemId } = useFormField();
return /* @__PURE__ */ jsx(
Label,
{
ref,
className: cn(error && "text-destructive", className),
htmlFor: formItemId,
...props
}
);
});
FormLabel.displayName = "FormLabel";
var FormControl = React.forwardRef(({ ...props }, ref) => {
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
return /* @__PURE__ */ jsx(
Slot,
{
ref,
id: formItemId,
"aria-describedby": !error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`,
"aria-invalid": !!error,
...props
}
);
});
FormControl.displayName = "FormControl";
var FormDescription = React.forwardRef(({ className, ...props }, ref) => {
const { formDescriptionId } = useFormField();
return /* @__PURE__ */ jsx(
"p",
{
ref,
id: formDescriptionId,
className: cn("text-[0.8rem] text-muted-foreground", className),
...props
}
);
});
FormDescription.displayName = "FormDescription";
var FormMessage = React.forwardRef(({ className, children, ...props }, ref) => {
const { error, formMessageId } = useFormField();
const body = error ? String(error?.message) : children;
if (!body) {
return null;
}
return /* @__PURE__ */ jsx(
"p",
{
ref,
id: formMessageId,
className: cn("text-[0.8rem] font-medium text-destructive", className),
...props,
children: body
}
);
});
FormMessage.displayName = "FormMessage";
var Form2 = ({ form, children, ...props }) => {
return /* @__PURE__ */ jsx(Form, { ...form, children: /* @__PURE__ */ jsx("form", { ...props, onSubmit: props.onSubmit || ((e) => e.preventDefault()), children }) });
};
var FormItem2 = ({
itemProps,
label,
labelProps,
description,
descriptionProps,
render,
...controllerProps
}) => {
return /* @__PURE__ */ jsx(
FormField,
{
...controllerProps,
render: (params) => /* @__PURE__ */ jsxs(FormItem, { ...itemProps, children: [
label && /* @__PURE__ */ jsx(FormLabel, { ...labelProps, children: label }),
/* @__PURE__ */ jsx(FormControl, { children: render(params) }),
description && /* @__PURE__ */ jsx(FormDescription, { ...descriptionProps, children: description }),
/* @__PURE__ */ jsx(FormMessage, {})
] })
}
);
};
// src/form/index.tsx
var Form3 = Form2;
Object.assign(Form3, ReactHookForm);
export { Form3 as Form, FormItem2 as FormItem };