Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kowczarz committed Dec 19, 2023
1 parent 6b93011 commit 91239d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
26 changes: 8 additions & 18 deletions src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {FormProps, InputRef, InputRefs, InputValues, RegisterInput, ValueType} f
// More details: https://github.com/Expensify/App/pull/16444#issuecomment-1482983426
const VALIDATE_DELAY = 200;

function getInitialValueByType(valueType?: ValueType): false | Date | '' {
type DefaultValue = false | Date | '';

function getInitialValueByType(valueType?: ValueType): DefaultValue {
switch (valueType) {
case 'string':
return '';
Expand Down Expand Up @@ -248,19 +250,15 @@ function FormProvider<TForm extends Form & Record<string, unknown>>(
setTouchedInput(inputID);
}, VALIDATE_DELAY);
}
if (typeof inputProps.onTouched === 'function') {
inputProps.onTouched(event);
}
inputProps.onTouched?.(event);
},
onPress: (event) => {
if (!inputProps.shouldSetTouchedOnBlurOnly) {
setTimeout(() => {
setTouchedInput(inputID);
}, VALIDATE_DELAY);
}
if (typeof inputProps.onPress === 'function') {
inputProps.onPress(event);
}
inputProps.onPress?.(event);
},
onPressOut: (event) => {
// To prevent validating just pressed inputs, we need to set the touched input right after
Expand All @@ -271,9 +269,7 @@ function FormProvider<TForm extends Form & Record<string, unknown>>(
setTouchedInput(inputID);
}, VALIDATE_DELAY);
}
if (typeof inputProps.onPressOut === 'function') {
inputProps.onPressOut(event);
}
inputProps.onPressOut?.(event);
},
onBlur: (event) => {
// Only run validation when user proactively blurs the input.
Expand All @@ -298,10 +294,7 @@ function FormProvider<TForm extends Form & Record<string, unknown>>(
}
}, VALIDATE_DELAY);
}

if (typeof inputProps.onBlur === 'function') {
inputProps.onBlur(event);
}
inputProps.onBlur?.(event);
},
onInputChange: (value, key) => {
const inputKey = key || inputID;
Expand All @@ -320,10 +313,7 @@ function FormProvider<TForm extends Form & Record<string, unknown>>(
if (inputProps.shouldSaveDraft) {
FormActions.setDraftValues(formID, {[inputKey]: value});
}

if (typeof inputProps.onValueChange === 'function') {
inputProps.onValueChange(value, inputKey);
}
inputProps.onValueChange?.(value, inputKey);
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ type InputProps = InputPropsToPass & {

type RegisterInput = (inputID: string, props: InputPropsToPass) => InputProps;

export type {InputWrapperProps, FormProps, InputRef, InputRefs, RegisterInput, ValueType, InputValues, InputProps};
export type {InputWrapperProps, FormProps, InputRef, InputRefs, RegisterInput, ValueType, InputValues, InputProps, FormID};

0 comments on commit 91239d5

Please sign in to comment.