Skip to content
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

Using React Hooks & withNextInputAutoFocus together #109

Open
AsadSaleh opened this issue May 2, 2019 · 4 comments
Open

Using React Hooks & withNextInputAutoFocus together #109

AsadSaleh opened this issue May 2, 2019 · 4 comments

Comments

@AsadSaleh
Copy link

Thanks for this awesome lib, it has been fun all the time to use it. I have a question though on how could i use withNextInputAutoFocus and my functional Hooks component. According to your docs, using withNextInputAutoFocus requires us to have a focus method, with a sole purpose to focus on the element.

The problem occurs when I'am using hooks. As we know that in functional component you can't have a method, on the other hand, because you want to use hooks you cannot use class. So I'am stuck in the middle of both. I really hope I can use Hooks and withNextInputAutoFocus together as I really enjoyed working with both.

@mlabrum
Copy link

mlabrum commented Aug 2, 2019

@appjitsu
Copy link

appjitsu commented Mar 18, 2020

I tried using useImperativeHandle with forwardRef and this module, but cannot get it to work. What it tries to do is submit the entire form instead of just focusing to the next input. What am I missing?

import React, { useRef, useImperativeHandle, forwardRef} from 'react';
import {
  Item,
  Input,
  Label,
  Icon,
} from 'native-base';
import { TextInput as RNTextInput } from 'react-native';
import { withFormikControl } from 'react-native-formik';
import {MaskService, TextInputMaskOptionProp} from 'react-native-masked-text';

import FormError from './FormError';

const TextInput = (props, ref) => {
  const { error, touched, name, value, setFieldValue, label, last, maskType, maskOptions } = props;

  let inputRef = useRef();
  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current.focus();
    }
  }));

  return (
    <>
      <Item
        floatingLabel
        error={error !== undefined}
        success={touched && error === undefined}
      >
        <Label>{label}</Label>
        <Input
          {...props}
          value={value}
          onChangeText={setFieldValue}
          returnKeyType={last ? 'done' : 'next'}
          ref={inputRef}
        />
        {error ? (<Icon danger name='close-circle' />) : <></>}
        {touched && !error ? (<Icon success name='checkmark-circle' />) : <></>}
      </Item>
      <FormError error={error} />
    </>
  );
};

export default withFormikControl(forwardRef(TextInput));

I'm creating my input in the parent form with:

import {
  Form,
} from 'native-base';
import { Formik } from 'formik';
import {
  handleTextInput,
  withNextInputAutoFocusInput,
  withNextInputAutoFocusForm,
  withInputTypeProps,
} from 'react-native-formik';
...
import {
  TextInput,
} from '../common';
...
const ComposedTextInput = compose(
  handleTextInput,
  withNextInputAutoFocusInput,
  withInputTypeProps,
)(TextInput);
const FormView = withNextInputAutoFocusForm(Form);

@v-honcharenko
Copy link

@appjitsu You need to move the withNextInputAutoFocusInput HOC to the end:

const ComposedTextInput = compose(
  handleTextInput,
  withInputTypeProps,
  withNextInputAutoFocusInput, // <- here
)(TextInput);

@mymy47
Copy link

mymy47 commented Aug 14, 2023

This solution worked for me:

import React, { forwardRef } from 'react'
import { TextInput } from 'react-native'
import {
  handleTextInput,
  withInputTypeProps,
  withNextInputAutoFocusInput,
} from 'react-native-formik'
import { compose } from 'recompose'

const CustomTextInput = (props, ref) => {
  return (
    <TextInput {...props} ref={ref}/>
  )
}

export const FormikInput = compose(
  handleTextInput,
  withInputTypeProps,
  withNextInputAutoFocusInput,
)(forwardRef(CustomTextInput))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants