跳至正文
获取 Pro
基础原语U.14

表单字段

FreeRegistry
components/linyi/field.tsx
@base-ui/reactclass-variance-authority
"use client";
 
import * as React from "react";
import { Field as BaseField } from "@base-ui/react/field";
import { Form as BaseForm } from "@base-ui/react/form";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/cn";
 
export const formVariants = cva("flex w-full flex-col", {
variants: {
density: {
compact: "gap-3",
default: "gap-4",
comfortable: "gap-5",
},
},
defaultVariants: {
density: "default",
},
});
 
export const fieldVariants = cva("group flex w-full", {
variants: {
orientation: {
vertical: "flex-col gap-1.5",
horizontal: "flex-row items-start justify-between gap-4",
},
},
defaultVariants: {
orientation: "vertical",
},
});
 
export const fieldLabelVariants = cva(
"inline-flex items-center gap-1 text-xs font-medium text-fg select-none data-[disabled]:opacity-50",
{
variants: {
required: {
true: "after:content-['*'] after:text-danger after:font-mono-num",
false: "",
},
},
defaultVariants: {
required: false,
},
}
);
 
export const fieldControlVariants = cva(
[
"w-full min-w-0 rounded-control border text-fg placeholder:text-fg-muted outline-none transition-colors",
"focus-visible:ring-2 focus-visible:ring-accent/35 focus-visible:border-accent",
"data-[invalid]:border-danger data-[invalid]:hover:border-danger data-[invalid]:focus-visible:border-danger data-[invalid]:focus-visible:ring-danger/30",
"data-[disabled]:opacity-50 data-[disabled]:cursor-not-allowed",
].join(" "),
{
variants: {
variant: {
surface: "bg-surface border-line hover:border-line-strong",
well: "bg-well border-line-well hover:border-line-strong",
ghost: "bg-transparent border-transparent hover:bg-well/70",
},
size: {
sm: "h-control-sm px-2.5 text-xs",
md: "h-control-md px-3 text-xs",
lg: "h-control-lg px-3.5 text-sm",
},
},
defaultVariants: {
variant: "surface",
size: "md",
},
}
);
 
export interface FormProps<
FormValues extends Record<string, unknown> = Record<string, unknown>,
> extends Omit<BaseForm.Props<FormValues>, "className">,
VariantProps<typeof formVariants> {
className?: string;
ref?: React.Ref<HTMLFormElement>;
}
 
export function Form<
FormValues extends Record<string, unknown> = Record<string, unknown>,
>({ className, density = "default", ...props }: FormProps<FormValues>) {
return (
<BaseForm<FormValues>
className={cn(formVariants({ density }), className)}
{...props}
/>
);
}
 
export interface FieldProps
extends Omit<React.ComponentPropsWithRef<typeof BaseField.Root>, "className">,
VariantProps<typeof fieldVariants> {
className?: string;
}
 
export function Field({
className,
orientation = "vertical",
...props
}: FieldProps) {
return (
<BaseField.Root
className={cn(fieldVariants({ orientation }), className)}
{...props}
/>
);
}
 
export interface FieldLabelProps
extends Omit<React.ComponentPropsWithRef<typeof BaseField.Label>, "className">,
VariantProps<typeof fieldLabelVariants> {
className?: string;
}
 
export function FieldLabel({
className,
required = false,
...props
}: FieldLabelProps) {
return (
<BaseField.Label
className={cn(fieldLabelVariants({ required }), className)}
{...props}
/>
);
}
 
export interface FieldControlProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseField.Control>,
"size" | "className"
>,
VariantProps<typeof fieldControlVariants> {
className?: string;
}
 
export function FieldControl({
className,
variant = "surface",
size = "md",
...props
}: FieldControlProps) {
return (
<BaseField.Control
className={cn(fieldControlVariants({ variant, size }), className)}
{...props}
/>
);
}
 
export type FieldDescriptionProps = Omit<
React.ComponentPropsWithRef<typeof BaseField.Description>,
"className"
> & {
className?: string;
};
 
export function FieldDescription({
className,
...props
}: FieldDescriptionProps) {
return (
<BaseField.Description
className={cn("text-[11px] leading-relaxed text-fg-muted", className)}
{...props}
/>
);
}
 
export type FieldErrorProps = Omit<
React.ComponentPropsWithRef<typeof BaseField.Error>,
"className"
> & {
className?: string;
};
 
export function FieldError({ className, ...props }: FieldErrorProps) {
return (
<BaseField.Error
className={cn("text-[11px] leading-snug text-danger", className)}
{...props}
/>
);
}
 
export const FieldValidity = BaseField.Validity;
export const FieldItem = BaseField.Item;