Skip to content

Fix/ TextField (Incubator) Validation #1590

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 3 commits into from
Oct 12, 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[]): [boolean, number?];
export declare function validate(value?: string, validator?: Validator | Validator[], validationMessage?: string | string[]): [boolean, number?];
export declare function getRelevantValidationMessage(validationMessage: string | string[] | undefined, failingValidatorIndex: undefined | number): string | string[] | undefined;
6 changes: 5 additions & 1 deletion generatedTypes/src/incubator/TextField/useFieldState.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export interface FieldStateProps extends InputProps {
* A single or multiple validator. Can be a string (required, email) or custom function.
*/
validate?: Validator | Validator[];
/**
* The validation message to display when field is invalid (depends on validate)
*/
validationMessage?: string | string[];
/**
* Callback for when field validity has changed
*/
onChangeValidity?: (isValid: boolean) => void;
}
export default function useFieldState({ validate, validateOnBlur, validateOnChange, validateOnStart, onChangeValidity, ...props }: FieldStateProps): {
export default function useFieldState({ validate, validationMessage, validateOnBlur, validateOnChange, validateOnStart, onChangeValidity, ...props }: FieldStateProps): {
onFocus: (...args: any) => void;
onBlur: (...args: any) => void;
onChangeText: (text: any) => void;
Expand Down
6 changes: 4 additions & 2 deletions src/incubator/TextField/Presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export function getColorByState(color?: ColorType, context?: FieldContextType) {
return finalColor;
}

export function validate(value?: string, validator?: Validator | Validator[]): [boolean, number?] {
export function validate(value?: string,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests to check this new scenario

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

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

describe('TextField:Presenter', () => {
describe('validate', () => {
it('should return true if validator is undefined', () => {
it('should return true if validator and validationMessage are 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
5 changes: 2 additions & 3 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ const TextField = (props: InternalTextFieldProps) => {
trailingAccessory,
// Validation
enableErrors, // TODO: rename to enableValidation
validationMessage,
validationMessageStyle,
validationMessagePosition = ValidationMessagePosition.BOTTOM,
// Char Counter
Expand Down Expand Up @@ -172,7 +171,7 @@ const TextField = (props: InternalTextFieldProps) => {
<ValidationMessage
enableErrors={enableErrors}
validate={others.validate}
validationMessage={validationMessage}
validationMessage={props.validationMessage}
validationMessageStyle={validationMessageStyle}
/>
)}
Expand Down Expand Up @@ -210,7 +209,7 @@ const TextField = (props: InternalTextFieldProps) => {
<ValidationMessage
enableErrors={enableErrors}
validate={others.validate}
validationMessage={validationMessage}
validationMessage={props.validationMessage}
validationMessageStyle={validationMessageStyle}
retainSpace
/>
Expand Down
12 changes: 8 additions & 4 deletions src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface FieldStateProps extends InputProps {
* A single or multiple validator. Can be a string (required, email) or custom function.
*/
validate?: Validator | Validator[];
/**
* The validation message to display when field is invalid (depends on validate)
*/
validationMessage?: string | string[];
/**
* Callback for when field validity has changed
*/
Expand All @@ -21,6 +25,7 @@ export interface FieldStateProps extends InputProps {

export default function useFieldState({
validate,
validationMessage,
validateOnBlur,
validateOnChange,
validateOnStart,
Expand All @@ -40,9 +45,8 @@ export default function useFieldState({

useEffect(() => {
if (props.value !== value) {

setValue(props.value);

if (validateOnChange) {
validateField(props.value);
}
Expand All @@ -56,12 +60,12 @@ export default function useFieldState({
}, [isValid]);

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

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

const onFocus = useCallback((...args: any) => {
setIsFocused(true);
Expand Down