PolymerElements / paper-input

A Material Design text field
https://www.webcomponents.org/element/PolymerElements/paper-input
130 stars 162 forks source link

paper-input type="number" floating doesn't work if you don't start with a number #632

Open MichSach opened 6 years ago

MichSach commented 6 years ago

Description

if you have paper-input type="number" and auto-validate pattern="[0-9]+" and you start typing with a '-' or an 'e' (allowed characters beside numbers), the floating does not work.

Expected outcome

The label is floating. Like this:

image

Actual outcome

Label not floating. Like this:

image

One minus works:

image

Two not:

image

I know that this entry makes no sense, but nevertheless it should float. Or should it only float if the entry is valid?

Live Demo

Steps to reproduce

paper-input type="number" auto-validate pattern="[0-9]+" start typing with e or -- or another allowed non number

Browsers Affected

notwaldorf commented 6 years ago

Unfortunately this is a can't fix. The native input with type="number" doesn't send an event when you type the incorrect characters, and they're not saved in the input itself, so there's no way to detect that this text change happened and force the label to float.

(duplicate of https://github.com/PolymerElements/paper-input/issues/377, which has more details)

jogibear9988 commented 6 years ago

@notwaldorf can't we use the validity property of the input? we have validity.badInput set to true when this happens! see also: https://developer.mozilla.org/en-US/docs/Web/API/ValidityState

jogibear9988 commented 6 years ago

I think we could listen to keypress, and look if badInput is true, and then float also!

notwaldorf commented 6 years ago

The actual validity of the input doesn't really have anything to do with it, since you don't get an event when the validity changes, so you need to listen to keypresses anyway. And that actually much more complicated than it sounds:

I'm leaning towards not fixing this since the solution sounds super messy and hard to maintain (and expensive, that even will fire every time you type), and the bug it fixes is actually fairly small, and you could technically write this workaround (the keypress code) in your code.

jogibear9988 commented 6 years ago

If I add this code:

$0.onkeyup = (e) => console.log(e.path[0].validity.badInput)

to the input in your paper input example: https://www.webcomponents.org/element/PolymerElements/paper-input/demo/demo/index.html (the one with the $ in front) it works as expected I got "true" in the console when the value is bad

jogibear9988 commented 6 years ago

when you look here: https://github.com/PolymerElements/paper-input/blob/43ad083cc29680f9a02fc3415220bd3cb1b8967d/paper-input-container.html#L591 you already have the check for validity, I think only the trigger is missing

jogibear9988 commented 6 years ago

@notwaldorf if I work on a fix wich listens for keyup, will this be accepted?

jogibear9988 commented 6 years ago

Is there also a bug in the element, cause the event listeners in the attached callback are added every time the control is attached to the dom. I use dockspawn (a docking framework), wich dettaches and attaches elements when you move your dock pages

jogibear9988 commented 6 years ago

It works, if a add this to the ready:

this._inputElement.children[0].addEventListener("keyup", (e) => this._handleValue(this._inputElement));

and change:

  if (value || value === 0 || (inputElement.type === 'number' && !inputElement.checkValidity())) {

to

  if (value || value === 0 || (inputElement.children[0].type === 'number' && !inputElement.children[0].checkValidity())) {

i know this fix is not correct, but i don't know how the correct fix should look like? shoul iron-input wrap checkValidity?

jogibear9988 commented 6 years ago

@notwaldorf have you read my comments? Do you still think itˋs not worth to be fixed?

notwaldorf commented 6 years ago

I have read, but can you send a PR so we can actually run tests and I can see the diff? It's starting to be hard to keep track of what the change exactly is, without the full diff :)

jogibear9988 commented 6 years ago

I will do a pull req.

But I'm not sure, if I only should fix it if child is a input, or also look if child is a iron-input. If it's a iron input, iron input has to expose some of the inputs api!

jogibear9988 commented 6 years ago

I've created a fix, seehttps://github.com/PolymerElements/paper-input/pull/642 but googlebot says I've not signed the cla (but I have)

jogibear9988 commented 6 years ago

fixed it, committed with wrong GH username ;-(