Skip to content

Fix/ TextField (incubator) validation message on start #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2021
Merged
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
2 changes: 1 addition & 1 deletion generatedTypes/src/incubator/TextField/Presenter.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FieldContextType } from './FieldContext';
import { ColorType, Validator } from './types';
export declare function getColorByState(color?: ColorType, context?: FieldContextType): string | undefined;
export declare function validate(value?: string, validator?: Validator | Validator[], validationMessage?: string | string[]): [boolean, number?];
export declare function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?];
export declare function getRelevantValidationMessage(validationMessage: string | string[] | undefined, failingValidatorIndex: undefined | number): string | string[] | undefined;
6 changes: 2 additions & 4 deletions src/incubator/TextField/Presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ export function getColorByState(color?: ColorType, context?: FieldContextType) {
return finalColor;
}

export function validate(value?: string,
validator?: Validator | Validator[],
validationMessage?: string | string[]): [boolean, number?] {
export function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?] {
if (_.isUndefined(validator)) {
return [!validationMessage, undefined];
return [true, undefined];
}

let _isValid = true;
Expand Down
9 changes: 2 additions & 7 deletions src/incubator/TextField/__tests__/Presenter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import * as uut from '../Presenter';

describe('TextField:Presenter', () => {
describe('validate', () => {
it('should return true if validator and validationMessage are undefined', () => {
it('should return true if validator is undefined', () => {
expect(uut.validate('value', undefined)).toEqual([true, undefined]);
expect(uut.validate('value', undefined, undefined)).toEqual([true, undefined]);
});

it('should return false if validator is undefined and there is a validationMessage', () => {
expect(uut.validate('value', undefined, 'Error!')).toEqual([false, undefined]);
});

it('should validate email', () => {
Expand Down Expand Up @@ -49,7 +44,7 @@ describe('TextField:Presenter', () => {
it('should return undefined when there is no validationMessage', () => {
expect(uut.getRelevantValidationMessage(undefined, 0)).toBeUndefined();
});

it('should return the validation message when there is no validate method', () => {
expect(uut.getRelevantValidationMessage('error message', undefined)).toBe('error message');
});
Expand Down
4 changes: 2 additions & 2 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const TextField = (props: InternalTextFieldProps) => {
<ValidationMessage
enableErrors={enableErrors}
validate={others.validate}
validationMessage={props.validationMessage}
validationMessage={others.validationMessage}
validationMessageStyle={validationMessageStyle}
/>
)}
Expand Down Expand Up @@ -209,7 +209,7 @@ const TextField = (props: InternalTextFieldProps) => {
<ValidationMessage
enableErrors={enableErrors}
validate={others.validate}
validationMessage={props.validationMessage}
validationMessage={others.validationMessage}
validationMessageStyle={validationMessageStyle}
retainSpace
/>
Expand Down
14 changes: 10 additions & 4 deletions src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export default function useFieldState({
}, [isValid]);

const validateField = useCallback((valueToValidate = value) => {
const [_isValid, _failingValidatorIndex] = Presenter.validate(valueToValidate, validate, validationMessage);
const [_isValid, _failingValidatorIndex] = Presenter.validate(valueToValidate, validate);

setIsValid(_isValid);
setFailingValidatorIndex(_failingValidatorIndex);
},
[value, validate, validationMessage]);
[value, validate]);

const onFocus = useCallback((...args: any) => {
setIsFocused(true);
Expand Down Expand Up @@ -95,8 +95,14 @@ export default function useFieldState({
[props.onChangeText, validateOnChange, validateField]);

const fieldState = useMemo(() => {
return {value, hasValue: !_.isEmpty(value), isValid, isFocused, failingValidatorIndex};
}, [value, isFocused, isValid, failingValidatorIndex]);
return {
value,
hasValue: !_.isEmpty(value),
isValid: validationMessage && !validate ? false : isValid,
isFocused,
failingValidatorIndex
};
}, [value, isFocused, isValid, failingValidatorIndex, validationMessage, validate]);

return {
onFocus,
Expand Down