From 4652f0d94dd73c6be20d5afe0dcd7fc674f4d221 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sat, 17 May 2025 11:06:40 -0500 Subject: [PATCH] add optional regex validation to TextEntry component --- web/src/components/input/TextEntry.tsx | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/web/src/components/input/TextEntry.tsx b/web/src/components/input/TextEntry.tsx index 85445b110..e266444c7 100644 --- a/web/src/components/input/TextEntry.tsx +++ b/web/src/components/input/TextEntry.tsx @@ -18,18 +18,33 @@ type TextEntryProps = { allowEmpty?: boolean; onSave: (text: string) => void; children?: React.ReactNode; + regexPattern?: RegExp; + regexErrorMessage?: string; }; + export default function TextEntry({ defaultValue = "", placeholder, allowEmpty = false, onSave, children, + regexPattern, + regexErrorMessage = "Input does not match the required format", }: TextEntryProps) { const formSchema = z.object({ - text: allowEmpty - ? z.string().optional() - : z.string().min(1, "Field is required"), + text: z + .string() + .optional() + .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>({