Skip to content

Commit 59e5131

Browse files
authored
Fix/ TextField (Incubator) Validation (#1590)
* set isValid to false if there's validationMessage without validator * test validate's validationMessage param * update validate method
1 parent abad74f commit 59e5131

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
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[]): [boolean, number?];
4+
export declare function validate(value?: string, validator?: Validator | Validator[], validationMessage?: string | string[]): [boolean, number?];
55
export declare function getRelevantValidationMessage(validationMessage: string | string[] | undefined, failingValidatorIndex: undefined | number): string | string[] | undefined;

generatedTypes/src/incubator/TextField/useFieldState.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ export interface FieldStateProps extends InputProps {
88
* A single or multiple validator. Can be a string (required, email) or custom function.
99
*/
1010
validate?: Validator | Validator[];
11+
/**
12+
* The validation message to display when field is invalid (depends on validate)
13+
*/
14+
validationMessage?: string | string[];
1115
/**
1216
* Callback for when field validity has changed
1317
*/
1418
onChangeValidity?: (isValid: boolean) => void;
1519
}
16-
export default function useFieldState({ validate, validateOnBlur, validateOnChange, validateOnStart, onChangeValidity, ...props }: FieldStateProps): {
20+
export default function useFieldState({ validate, validationMessage, validateOnBlur, validateOnChange, validateOnStart, onChangeValidity, ...props }: FieldStateProps): {
1721
onFocus: (...args: any) => void;
1822
onBlur: (...args: any) => void;
1923
onChangeText: (text: any) => void;

src/incubator/TextField/Presenter.ts

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

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

3133
let _isValid = true;

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

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

33
describe('TextField:Presenter', () => {
44
describe('validate', () => {
5-
it('should return true if validator is undefined', () => {
5+
it('should return true if validator and validationMessage are 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]);
712
});
813

914
it('should validate email', () => {

src/incubator/TextField/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ const TextField = (props: InternalTextFieldProps) => {
134134
trailingAccessory,
135135
// Validation
136136
enableErrors, // TODO: rename to enableValidation
137-
validationMessage,
138137
validationMessageStyle,
139138
validationMessagePosition = ValidationMessagePosition.BOTTOM,
140139
// Char Counter
@@ -172,7 +171,7 @@ const TextField = (props: InternalTextFieldProps) => {
172171
<ValidationMessage
173172
enableErrors={enableErrors}
174173
validate={others.validate}
175-
validationMessage={validationMessage}
174+
validationMessage={props.validationMessage}
176175
validationMessageStyle={validationMessageStyle}
177176
/>
178177
)}
@@ -210,7 +209,7 @@ const TextField = (props: InternalTextFieldProps) => {
210209
<ValidationMessage
211210
enableErrors={enableErrors}
212211
validate={others.validate}
213-
validationMessage={validationMessage}
212+
validationMessage={props.validationMessage}
214213
validationMessageStyle={validationMessageStyle}
215214
retainSpace
216215
/>

src/incubator/TextField/useFieldState.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export interface FieldStateProps extends InputProps {
1313
* A single or multiple validator. Can be a string (required, email) or custom function.
1414
*/
1515
validate?: Validator | Validator[];
16+
/**
17+
* The validation message to display when field is invalid (depends on validate)
18+
*/
19+
validationMessage?: string | string[];
1620
/**
1721
* Callback for when field validity has changed
1822
*/
@@ -21,6 +25,7 @@ export interface FieldStateProps extends InputProps {
2125

2226
export default function useFieldState({
2327
validate,
28+
validationMessage,
2429
validateOnBlur,
2530
validateOnChange,
2631
validateOnStart,
@@ -40,9 +45,8 @@ export default function useFieldState({
4045

4146
useEffect(() => {
4247
if (props.value !== value) {
43-
4448
setValue(props.value);
45-
49+
4650
if (validateOnChange) {
4751
validateField(props.value);
4852
}
@@ -56,12 +60,12 @@ export default function useFieldState({
5660
}, [isValid]);
5761

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

6165
setIsValid(_isValid);
6266
setFailingValidatorIndex(_failingValidatorIndex);
6367
},
64-
[value, validate]);
68+
[value, validate, validationMessage]);
6569

6670
const onFocus = useCallback((...args: any) => {
6771
setIsFocused(true);

0 commit comments

Comments
 (0)