Skip to content

Commit e57b534

Browse files
authored
Add missing functionality for Incubator.TextField (#1121)
1 parent 1d0fd8c commit e57b534

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

demo/src/screens/componentScreens/IncubatorTextFieldScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export default class TextFieldScreen extends Component {
146146
validationMessagePosition={errorPosition}
147147
validate={['required', 'email']}
148148
validateOnChange
149+
onChangeValidity={(isValid: boolean) => console.warn('validity changed:', isValid, Date.now())}
149150
// validateOnStart
150151
// validateOnBlur
151152
fieldStyle={styles.withUnderline}

generatedTypes/incubator/TextField/index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export declare type TextFieldProps = MarginModifiers & PaddingModifiers & Typogr
4747
* Should validate when losing focus of TextField
4848
*/
4949
validateOnBlur?: boolean;
50+
/**
51+
* Callback for when field validity has changed
52+
*/
53+
onChangeValidity?: (isValid: boolean) => void;
5054
/**
5155
* The position of the validation message (top/bottom)
5256
*/
@@ -101,6 +105,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
101105
* Should validate when losing focus of TextField
102106
*/
103107
validateOnBlur?: boolean | undefined;
108+
/**
109+
* Callback for when field validity has changed
110+
*/
111+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
104112
/**
105113
* The position of the validation message (top/bottom)
106114
*/
@@ -152,6 +160,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
152160
* Should validate when losing focus of TextField
153161
*/
154162
validateOnBlur?: boolean | undefined;
163+
/**
164+
* Callback for when field validity has changed
165+
*/
166+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
155167
/**
156168
* The position of the validation message (top/bottom)
157169
*/
@@ -203,6 +215,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
203215
* Should validate when losing focus of TextField
204216
*/
205217
validateOnBlur?: boolean | undefined;
218+
/**
219+
* Callback for when field validity has changed
220+
*/
221+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
206222
/**
207223
* The position of the validation message (top/bottom)
208224
*/
@@ -254,6 +270,10 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
254270
* Should validate when losing focus of TextField
255271
*/
256272
validateOnBlur?: boolean | undefined;
273+
/**
274+
* Callback for when field validity has changed
275+
*/
276+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
257277
/**
258278
* The position of the validation message (top/bottom)
259279
*/

generatedTypes/incubator/TextField/useFieldState.d.ts

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

generatedTypes/incubator/TextField/usePreset.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
204204
validateOnStart?: boolean | undefined;
205205
validateOnChange?: boolean | undefined;
206206
validateOnBlur?: boolean | undefined;
207+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
207208
fieldStyle?: import("react-native").ViewStyle | undefined;
208209
containerStyle?: import("react-native").ViewStyle | undefined;
209210
modifiers: import("../../commons/modifiers").ExtractedStyle;
@@ -581,6 +582,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
581582
validateOnStart?: boolean | undefined;
582583
validateOnChange?: boolean | undefined;
583584
validateOnBlur?: boolean | undefined;
585+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
584586
fieldStyle?: import("react-native").ViewStyle | undefined;
585587
containerStyle?: import("react-native").ViewStyle | undefined;
586588
modifiers: import("../../commons/modifiers").ExtractedStyle;
@@ -1078,6 +1080,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
10781080
validateOnStart?: boolean | undefined;
10791081
validateOnChange?: boolean | undefined;
10801082
validateOnBlur: boolean;
1083+
onChangeValidity?: ((isValid: boolean) => void) | undefined;
10811084
fieldStyle: import("react-native").ViewStyle | {
10821085
borderBottomWidth: number;
10831086
borderBottomColor: string;

src/incubator/TextField/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type TextFieldProps = MarginModifiers &
3636
InputProps &
3737
LabelProps &
3838
FloatingPlaceholderProps &
39-
// We're declaring these props explicitly here for react-docgen
39+
// We're declaring these props explicitly here for react-docgen (which can't read hooks)
4040
// FieldStateProps &
4141
ValidationMessageProps &
4242
Omit<CharCounterProps, 'maxLength'> & {
@@ -72,6 +72,10 @@ export type TextFieldProps = MarginModifiers &
7272
* Should validate when losing focus of TextField
7373
*/
7474
validateOnBlur?: boolean;
75+
/**
76+
* Callback for when field validity has changed
77+
*/
78+
onChangeValidity?: (isValid: boolean) => void;
7579
/**
7680
* The position of the validation message (top/bottom)
7781
*/

src/incubator/TextField/useFieldState.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {useCallback, useState, useEffect, useMemo} from 'react';
22
import {TextInputProps} from 'react-native';
33
import _ from 'lodash';
44
import * as Presenter from './Presenter';
5+
import {useDidUpdate} from '../../hooks';
56
import {Validator} from './types';
67

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

1723
export default function useFieldState({
1824
validate,
1925
validateOnBlur,
2026
validateOnChange,
2127
validateOnStart,
28+
onChangeValidity,
2229
...props
2330
}: FieldStateProps) {
2431
const [value, setValue] = useState(props.value);
@@ -32,6 +39,10 @@ export default function useFieldState({
3239
}
3340
}, []);
3441

42+
useDidUpdate(() => {
43+
onChangeValidity?.(isValid);
44+
}, [isValid]);
45+
3546
const validateField = useCallback((valueToValidate = value) => {
3647
const [_isValid, _failingValidatorIndex] = Presenter.validate(valueToValidate, validate);
3748

0 commit comments

Comments
 (0)