Skip to content

TextField - Support isValid ref method #2049

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
May 17, 2022
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
3 changes: 2 additions & 1 deletion src/incubator/TextField/FieldContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const FieldContext = createContext<FieldContextType>({
isValid: true,
failingValidatorIndex: undefined,
disabled: false,
validateField: _.noop
validateField: _.noop,
checkValidity: () => true
});

export default FieldContext;
6 changes: 3 additions & 3 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ const TextField = (props: InternalTextFieldProps) => {
...others
} = usePreset(props);
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
const {onFocus, onBlur, onChangeText, fieldState, validateField} = useFieldState(others);
const {onFocus, onBlur, onChangeText, fieldState, validateField, checkValidity} = useFieldState(others);

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

const leadingAccessoryClone = useMemo(() => {
if (leadingAccessory) {
Expand Down
2 changes: 2 additions & 0 deletions src/incubator/TextField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export type FieldContextType = {
failingValidatorIndex?: number;
disabled: boolean;
validateField: () => void;
checkValidity: () => boolean;
};

export interface TextFieldMethods {
Expand All @@ -234,4 +235,5 @@ export interface TextFieldMethods {
blur: () => void;
clear: () => void;
validate: () => boolean;
isValid: () => boolean;
}
8 changes: 7 additions & 1 deletion src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export default function useFieldState({
onChangeValidity?.(isValid);
}, [isValid]);

const checkValidity = useCallback((valueToValidate = value) => {
const [_isValid] = Presenter.validate(valueToValidate, validate);
return _isValid;
}, [value, validate]);

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

Expand Down Expand Up @@ -92,6 +97,7 @@ export default function useFieldState({
onBlur,
onChangeText,
fieldState,
validateField
validateField,
checkValidity
};
}
4 changes: 4 additions & 0 deletions src/incubator/TextField/useImperativeInputHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const useImperativeInputHandle = (ref: React.Ref<any>, props: Pick<TextInputProp
},
validate: () => {
return context.validateField();
},
// Note: This returns field validity without actually validating it
isValid: () => {
return context.checkValidity();
}
};
});
Expand Down