Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
385 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2019 Vaadin Ltd. | ||
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
--> | ||
|
||
<link rel="import" href="../../polymer/polymer-element.html"> | ||
<link rel="import" href="../../polymer/lib/elements/custom-style.html"> | ||
<link rel="import" href="vaadin-number-field.html"> | ||
|
||
<dom-module id="vaadin-integer-field-template"> | ||
|
||
<script> | ||
(function() { | ||
|
||
/** | ||
* `<vaadin-integer-field>` is a Web Component for integer field control in forms. | ||
* | ||
* ```html | ||
* <vaadin-integer-field label="Number"> | ||
* </vaadin-integer-field> | ||
* ``` | ||
* | ||
* @memberof Vaadin | ||
* @extends Vaadin.NumberFieldElement | ||
* @demo demo/index.html | ||
*/ | ||
class IntegerFieldElement extends Vaadin.NumberFieldElement { | ||
static get is() { | ||
return 'vaadin-integer-field'; | ||
} | ||
|
||
static get version() { | ||
return '2.4.11'; | ||
} | ||
|
||
ready() { | ||
super.ready(); | ||
this.inputElement.addEventListener('drop', this.__onDrop.bind(this)); | ||
this.inputElement.addEventListener('paste', this.__onPaste.bind(this)); | ||
} | ||
|
||
_valueChanged(newVal, oldVal) { | ||
if (newVal !== '' && !this.__isInteger(newVal)) { | ||
console.warn(`Trying to set non-integer value "${newVal}" to <vaadin-integer-field>.` | ||
+ ` Clearing the value.`); | ||
this.value = ''; | ||
return; | ||
} | ||
super._valueChanged(newVal, oldVal); | ||
} | ||
|
||
_onKeyDown(e) { | ||
if (!this.__shouldAcceptKey(e)) { | ||
e.preventDefault(); | ||
} | ||
super._onKeyDown(e); | ||
} | ||
|
||
__shouldAcceptKey(event) { | ||
return (event.metaKey || event.ctrlKey) | ||
|| !event.key // allow typing anything if event.key is not supported | ||
|| event.key.length !== 1 // allow "Backspace", "ArrowLeft" etc. | ||
|| /^[-+\d]$/.test(event.key); | ||
} | ||
|
||
__onDrop(e) { | ||
const draggedText = e.dataTransfer.getData('text'); | ||
if (!this.__isValidInput(draggedText)) { | ||
e.preventDefault(); | ||
} | ||
} | ||
|
||
__onPaste(e) { | ||
const pastedText = (e.clipboardData || window.clipboardData).getData('text'); | ||
if (!this.__isValidInput(pastedText)) { | ||
e.preventDefault(); | ||
} | ||
} | ||
|
||
_stepChanged(newVal, oldVal) { | ||
if (!this.__hasOnlyDigits(newVal)) { | ||
console.warn(`Trying to set invalid step size "${newVal}",` | ||
+ ` which is not a positive integer, to <vaadin-integer-field>.` | ||
+ ` Resetting the default value 1.`); | ||
this.step = 1; | ||
return; | ||
} | ||
super._stepChanged(newVal, oldVal); | ||
} | ||
|
||
__isInteger(value) { | ||
return /^(-\d)?\d*$/.test(String(value)); | ||
} | ||
|
||
__isValidInput(value) { | ||
return /^[\d+-]*$/.test(String(value)); | ||
} | ||
|
||
__hasOnlyDigits(value) { | ||
return /^\d*$/.test(String(value)); | ||
} | ||
} | ||
|
||
window.customElements.define(IntegerFieldElement.is, IntegerFieldElement); | ||
|
||
/** | ||
* @namespace Vaadin | ||
*/ | ||
window.Vaadin = window.Vaadin || {}; | ||
Vaadin.IntegerFieldElement = IntegerFieldElement; | ||
})(); | ||
</script> | ||
</dom-module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.