mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-07-03 18:41:14 +03:00
Some checks failed
CI / AMD64 Build (push) Has been cancelled
CI / ARM Build (push) Has been cancelled
CI / Jetson Jetpack 6 (push) Has been cancelled
CI / AMD64 Extra Build (push) Has been cancelled
CI / ARM Extra Build (push) Has been cancelled
CI / Synaptics Build (push) Has been cancelled
CI / Assemble and push default build (push) Has been cancelled
* remove redundant per-view toasters in settings * add variants to standardize dialog footer button layouts * remove text-md this class name compiles to nothing in tailwind. we used to add it to prevent iOS from zooming when focusing on an input, but that is now solved via the viewport meta in index.html * make wizard footers consistent with dialog footers * consistent destructive button style remove text-white from individual buttons and add it to the variant
94 lines
2.3 KiB
TypeScript
94 lines
2.3 KiB
TypeScript
import {
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
FormItem,
|
|
FormMessage,
|
|
} from "@/components/ui/form";
|
|
import { Input } from "@/components/ui/input";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import React, { useCallback } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
type TextEntryProps = {
|
|
defaultValue?: string;
|
|
placeholder?: string;
|
|
allowEmpty?: boolean;
|
|
onSave: (text: string) => void;
|
|
children?: React.ReactNode;
|
|
regexPattern?: RegExp;
|
|
regexErrorMessage?: string;
|
|
forbiddenPattern?: RegExp;
|
|
forbiddenErrorMessage?: string;
|
|
};
|
|
|
|
export default function TextEntry({
|
|
defaultValue = "",
|
|
placeholder,
|
|
allowEmpty = false,
|
|
onSave,
|
|
children,
|
|
regexPattern,
|
|
regexErrorMessage = "Input does not match the required format",
|
|
forbiddenPattern,
|
|
forbiddenErrorMessage = "Input contains invalid characters",
|
|
}: TextEntryProps) {
|
|
const formSchema = z.object({
|
|
text: z
|
|
.string()
|
|
.optional()
|
|
.refine((val) => !val || !forbiddenPattern?.test(val), {
|
|
message: forbiddenErrorMessage,
|
|
})
|
|
.refine(
|
|
(val) => {
|
|
if (!allowEmpty && !val) return false;
|
|
if (val && regexPattern) return regexPattern.test(val);
|
|
return true;
|
|
},
|
|
{
|
|
message: regexPattern ? regexErrorMessage : "Field is required",
|
|
},
|
|
),
|
|
});
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: { text: defaultValue },
|
|
});
|
|
|
|
const onSubmit = useCallback(
|
|
(data: z.infer<typeof formSchema>) => {
|
|
onSave(data.text || "");
|
|
},
|
|
[onSave],
|
|
);
|
|
|
|
return (
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
<FormField
|
|
control={form.control}
|
|
name="text"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormControl>
|
|
<Input
|
|
{...field}
|
|
className="w-full"
|
|
placeholder={placeholder}
|
|
type="text"
|
|
/>
|
|
</FormControl>
|
|
<FormMessage className="text-xs text-destructive" />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{children}
|
|
</form>
|
|
</Form>
|
|
);
|
|
}
|