Skip to content

Commit a63abfa

Browse files
authored
add isPrefilled arg (#859)
Normally, the nameEmpty error is not displayed when the field is empty. However, in the specific case where the field is automatically populated when a source file is selected, the nameEmpty error should be shown if the field is cleared after auto-completion. This behavior is controlled by the isPrefilled argument. Put it to true in the component calling use-unique-name-validation to activate this logic. (Not: currently, the validation checks whether the entire form is dirty, rather than just tracking whether the file selection (which triggers auto-completion) has changed.)
1 parent e0eecd5 commit a63abfa

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/components/inputs/reactHookForm/text/UniqueNameInput.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface UniqueNameInputProps {
2727
>;
2828
activeDirectory?: UUID;
2929
currentName?: string;
30+
isPrefilled?: boolean;
3031
sx?: SxStyle;
3132
fullWidth?: boolean;
3233
}
@@ -42,6 +43,7 @@ export function UniqueNameInput({
4243
onManualChangeCallback,
4344
formProps,
4445
currentName = '',
46+
isPrefilled = false,
4547
activeDirectory,
4648
sx,
4749
fullWidth = true,
@@ -58,6 +60,7 @@ export function UniqueNameInput({
5860
currentName,
5961
elementType,
6062
activeDirectory,
63+
isPrefilled,
6164
});
6265

6366
// Handle on user's change

src/hooks/use-unique-name-validation.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface UseUniqueNameValidationParams {
1818
elementType: ElementType;
1919
activeDirectory?: string;
2020
elementExists?: (directory: UUID, name: string, type: ElementType) => Promise<boolean>;
21+
isPrefilled?: boolean;
2122
}
2223

2324
export function useUniqueNameValidation({
@@ -26,6 +27,7 @@ export function useUniqueNameValidation({
2627
elementType,
2728
activeDirectory,
2829
elementExists = elementAlreadyExists,
30+
isPrefilled = false,
2931
}: UseUniqueNameValidationParams) {
3032
const {
3133
setError,
@@ -36,6 +38,7 @@ export function useUniqueNameValidation({
3638

3739
const {
3840
field: { value },
41+
fieldState: { isDirty: fieldIsDirty },
3942
} = useController({ name });
4043

4144
const {
@@ -92,14 +95,20 @@ export function useUniqueNameValidation({
9295
debouncedHandleCheckName(trimmedValue);
9396
}
9497

95-
// if the form is unchanged, we don't do custom validation
96-
if (!formIsDirty) {
98+
// if the name is unchanged, we don't do custom validation
99+
if (!isPrefilled && !fieldIsDirty) {
97100
clearErrors(name);
98101
return;
99102
}
100-
if (trimmedValue === defaultFieldValue && trimmedValue.length > 0) {
103+
if (isPrefilled && !formIsDirty) {
104+
clearErrors(name);
101105
return;
102106
}
107+
108+
if (isPrefilled && trimmedValue === defaultFieldValue && trimmedValue.length > 0) {
109+
return;
110+
}
111+
103112
if (trimmedValue) {
104113
clearErrors(name);
105114
setError('root.isValidating', {
@@ -120,7 +129,9 @@ export function useUniqueNameValidation({
120129
setError,
121130
clearErrors,
122131
name,
132+
fieldIsDirty,
123133
formIsDirty,
134+
isPrefilled,
124135
defaultFieldValue,
125136
directory,
126137
selectedDirectory,

0 commit comments

Comments
 (0)