|
1 | 1 | import { useForm } from "react-hook-form"; |
2 | 2 | import { zodResolver } from "@hookform/resolvers/zod"; |
3 | 3 | import { z } from "zod"; |
| 4 | +import { useEffect, useRef } from "react"; |
4 | 5 | import { useTranslation } from "react-i18next"; |
5 | 6 | import { Button } from "@/components/ui/button"; |
6 | 7 | import { Input } from "@/components/ui/input"; |
@@ -73,19 +74,30 @@ export function TenantForm({ |
73 | 74 | }); |
74 | 75 |
|
75 | 76 | const watchName = form.watch("name"); |
| 77 | + const watchSlug = form.watch("slug"); |
| 78 | + const userHasEditedSlug = useRef(false); |
76 | 79 |
|
77 | | - // Auto-generate slug from name if enabled |
78 | | - if (autoGenerateSlug) { |
79 | | - const slugValue = watchName |
80 | | - .trim() |
81 | | - .toLowerCase() |
82 | | - .replace(/\s+/g, "-") |
83 | | - .replace(/[^a-z0-9-]/g, ""); |
84 | | - |
85 | | - if (slugValue !== form.getValues("slug")) { |
86 | | - form.setValue("slug", slugValue); |
| 80 | + // Track if user has manually edited the slug |
| 81 | + useEffect(() => { |
| 82 | + if (watchSlug && watchSlug !== initialValues.slug) { |
| 83 | + userHasEditedSlug.current = true; |
| 84 | + } |
| 85 | + }, [watchSlug, initialValues.slug]); |
| 86 | + |
| 87 | + // Auto-generate slug from name if enabled and user hasn't manually edited slug |
| 88 | + useEffect(() => { |
| 89 | + if (autoGenerateSlug && !userHasEditedSlug.current && watchName) { |
| 90 | + const slugValue = watchName |
| 91 | + .trim() |
| 92 | + .toLowerCase() |
| 93 | + .replace(/\s+/g, "-") |
| 94 | + .replace(/[^a-z0-9-]/g, ""); |
| 95 | + |
| 96 | + if (slugValue !== form.getValues("slug")) { |
| 97 | + form.setValue("slug", slugValue, { shouldValidate: false }); |
| 98 | + } |
87 | 99 | } |
88 | | - } |
| 100 | + }, [watchName, autoGenerateSlug, form]); |
89 | 101 |
|
90 | 102 | const handleFormSubmit = (values: z.infer<typeof formSchema>) => { |
91 | 103 | onSubmit(values); |
@@ -116,11 +128,18 @@ export function TenantForm({ |
116 | 128 | <FormItem> |
117 | 129 | <FormLabel>{t("slug")}</FormLabel> |
118 | 130 | <FormControl> |
119 | | - <Input placeholder={t("slug")} {...field} /> |
| 131 | + <Input |
| 132 | + placeholder={t("slug")} |
| 133 | + {...field} |
| 134 | + onChange={(e) => { |
| 135 | + userHasEditedSlug.current = true; |
| 136 | + field.onChange(e); |
| 137 | + }} |
| 138 | + /> |
120 | 139 | </FormControl> |
121 | 140 | <FormMessage /> |
122 | 141 | <p className="text-xs text-muted-foreground"> |
123 | | - {t("name")}: /tenant/{field.value || "example"} |
| 142 | + URL: /tenant/{field.value || "example"} |
124 | 143 | </p> |
125 | 144 | </FormItem> |
126 | 145 | )} |
|
0 commit comments