@szum-tech/design-system
Version:
Szum-Tech design system with tailwindcss support
89 lines (86 loc) • 3.39 kB
JavaScript
import { HelperText } from './chunk-5PBHBOXI.js';
import { Label } from './chunk-VZKTT6CG.js';
import { useFormContext, FormProvider, Controller } from 'react-hook-form';
import * as React2 from 'react';
import { twMerge } from 'tailwind-merge';
import { jsx, jsxs } from 'react/jsx-runtime';
import { Slot } from '@radix-ui/react-slot';
var Form = FormProvider;
var FormItemContext = React2.createContext({});
var FormItem = React2.forwardRef(function({ className, ...props }, ref) {
const id = React2.useId();
return /* @__PURE__ */ jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { ref, className: twMerge("space-y-2", className), ...props }) });
});
FormItem.displayName = "FormItem";
var FormFieldContext = React2.createContext({});
var FormField = ({
...props
}) => {
return /* @__PURE__ */ jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
};
var useFormField = () => {
const fieldContext = React2.useContext(FormFieldContext);
const itemContext = React2.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 FormLabel = React2.forwardRef(
({ className, caption, ...props }, ref) => {
const { error, formItemId } = useFormField();
return /* @__PURE__ */ jsxs("div", { className: "flex flex-row items-end justify-between", children: [
/* @__PURE__ */ jsx(
Label,
{
ref,
className: twMerge(error ? "text-error-500" : null, className),
htmlFor: formItemId,
...props
}
),
caption ? /* @__PURE__ */ jsx("div", { className: "typography-caption text-gray-200", children: caption }) : null
] });
}
);
FormLabel.displayName = "FormLabel";
var FormControl = React2.forwardRef(function(props, ref) {
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
const newProps = { ...props, invalid: !!error };
return /* @__PURE__ */ jsx(
Slot,
{
ref,
id: formItemId,
"aria-describedby": !error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`,
"aria-invalid": !!error,
...newProps
}
);
});
FormControl.displayName = "FormControl";
var FormMessage = React2.forwardRef(function({ children, ...props }, ref) {
const { error, formMessageId } = useFormField();
const body = error ? String(error?.message) : children;
if (!body) {
return null;
}
return /* @__PURE__ */ jsx(HelperText, { ref, type: "error", id: formMessageId, ...props, children: body });
});
FormMessage.displayName = "FormMessage";
var FormDescription = React2.forwardRef(function(props, ref) {
const { formDescriptionId } = useFormField();
return /* @__PURE__ */ jsx(HelperText, { ref, id: formDescriptionId, ...props });
});
FormDescription.displayName = "FormDescription";
export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage };