1- import { Component , Prop , Part } from '@pictogrammers/element' ;
1+ import { Component , Prop , Part , normalizeFloat } from '@pictogrammers/element' ;
22
33import template from './inputNumber.html' ;
44import style from './inputNumber.css' ;
5+ import PgButtonIncrement from '../buttonIncrement/buttonIncrement' ;
56
67@Component ( {
78 selector : 'pg-input-number' ,
@@ -10,22 +11,48 @@ import style from './inputNumber.css';
1011} )
1112export default class PgInputNumber extends HTMLElement {
1213 @Prop ( ) name : string = '' ;
13- @Prop ( ) value : string = '' ;
14+ @Prop ( normalizeFloat ) value : number = 0 ;
15+ @Prop ( normalizeFloat ) min : number = 0 ;
16+ @Prop ( normalizeFloat ) max : number = 100 ;
17+ @Prop ( normalizeFloat ) step : number = 1 ;
1418 @Prop ( ) placeholder : string = '' ;
1519 @Prop ( ) readOnly : boolean = false ;
20+ @Prop ( ) disableButtons : boolean = false ;
1621
1722 @Part ( ) $input : HTMLInputElement ;
23+ @Part ( ) $buttonMinus : PgButtonIncrement ;
24+ @Part ( ) $buttonPlus : PgButtonIncrement ;
1825
1926 connectedCallback ( ) {
2027 this . $input . addEventListener ( 'input' , this . handleInput . bind ( this ) ) ;
2128 this . $input . addEventListener ( 'change' , this . handleChange . bind ( this ) ) ;
29+ this . $buttonMinus . addEventListener ( 'increment' , ( e : any ) => {
30+ this . #triggerInput( this . value - this . step ) ;
31+ } ) ;
32+ this . $buttonPlus . addEventListener ( 'increment' , ( e : any ) => {
33+ this . #triggerInput( this . value + this . step ) ;
34+ } ) ;
35+ this . $buttonMinus . addEventListener ( 'finish' , ( e : any ) => {
36+ this . dispatchEvent ( new CustomEvent ( 'change' , {
37+ detail : {
38+ value : this . value ,
39+ } ,
40+ } ) ) ;
41+ } ) ;
42+ this . $buttonPlus . addEventListener ( 'finish' , ( e : any ) => {
43+ this . dispatchEvent ( new CustomEvent ( 'change' , {
44+ detail : {
45+ value : this . value ,
46+ } ,
47+ } ) ) ;
48+ } ) ;
2249 }
2350
2451 skipValue = false ;
2552
2653 render ( changes ) {
2754 if ( changes . value && ! this . skipValue ) {
28- this . $input . value = this . value ;
55+ this . $input . value = ` ${ this . value } ` ;
2956 }
3057 if ( changes . placeholder ) {
3158 this . $input . placeholder = this . placeholder ;
@@ -36,20 +63,24 @@ export default class PgInputNumber extends HTMLElement {
3663 this . skipValue = false ;
3764 }
3865
39- handleInput ( e ) {
40- e . stopPropagation ( ) ;
41- this . skipValue = true ;
42- this . value = e . target . value ;
66+ #triggerInput( value ) {
4367 this . dispatchEvent (
4468 new CustomEvent ( 'input' , {
4569 detail : {
46- value : e . target . value ,
47- name : e . name
70+ value : value ,
71+ name : this . name ,
4872 }
4973 } )
5074 ) ;
5175 }
5276
77+ handleInput ( e ) {
78+ e . stopPropagation ( ) ;
79+ this . skipValue = true ;
80+ const value = parseInt ( e . target . value ?? '0' , 10 ) ;
81+ this . #triggerInput( value ) ;
82+ }
83+
5384 handleChange ( e ) {
5485 this . dispatchEvent (
5586 new CustomEvent ( 'change' , {
0 commit comments