Switch Field

PreviousNext

A component for Wandry UI

Docs
wandry-uicomponent

Preview

Loading preview…
registry/wandry-ui/switch-field.tsx
"use client";
import * as React from "react";
import { useField } from "@wandry/inertia-form";

import {
  Field,
  FieldContent,
  FieldDescription,
  FieldLabel,
} from "@/components/ui/field";
import { Switch } from "@/components/ui/switch";

export type SwitchFieldProps = {
  name: string;
  label?: string;
  description?: string;
  errorName?: string;
  orientation?: "vertical" | "horizontal" | "responsive";
};

const SwitchField: React.FC<SwitchFieldProps> = ({
  name,
  label,
  description,
  errorName,
  orientation = "horizontal",
}) => {
  const field = useField(name, { defaultValue: false, errorName });

  return (
    <Field orientation={orientation}>
      <FieldContent>
        <FieldLabel htmlFor={name}>{label}</FieldLabel>
        <FieldDescription>{description}</FieldDescription>
      </FieldContent>
      <Switch
        id={name}
        checked={field.value}
        onCheckedChange={field.onChange}
      />
    </Field>
  );
};

export default SwitchField;

Installation

npx shadcn@latest add @wandry-ui/switch-field

Usage

import { SwitchField } from "@/components/switch-field"
<SwitchField />