From 44b93e0598c1b0761ac675696257c244a2b1f229 Mon Sep 17 00:00:00 2001 From: Caroline Jeandat Date: Wed, 17 Sep 2025 18:22:26 +0200 Subject: [PATCH] add isPrefilled arg --- .../reactHookForm/text/UniqueNameInput.tsx | 3 +++ src/hooks/use-unique-name-validation.ts | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/components/inputs/reactHookForm/text/UniqueNameInput.tsx b/src/components/inputs/reactHookForm/text/UniqueNameInput.tsx index f2e419f0..fdb2ec2d 100644 --- a/src/components/inputs/reactHookForm/text/UniqueNameInput.tsx +++ b/src/components/inputs/reactHookForm/text/UniqueNameInput.tsx @@ -33,6 +33,7 @@ export interface UniqueNameInputProps { >; activeDirectory?: UUID; currentName?: string; + isPrefilled?: boolean; sx?: SxProps; fullWidth?: boolean; } @@ -48,6 +49,7 @@ export function UniqueNameInput({ onManualChangeCallback, formProps, currentName = '', + isPrefilled = false, activeDirectory, sx, fullWidth = true, @@ -64,6 +66,7 @@ export function UniqueNameInput({ currentName, elementType, activeDirectory, + isPrefilled, }); // Handle on user's change diff --git a/src/hooks/use-unique-name-validation.ts b/src/hooks/use-unique-name-validation.ts index 1f5590a6..778058c2 100644 --- a/src/hooks/use-unique-name-validation.ts +++ b/src/hooks/use-unique-name-validation.ts @@ -18,6 +18,7 @@ interface UseUniqueNameValidationParams { elementType: ElementType; activeDirectory?: string; elementExists?: (directory: UUID, name: string, type: ElementType) => Promise; + isPrefilled?: boolean; } export function useUniqueNameValidation({ @@ -26,6 +27,7 @@ export function useUniqueNameValidation({ elementType, activeDirectory, elementExists = elementAlreadyExists, + isPrefilled = false, }: UseUniqueNameValidationParams) { const { setError, @@ -36,6 +38,7 @@ export function useUniqueNameValidation({ const { field: { value }, + fieldState: { isDirty: fieldIsDirty }, } = useController({ name }); const { @@ -92,14 +95,20 @@ export function useUniqueNameValidation({ debouncedHandleCheckName(trimmedValue); } - // if the form is unchanged, we don't do custom validation - if (!formIsDirty) { + // if the name is unchanged, we don't do custom validation + if (!isPrefilled && !fieldIsDirty) { clearErrors(name); return; } - if (trimmedValue === defaultFieldValue && trimmedValue.length > 0) { + if (isPrefilled && !formIsDirty) { + clearErrors(name); return; } + + if (isPrefilled && trimmedValue === defaultFieldValue && trimmedValue.length > 0) { + return; + } + if (trimmedValue) { clearErrors(name); setError('root.isValidating', { @@ -120,7 +129,9 @@ export function useUniqueNameValidation({ setError, clearErrors, name, + fieldIsDirty, formIsDirty, + isPrefilled, defaultFieldValue, directory, selectedDirectory,