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

handle android specific update from input event #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/PincodeInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
let type;
let selectTextOnFocus;

let userAgent = navigator.userAgent;
const android = userAgent?.match(/android/i);

const ctx = getContext("Pincode");
const unsubscribeType = ctx._type.subscribe((_type) => {
type = _type;
Expand Down Expand Up @@ -56,6 +59,20 @@
if (selectTextOnFocus) ref.select();
}}"
on:blur
on:input
on:input="{(e) => {
if (android) {
// Get latest char from the input value
const latestChar = e?.target?.value[(e?.target?.value?.length ?? 0) - 1];
// Update value according to input type, as was done on the on:keyup event
if (type === 'numeric' && /^[0-9]$/.test(latestChar)) {
ctx.update(id, latestChar);
}
if (type === 'alphanumeric' && /^[a-zA-Z0-9]$/.test(latestChar)) {
ctx.update(id, latestChar);
}
}
}}"
on:keydown
on:keydown="{(e) => {
if (e.key === KEYBOARD.BACKSPACE) {
Expand All @@ -74,12 +91,15 @@
if (e.key !== KEYBOARD.TAB) {
e.preventDefault();

if (type === 'numeric' && /^[0-9]$/.test(e.key)) {
ctx.update(id, e.key);
}
// Do not try to update the value from the keydown event if on android, leave that to the input event
if (!android) {
if (type === 'numeric' && /^[0-9]$/.test(e.key)) {
ctx.update(id, e.key);
}

if (type === 'alphanumeric' && /^[a-zA-Z0-9]$/.test(e.key)) {
ctx.update(id, e.key);
if (type === 'alphanumeric' && /^[a-zA-Z0-9]$/.test(e.key)) {
ctx.update(id, e.key);
}
}
}
}}"
Expand Down
7 changes: 6 additions & 1 deletion src/PincodeInput.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface PincodeInputProps extends svelte.JSX.HTMLAttributes<HTMLElement

export default class PincodeInput extends SvelteComponentTyped<
PincodeInputProps,
{ focus: WindowEventMap["focus"]; blur: WindowEventMap["blur"]; keydown: WindowEventMap["keydown"] },
{
focus: WindowEventMap["focus"];
blur: WindowEventMap["blur"];
input: WindowEventMap["input"];
keydown: WindowEventMap["keydown"];
},
{}
> {}