-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(zimbra): required form field has error on init
ref: MANAGER-16496 Signed-off-by: Tristan WAGNER <[email protected]>
- Loading branch information
1 parent
2a46bbb
commit f2e127f
Showing
11 changed files
with
227 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useState } from 'react'; | ||
|
||
export type FieldType = { | ||
value: string; | ||
touched?: boolean; | ||
defaultValue?: string; | ||
hasError?: boolean; | ||
required?: boolean; | ||
validate?: ((value: string) => boolean) | RegExp; | ||
}; | ||
|
||
export interface FormTypeInterface { | ||
[key: string]: FieldType; | ||
} | ||
|
||
export const validateField = ( | ||
name: string, | ||
value: string, | ||
form: FormTypeInterface, | ||
) => { | ||
const field = form[name]; | ||
|
||
if (!field) { | ||
throw new Error(`validateField field is not defined for name "${name}"`); | ||
} | ||
|
||
if (!field.required && !value) { | ||
return true; | ||
} | ||
|
||
if (typeof field.validate === 'function') { | ||
return field.validate(value); | ||
} | ||
|
||
if (field.validate instanceof RegExp) { | ||
return field.validate.test(String(value)); | ||
} | ||
|
||
return !field.required || !!value; | ||
}; | ||
|
||
export const validateForm = (form: FormTypeInterface) => { | ||
const touched = Object.values(form).find((field) => field.touched); | ||
const error = Object.values(form).find( | ||
(field) => field.hasError || (field.required && field.value === ''), | ||
); | ||
return touched && !error; | ||
}; | ||
|
||
type FormTypeOptions = { | ||
onValueChange?: ( | ||
state: FormTypeInterface, | ||
name: string, | ||
value: string, | ||
) => FormTypeInterface; | ||
}; | ||
|
||
export const useForm = ( | ||
initialForm: FormTypeInterface = {}, | ||
options: FormTypeOptions = {}, | ||
) => { | ||
const [isFormValid, setIsFormValid] = useState(false); | ||
const [form, setForm] = useState<FormTypeInterface>(initialForm); | ||
|
||
const setValue = (name: string, value: string, isBlur = false) => { | ||
let newForm = form; | ||
if (value !== form[name].value || isBlur) { | ||
newForm[name] = { | ||
...form[name], | ||
value, | ||
touched: true, | ||
hasError: !validateField(name, value, form), | ||
}; | ||
if (typeof options?.onValueChange === 'function') { | ||
newForm = options.onValueChange(newForm, name, value); | ||
} | ||
setForm((oldForm) => ({ ...oldForm, ...newForm })); | ||
setIsFormValid(validateForm(form)); | ||
} | ||
}; | ||
return { isFormValid, form, setValue, setForm }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.