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

InputNumber: Cannot Type Zero as an Input #6727

Closed
fufupati opened this issue Jun 4, 2024 · 4 comments · Fixed by #6728, leoo1992/GeradorQRCode#57 or leoo1992/GeradorQRCode#80 · May be fixed by nhattpn/BTL_LTNC#56
Closed

InputNumber: Cannot Type Zero as an Input #6727

fufupati opened this issue Jun 4, 2024 · 4 comments · Fixed by #6728, leoo1992/GeradorQRCode#57 or leoo1992/GeradorQRCode#80 · May be fixed by nhattpn/BTL_LTNC#56
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@fufupati
Copy link

fufupati commented Jun 4, 2024

Describe the bug

The InputNumber component doesn't accept 0 as an input regardless from it's properties.

Tested on Hungarian layout keyboard (code page 852).

Reproducer

https://primereact.org/inputnumber

PrimeReact version

10.6.6

React version

18.x

Language

ALL

Build / Runtime

Create React App (CRA)

Browser(s)

Tested on Chrome, Edge, Firefox

Steps to reproduce the behavior

No response

Expected behavior

No response

@fufupati fufupati added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jun 4, 2024
@fufupati
Copy link
Author

fufupati commented Jun 5, 2024

I've debugged a little and in the InputNumber OnInputKeyDown function switch statement's default case:

default:
    event.preventDefault();
    let char = event.key;
    console.log(event)
    if (char) {
        const _isDecimalSign = isDecimalSign(char);
        const _isMinusSign = isMinusSign(char);

        if ((event.code && (event.code.startsWith('Digit') || event.code.startsWith('Numpad')) && Number(char) >= 0 && Number(char) <= 9) || _isMinusSign || _isDecimalSign) {
            insert(event, char, { isDecimalSign: _isDecimalSign, isMinusSign: _isMinusSign });
        }
    }

    break;

The event.code is checked if it starts with 'Digit', this is problematic because for me when I press zero the event.code = "Backquote", so the condition will fail. In previous builds this wasn't a problem because not the event.code was checked but instead the event.keyCode which should be preferred.

Proposed solution:

default:
    event.preventDefault();
    let char = event.key;
    console.log(event)
    if (char) {
        const _isDecimalSign = isDecimalSign(char);
        const _isMinusSign = isMinusSign(char);
        const isDigit = event.keyCode >= 48 && event.keyCode <= 57;
        const isNumpadDigit = event.keyCode >= 96 && event.keyCode <= 105;

        if ((event.code && (isDigit || isNumpadDigit) && Number(char) >= 0 && Number(char) <= 9) || _isMinusSign || _isDecimalSign) {
            insert(event, char, { isDecimalSign: _isDecimalSign, isMinusSign: _isMinusSign });
        }
    }

    break;

@fufupati
Copy link
Author

fufupati commented Jun 5, 2024

Just read that the keyCode property is deprecated (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode), so instead I think the best solution is to just omit the (event.code.startsWith('Digit') || event.code.startsWith('Numpad')) part from the condition as I don't see if it adds any value, the Number(char) >= 0 && Number(char) <= 9) part already checking if it's a number. I tested it and it seems to work fine now and I can write '0'.

@melloware
Copy link
Member

@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Jun 5, 2024
melloware added a commit to melloware/primereact that referenced this issue Jun 5, 2024
@melloware melloware self-assigned this Jun 5, 2024
@melloware melloware added this to the 10.7.0 milestone Jun 5, 2024
@melloware
Copy link
Member

I submitted a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment