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

Utility for cross-platform input validations #6

Open
baldeepsingh-pantelwar opened this issue Feb 2, 2024 · 0 comments
Open

Utility for cross-platform input validations #6

baldeepsingh-pantelwar opened this issue Feb 2, 2024 · 0 comments

Comments

@baldeepsingh-pantelwar
Copy link
Collaborator

Problem statement

  • On iOS devices, the onKeyDown event bypasses the onChange event, so we need to validate on both onKeyDown and onChange for these devices
  • On Android devices, number input field bypasses any restrictions to hyphens - and dots . and hence bypasses the onChange checks, for example a maximum length of 10 digits check will be bypassed, so we need global checks on onKeyDown event, which does not give access to the entire value of the input
  • On macOS, the behaviour is same as iOS devices
  • If we add a regex to either onChange or onKeyDown, then certain keys will be bypassed like Backspace Arrow Keys Shift Tab Ctrl + C Ctrl + V Escape Tab Enter and more

API Design Approach

Approach 1

<TextField 
  onChange={valdiate((e) => { /** any component specific logic */ }, {
    specialCharacters: false,
    numeric: false,
    emojis: false,
    maxLength: 10
  })}
  onKeyDown={valdiate((e) => { /** any component specific logic */ }, {
    specialCharacters: false,
    numeric: false,
    emojis: false,
    maxLength: 10
  })}
/>

Approach 2

<TextField 
  {...validate({
    specialCharacters: false,
    numeric: false,
    emojis: false,
    maxLength: 10
  },
  {
    onChange: (e) => { /** any component specific logic */ },
    onKeyDown: (e) => { /** any component specific logic */ }
  })}
/>

API development approach

type Options = Partial<{
    specialCharacters: boolean;
    numeric: boolean;
    emojis: boolean;
    maxLength: number;
}>;

function validate(options: Options, props?: TextFieldProps): TextFieldProps {
  const specialCharacters = "`~!@#$%^&*()_+-={}|[]\\:\"';<>?,./".split();
  const numbers = "0123456789".split();
  const alphabets = "abcdefghijklmnopqrstuvwxyz".split();
  const alphabetsCapitalized = alphabets.map((char) => char.toUpperCase()).split();

  return {
    onChange: (e) => {
      if (options.specialCharacters && specialCharacters.some((char) => e.target.value.includes(char))) {
        return e.preventDefault();
      }
      if (options.numbers && numbers.some((char) => e.target.value.includes(char))) {
        return e.preventDefault();
      }
     // rest of the validations
     props.onChange?.(e);
    },
    onKeyDown: (e) => {
      // similar to onChange, but e.target.value is not available, so do maxLength check accordingly
    }
  }
}

Test Cases

Test cases should be written thoroughly and ran across multiple platform

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

1 participant