imaskjs icon indicating copy to clipboard operation
imaskjs copied to clipboard

Element focus function not focus correctly

Open Bulldozer323 opened this issue 3 years ago • 2 comments

Hello, on mobile device (ios 15.0.2) focus function on element not show keyboard.

Package version – react-imask 6.2.2

To Reproduce: Open from mobile device – https://codesandbox.io/s/little-thunder-rwkkt?file=/src/App.js and refresh sandbox window if needed, input has active but keyboard not showing.

Code:

import { IMaskInput } from "react-imask";
import { useEffect, useRef } from "react";

export default function App() {
  const inputRef = useRef(null);

  useEffect(() => {
    if (inputRef.current && inputRef.current.element) {
      inputRef.current.element.focus();
    }
  }, [inputRef]);

  return (
    <div className="App">
      <IMaskInput unmask value="123" mask={Number} ref={inputRef} />
    </div>
  );
}

Bulldozer323 avatar Nov 07 '21 18:11 Bulldozer323

Hello, I'm having the same problem.

If you focus the input with element.focus() it wont behave as it should but if you click it its fine.

AdrianoSilvaACIN avatar Mar 07 '23 12:03 AdrianoSilvaACIN

You'll have to try this to be sure, but looking at the latest documentation IMaskInput now can now take a ref and an inputRef. The inputRef is the ref to the inner input itself, whereas ref is a ref to the mask itself. Also, when that ref is applied the inputRef.current is the reference to the actual element, so your code would become:

import { IMaskInput } from "react-imask";
import { useEffect, useRef } from "react";

export default function App() {
  const inputRef = useRef(null);

  useEffect(() => {
      inputRef?.current?.focus();
  }, [inputRef]);

  return (
    <div className="App">
      <IMaskInput unmask value="123" mask={Number} inputRef={inputRef} />
    </div>
  );
}

cutterbl avatar Mar 18 '24 14:03 cutterbl