Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/inputs/reactHookForm/text/UniqueNameInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface UniqueNameInputProps {
>;
activeDirectory?: UUID;
currentName?: string;
isPrefilled?: boolean;
sx?: SxProps<Theme>;
fullWidth?: boolean;
}
Expand All @@ -48,6 +49,7 @@ export function UniqueNameInput({
onManualChangeCallback,
formProps,
currentName = '',
isPrefilled = false,
activeDirectory,
sx,
fullWidth = true,
Expand All @@ -64,6 +66,7 @@ export function UniqueNameInput({
currentName,
elementType,
activeDirectory,
isPrefilled,
});

// Handle on user's change
Expand Down
17 changes: 14 additions & 3 deletions src/hooks/use-unique-name-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface UseUniqueNameValidationParams {
elementType: ElementType;
activeDirectory?: string;
elementExists?: (directory: UUID, name: string, type: ElementType) => Promise<boolean>;
isPrefilled?: boolean;
}

export function useUniqueNameValidation({
Expand All @@ -26,6 +27,7 @@ export function useUniqueNameValidation({
elementType,
activeDirectory,
elementExists = elementAlreadyExists,
isPrefilled = false,
}: UseUniqueNameValidationParams) {
const {
setError,
Expand All @@ -36,6 +38,7 @@ export function useUniqueNameValidation({

const {
field: { value },
fieldState: { isDirty: fieldIsDirty },
} = useController({ name });

const {
Expand Down Expand Up @@ -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', {
Expand All @@ -120,7 +129,9 @@ export function useUniqueNameValidation({
setError,
clearErrors,
name,
fieldIsDirty,
formIsDirty,
isPrefilled,
defaultFieldValue,
directory,
selectedDirectory,
Expand Down
Loading