Skip to content

Add missing functionality for Incubator.TextField #1121

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 1 commit into from
Jan 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default class TextFieldScreen extends Component {
validationMessagePosition={errorPosition}
validate={['required', 'email']}
validateOnChange
onChangeValidity={(isValid: boolean) => console.warn('validity changed:', isValid, Date.now())}
// validateOnStart
// validateOnBlur
fieldStyle={styles.withUnderline}
Expand Down
20 changes: 20 additions & 0 deletions generatedTypes/incubator/TextField/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export declare type TextFieldProps = MarginModifiers & PaddingModifiers & Typogr
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean;
/**
* Callback for when field validity has changed
*/
onChangeValidity?: (isValid: boolean) => void;
/**
* The position of the validation message (top/bottom)
*/
Expand Down Expand Up @@ -101,6 +105,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean | undefined;
/**
* Callback for when field validity has changed
*/
onChangeValidity?: ((isValid: boolean) => void) | undefined;
/**
* The position of the validation message (top/bottom)
*/
Expand Down Expand Up @@ -152,6 +160,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean | undefined;
/**
* Callback for when field validity has changed
*/
onChangeValidity?: ((isValid: boolean) => void) | undefined;
/**
* The position of the validation message (top/bottom)
*/
Expand Down Expand Up @@ -203,6 +215,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean | undefined;
/**
* Callback for when field validity has changed
*/
onChangeValidity?: ((isValid: boolean) => void) | undefined;
/**
* The position of the validation message (top/bottom)
*/
Expand Down Expand Up @@ -254,6 +270,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean | undefined;
/**
* Callback for when field validity has changed
*/
onChangeValidity?: ((isValid: boolean) => void) | undefined;
/**
* The position of the validation message (top/bottom)
*/
Expand Down
6 changes: 5 additions & 1 deletion generatedTypes/incubator/TextField/useFieldState.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ export interface FieldStateProps extends TextInputProps {
* A single or multiple validator. Can be a string (required, email) or custom function.
*/
validate?: Validator | Validator[];
/**
* Callback for when field validity has changed
*/
onChangeValidity?: (isValid: boolean) => void;
}
export default function useFieldState({ validate, validateOnBlur, validateOnChange, validateOnStart, ...props }: FieldStateProps): {
export default function useFieldState({ validate, validateOnBlur, validateOnChange, validateOnStart, onChangeValidity, ...props }: FieldStateProps): {
onFocus: (...args: any) => void;
onBlur: (...args: any) => void;
onChangeText: (text: any) => void;
Expand Down
3 changes: 3 additions & 0 deletions generatedTypes/incubator/TextField/usePreset.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
validateOnStart?: boolean | undefined;
validateOnChange?: boolean | undefined;
validateOnBlur?: boolean | undefined;
onChangeValidity?: ((isValid: boolean) => void) | undefined;
fieldStyle?: import("react-native").ViewStyle | undefined;
containerStyle?: import("react-native").ViewStyle | undefined;
modifiers: import("../../commons/modifiers").ExtractedStyle;
Expand Down Expand Up @@ -581,6 +582,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
validateOnStart?: boolean | undefined;
validateOnChange?: boolean | undefined;
validateOnBlur?: boolean | undefined;
onChangeValidity?: ((isValid: boolean) => void) | undefined;
fieldStyle?: import("react-native").ViewStyle | undefined;
containerStyle?: import("react-native").ViewStyle | undefined;
modifiers: import("../../commons/modifiers").ExtractedStyle;
Expand Down Expand Up @@ -1078,6 +1080,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
validateOnStart?: boolean | undefined;
validateOnChange?: boolean | undefined;
validateOnBlur: boolean;
onChangeValidity?: ((isValid: boolean) => void) | undefined;
fieldStyle: import("react-native").ViewStyle | {
borderBottomWidth: number;
borderBottomColor: string;
Expand Down
6 changes: 5 additions & 1 deletion src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type TextFieldProps = MarginModifiers &
InputProps &
LabelProps &
FloatingPlaceholderProps &
// We're declaring these props explicitly here for react-docgen
// We're declaring these props explicitly here for react-docgen (which can't read hooks)
// FieldStateProps &
ValidationMessageProps &
Omit<CharCounterProps, 'maxLength'> & {
Expand Down Expand Up @@ -72,6 +72,10 @@ export type TextFieldProps = MarginModifiers &
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean;
/**
* Callback for when field validity has changed
*/
onChangeValidity?: (isValid: boolean) => void;
/**
* The position of the validation message (top/bottom)
*/
Expand Down
11 changes: 11 additions & 0 deletions src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {useCallback, useState, useEffect, useMemo} from 'react';
import {TextInputProps} from 'react-native';
import _ from 'lodash';
import * as Presenter from './Presenter';
import {useDidUpdate} from '../../hooks';
import {Validator} from './types';


export interface FieldStateProps extends TextInputProps {
validateOnStart?: boolean;
validateOnChange?: boolean;
Expand All @@ -12,13 +14,18 @@ export interface FieldStateProps extends TextInputProps {
* A single or multiple validator. Can be a string (required, email) or custom function.
*/
validate?: Validator | Validator[];
/**
* Callback for when field validity has changed
*/
onChangeValidity?: (isValid: boolean) => void
}

export default function useFieldState({
validate,
validateOnBlur,
validateOnChange,
validateOnStart,
onChangeValidity,
...props
}: FieldStateProps) {
const [value, setValue] = useState(props.value);
Expand All @@ -32,6 +39,10 @@ export default function useFieldState({
}
}, []);

useDidUpdate(() => {
onChangeValidity?.(isValid);
}, [isValid]);

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

Expand Down