Skip to content

Commit 897689a

Browse files
authored
Fix/ TextField (incubator) validation message on start (#1613)
* fix vadliation message on start * make validationMessage controlled by the user
1 parent cabfbe2 commit 897689a

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FieldContextType } from './FieldContext';
22
import { ColorType, Validator } from './types';
33
export declare function getColorByState(color?: ColorType, context?: FieldContextType): string | undefined;
4-
export declare function validate(value?: string, validator?: Validator | Validator[], validationMessage?: string | string[]): [boolean, number?];
4+
export declare function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?];
55
export declare function getRelevantValidationMessage(validationMessage: string | string[] | undefined, failingValidatorIndex: undefined | number): string | string[] | undefined;

src/incubator/TextField/Presenter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ export function getColorByState(color?: ColorType, context?: FieldContextType) {
2323
return finalColor;
2424
}
2525

26-
export function validate(value?: string,
27-
validator?: Validator | Validator[],
28-
validationMessage?: string | string[]): [boolean, number?] {
26+
export function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?] {
2927
if (_.isUndefined(validator)) {
30-
return [!validationMessage, undefined];
28+
return [true, undefined];
3129
}
3230

3331
let _isValid = true;

src/incubator/TextField/__tests__/Presenter.spec.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ import * as uut from '../Presenter';
22

33
describe('TextField:Presenter', () => {
44
describe('validate', () => {
5-
it('should return true if validator and validationMessage are undefined', () => {
5+
it('should return true if validator is undefined', () => {
66
expect(uut.validate('value', undefined)).toEqual([true, undefined]);
7-
expect(uut.validate('value', undefined, undefined)).toEqual([true, undefined]);
8-
});
9-
10-
it('should return false if validator is undefined and there is a validationMessage', () => {
11-
expect(uut.validate('value', undefined, 'Error!')).toEqual([false, undefined]);
127
});
138

149
it('should validate email', () => {
@@ -49,7 +44,7 @@ describe('TextField:Presenter', () => {
4944
it('should return undefined when there is no validationMessage', () => {
5045
expect(uut.getRelevantValidationMessage(undefined, 0)).toBeUndefined();
5146
});
52-
47+
5348
it('should return the validation message when there is no validate method', () => {
5449
expect(uut.getRelevantValidationMessage('error message', undefined)).toBe('error message');
5550
});

src/incubator/TextField/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const TextField = (props: InternalTextFieldProps) => {
171171
<ValidationMessage
172172
enableErrors={enableErrors}
173173
validate={others.validate}
174-
validationMessage={props.validationMessage}
174+
validationMessage={others.validationMessage}
175175
validationMessageStyle={validationMessageStyle}
176176
/>
177177
)}
@@ -209,7 +209,7 @@ const TextField = (props: InternalTextFieldProps) => {
209209
<ValidationMessage
210210
enableErrors={enableErrors}
211211
validate={others.validate}
212-
validationMessage={props.validationMessage}
212+
validationMessage={others.validationMessage}
213213
validationMessageStyle={validationMessageStyle}
214214
retainSpace
215215
/>

src/incubator/TextField/useFieldState.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export default function useFieldState({
6060
}, [isValid]);
6161

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

6565
setIsValid(_isValid);
6666
setFailingValidatorIndex(_failingValidatorIndex);
6767
},
68-
[value, validate, validationMessage]);
68+
[value, validate]);
6969

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

9797
const fieldState = useMemo(() => {
98-
return {value, hasValue: !_.isEmpty(value), isValid, isFocused, failingValidatorIndex};
99-
}, [value, isFocused, isValid, failingValidatorIndex]);
98+
return {
99+
value,
100+
hasValue: !_.isEmpty(value),
101+
isValid: validationMessage && !validate ? false : isValid,
102+
isFocused,
103+
failingValidatorIndex
104+
};
105+
}, [value, isFocused, isValid, failingValidatorIndex, validationMessage, validate]);
100106

101107
return {
102108
onFocus,

0 commit comments

Comments
 (0)