mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-04-16 18:18:42 -05:00
* Add types * Delete stuff * wip * findAllBetweenTwoTimestamps refactor * wip * wip * wip * wip * wip * wip * wip * Fixes * wip * wip * Fix InfoPopover button styling * wip * wip * wip * Merge branch 'rewrite' into new-calendar * wip * wip * wip * wip * Rename myform -> sendouform * wip * wip * wip * wip * wip * wip * wip * wip * wip * rename * fix test
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import * as React from "react";
|
|
import {
|
|
Controller,
|
|
type FieldPath,
|
|
type FieldValues,
|
|
get,
|
|
useFormContext,
|
|
} from "react-hook-form";
|
|
import { FormMessage } from "~/components/FormMessage";
|
|
import { Label } from "~/components/Label";
|
|
import { SendouSwitch } from "../elements/Switch";
|
|
|
|
export function ToggleFormField<T extends FieldValues>({
|
|
label,
|
|
name,
|
|
bottomText,
|
|
}: { label: string; name: FieldPath<T>; bottomText?: string }) {
|
|
const methods = useFormContext();
|
|
const id = React.useId();
|
|
|
|
const error = get(methods.formState.errors, name);
|
|
|
|
return (
|
|
<div>
|
|
<Label htmlFor={id}>{label}</Label>
|
|
<Controller
|
|
control={methods.control}
|
|
name={name}
|
|
render={({ field: { value, onChange } }) => (
|
|
<SendouSwitch
|
|
id={id}
|
|
isSelected={value ?? false}
|
|
onChange={onChange}
|
|
/>
|
|
)}
|
|
/>
|
|
{error && (
|
|
<FormMessage type="error">{error.message as string}</FormMessage>
|
|
)}
|
|
{bottomText && !error ? (
|
|
<FormMessage type="info">{bottomText}</FormMessage>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|