1

I am trying to implement Input Mask with children as input component, I used https://github.com/sanniassin/react-input-mask. The problem is while onChange() event triggers setState() is called and the state is updated. While updating the state the component is re-rendered. So the state value is also updated and cursor moved to the end of the input value.I am expecting it should stayed on changing position. Here is my code

import InputMask from 'react-input-mask';

const [state, setState] = useState<StateTypes>({
    myVal: '123.123.123-56'
});

const onChangeValue = (inputVal: any) => {
    setState({
      myVal: inputVal
});
};

<InputMask mask="999.999.999-99" onChange={onChangeValue} value={state.myVal}>
 <Input name={'myVal'} />
</InputMask>

After setting the state input component is re-rendered, so cursor point moved to the end. Can someone help me out.

Thanks

1
  • Found any solution to this? I encountered this issue too. Commented Sep 9, 2021 at 12:17

1 Answer 1

0
 try {
            window.document.getElementById("expiryDtId").focus();
          } catch (error) {
            console.error(error);
          }

got solution by using id and javascript

  <InputMask
        mask="99/99/9999" 

        id={"expiryDtId"}
/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.