Skip to content

Commit

Permalink
Fix/ TextField (incubator) validation message on start (#1613)
Browse files Browse the repository at this point in the history
* fix vadliation message on start

* make validationMessage controlled by the user
  • Loading branch information
lidord-wix authored Oct 20, 2021
1 parent cabfbe2 commit 897689a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
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

0 comments on commit 897689a

Please sign in to comment.