Documentation

ColorPickerField

Pick a color via swatches or manual input.

Preview

"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { useForm, FormProvider } from "react-hook-form";
import { z } from "zod";
import ColorPickerField from "@/components/ui/ColorPickerField";

export const schema = z.object({
  color: z.string().regex(/^#([0-9a-fA-F]{3}){1,2}$/i, { message: "Invalid color." }),
});

export function ColorPickerFieldBasicExample() {
  const methods = useForm<z.infer<typeof schema>>({
    resolver: zodResolver(schema),
    defaultValues: { color: "#000000" },
  });

  const onSubmit = (data: z.infer<typeof schema>) => {
    console.log("Form data:", data);
    alert("Check the console for the form data!");
  };

  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(onSubmit)} className="space-y-4 w-80">
        <ColorPickerField name="color" label="Favorite color" />
        <button type="submit" className="form-button form-button-primary w-full">Submit</button>
      </form>
    </FormProvider>
  );
}

Props

NameTypeDefaultDescription
name*string-Form field name.
label*string-Label displayed above the input.
swatchesstring[]-Predefined color swatches.
classNamestring-Additional CSS classes.