Skip to content

expose an imperative validate method on Incubator.TextField component #1459

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 2 commits into from
Aug 8, 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
22 changes: 22 additions & 0 deletions demo/src/screens/incubatorScreens/IncubatorTextFieldScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {KeyboardAwareInsetsView} = Keyboard;
export default class TextFieldScreen extends Component {
input = React.createRef<TextInput>();
input2 = React.createRef<TextInput>();
inputWithValidation = React.createRef<TextInput>();
state = {
errorPosition: TextField.validationMessagePositions.TOP,
shouldDisable: false,
Expand Down Expand Up @@ -121,6 +122,7 @@ export default class TextFieldScreen extends Component {
<Text h3 blue50>
Validation
</Text>

<Button
size={Button.sizes.xSmall}
label={`Error Position: ${_.upperCase(errorPosition)}`}
Expand Down Expand Up @@ -150,8 +152,28 @@ export default class TextFieldScreen extends Component {
// validateOnStart
// validateOnBlur
fieldStyle={styles.withUnderline}
marginB-s4
/>

<View row top marginT-s4>
<TextField
ref={this.inputWithValidation}
placeholder="Enter full name"
validate="required"
validationMessage="This field is required"
containerStyle={{flexGrow: 1}}
fieldStyle={styles.withUnderline}
/>
<Button
marginL-s5
label="Validate"
size={Button.sizes.xSmall}
onPress={() => {
this.inputWithValidation.current?.validate?.();
}}
/>
</View>

<View row centerV spread>
<Text h3 blue50 marginV-s4>
Colors By State
Expand Down
1 change: 1 addition & 0 deletions generatedTypes/incubator/TextField/FieldContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export declare type FieldContextType = {
isValid: boolean;
failingValidatorIndex?: number;
disabled: boolean;
validateField: () => void;
};
declare const FieldContext: import("react").Context<FieldContextType>;
export default FieldContext;
1 change: 1 addition & 0 deletions generatedTypes/incubator/TextField/useFieldState.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export default function useFieldState({ validate, validateOnBlur, validateOnChan
isFocused: boolean;
failingValidatorIndex: number | undefined;
};
validateField: (valueToValidate?: any) => void;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import { TextInput } from 'react-native';
declare const useImperativeInputHandle: (ref: React.Ref<any>) => React.MutableRefObject<TextInput | undefined>;
export default useImperativeInputHandle;
5 changes: 4 additions & 1 deletion src/incubator/TextField/FieldContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import {createContext} from 'react';

export type FieldContextType = {
Expand All @@ -7,14 +8,16 @@ export type FieldContextType = {
isValid: boolean;
failingValidatorIndex?: number;
disabled: boolean;
validateField: () => void
};

const FieldContext = createContext<FieldContextType>({
isFocused: false,
hasValue: false,
isValid: true,
failingValidatorIndex: undefined,
disabled: false
disabled: false,
validateField: _.noop
});

export default FieldContext;
5 changes: 4 additions & 1 deletion src/incubator/TextField/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {getColorByState} from './Presenter';
import {Colors} from '../../style';
import {Constants} from '../../helpers';
import FieldContext from './FieldContext';
import useImperativeInputHandle from './useImperativeInputHandle';

const DEFAULT_INPUT_COLOR: ColorType = {
default: Colors.grey10,
Expand Down Expand Up @@ -35,6 +36,7 @@ const Input = ({
forwardedRef,
...props
}: InputProps & ForwardRefInjectedProps) => {
const inputRef = useImperativeInputHandle(forwardedRef);
const context = useContext(FieldContext);
const placeholder = !context.isFocused ? props.placeholder : hint || props.placeholder;
const inputColor = getColorByState(color, context);
Expand All @@ -46,7 +48,8 @@ const Input = ({
{...props}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
ref={forwardedRef}
// @ts-expect-error
ref={inputRef}
underlineColorAndroid="transparent"
accessibilityState={{disabled: props.editable === false}}
/>
Expand Down
6 changes: 3 additions & 3 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ const TextField = (props: InternalTextFieldProps) => {
placeholder,
...others
} = usePreset(props);
const {onFocus, onBlur, onChangeText, fieldState} = useFieldState(others);
const {onFocus, onBlur, onChangeText, fieldState, validateField} = useFieldState(others);

const context = useMemo(() => {
return {...fieldState, disabled: others.editable === false};
}, [fieldState, others.editable]);
return {...fieldState, disabled: others.editable === false, validateField};
}, [fieldState, others.editable, validateField]);

const {margins, paddings, typography, color} = modifiers;
const typographyStyle = useMemo(() => omit(typography, 'lineHeight'), [typography]);
Expand Down
3 changes: 2 additions & 1 deletion src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default function useFieldState({
onFocus,
onBlur,
onChangeText,
fieldState
fieldState,
validateField
};
}
22 changes: 22 additions & 0 deletions src/incubator/TextField/useImperativeInputHandle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, {useContext, useImperativeHandle, useRef} from 'react';
import {TextInput} from 'react-native';
import FieldContext from './FieldContext';

const useImperativeInputHandle = (ref: React.Ref<any>) => {
const inputRef = useRef<TextInput>();
const context = useContext(FieldContext);
useImperativeHandle(ref, () => {
return {
focus: () => inputRef.current?.focus(),
blur: () => inputRef.current?.blur(),
clear: () => inputRef.current?.clear(),
validate: () => {
context.validateField();
}
};
});

return inputRef;
};

export default useImperativeInputHandle;