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

How to use onKeyDown? #352

Open
AnkitDhoot opened this issue Jul 13, 2022 · 4 comments
Open

How to use onKeyDown? #352

AnkitDhoot opened this issue Jul 13, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@AnkitDhoot
Copy link

How can I use onKeyDown feature in this component for submitting the OTP?

@devfolioco devfolioco deleted a comment from ritikbanger Mar 23, 2023
@prateek3255
Copy link
Collaborator

onKeyDown event is used by the library for performing certain actions with the inputs. Although you can open a PR to also call the onKeyDown passed by the consumer with the library's onKeyDown

@kahsing
Copy link

kahsing commented Apr 25, 2023

My encounter:
Firefox will still allow alphabets even with inputType set to number.

It wouldn't be the best solution, however a workaround for me to override the package's keyDown function:

  • to disabled invalid character
  • to apply logic used by the package.

export const blockInvalidChar = (e, keydownFunction) => {
  if (e.key.match(/^[^\n]{1}$/)) {  // Matching single character
    ['e', 'E', '+', '-'].includes(e.key) && e.preventDefault(); // Prevent number processors
    e.key.match(/^[a-zA-Z]|[^\w\s]$/) && e.preventDefault(); // Prevent alphabets & symbol
  }
  keydownFunction(e);  // Execute package's onKeydown function
};

....
<OtpInput
          value={otp}
          onChange={setOtp}
          numInputs={6}
          renderSeparator={false}
          shouldAutoFocus={true}
          inputType={'number'}
          renderInput={(props) => (
            <input
              {...props}
              type="number"
              pattern={'/[^0-9]/g'}
              className={`otp-input ${FAStyles.optInput}`}
              onKeyDown={(e) => blockInvalidChar(e, props.onKeyDown)}
            />
          )}

@prateek3255
Copy link
Collaborator

Hey @kahsing the firefox issue has been fixed with v3.0.2 check #393 for more info, and you should not override the the onKeyDown prop because it will lead to unexpected behaviours

@kahsing
Copy link

kahsing commented May 2, 2023

Cool. @prateek3255 It would be nice without have to tempered the used onKeyDown explicitly. Thanks!

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

No branches or pull requests

3 participants