Skip to content

Commit 07f1a4e

Browse files
committed
add min and max props
1 parent c227afd commit 07f1a4e

File tree

9 files changed

+101
-17
lines changed

9 files changed

+101
-17
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# NgxInputCounter
22

3-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 15.1.3.
3+
The input number with counter for Angular
44

55
## Getting Started
66

77
### Versions
88

9-
| Angular | lib |
9+
| Angular | ngx-input-counter |
1010
| -------- |:------:|
1111
| >=15.0.0 | v0.0.x |
1212

1313
### Instalation
1414

1515
```
16-
npm i lib
16+
npm i ngx-input-counter
1717
```
1818

1919
## Usage
2020

2121
Import the module
2222

2323
```typescript
24-
import { NgxInputCounterModule } from 'lib';
24+
import { NgxInputCounterModule } from 'ngx-input-counter';
2525
@NgModule({
2626
...
2727
imports: [
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
<h1>Demo</h1>
22

3+
<div>
4+
35
<ngx-input-counter [(ngModel)]="qty"></ngx-input-counter>
46

5-
Value: {{qty}}
7+
Value: {{qty}}
8+
9+
<h2>Custom style</h2>
10+
11+
<ngx-input-counter class="custom" [(ngModel)]="custom" [max]="5" [min]="0"></ngx-input-counter>
12+
13+
Value: {{custom}}
14+
15+
<h2>Form</h2>
16+
17+
<div [formGroup]="myForm">
18+
<ngx-input-counter class="custom" formControlName="age" [max]="20" [min]="18"></ngx-input-counter>
19+
20+
<pre>form: {{myForm.value | json}}
21+
errors: {{myForm.errors | json}}
22+
</pre>
23+
24+
</div>
25+
26+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:host {
2+
text-align: center;
3+
}

projects/demo/src/app/app.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component } from '@angular/core';
2+
import { FormControl, FormGroup, Validators } from '@angular/forms';
23

34
@Component({
45
selector: 'app-root',
@@ -8,4 +9,9 @@ import { Component } from '@angular/core';
89
export class AppComponent {
910
title = 'demo';
1011
qty = 2
12+
custom = 1
13+
14+
myForm: FormGroup = new FormGroup({
15+
age: new FormControl(2, [Validators.min(18)])
16+
})
1117
}

projects/demo/src/app/app.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NgModule } from '@angular/core';
2-
import { FormsModule } from '@angular/forms';
2+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
33
import { BrowserModule } from '@angular/platform-browser';
44
import { NgxInputCounterModule } from 'projects/ngx-input-counter/src/public-api';
55

@@ -12,7 +12,8 @@ import { AppComponent } from './app.component';
1212
imports: [
1313
BrowserModule,
1414
NgxInputCounterModule,
15-
FormsModule
15+
FormsModule,
16+
ReactiveFormsModule,
1617
],
1718
providers: [],
1819
bootstrap: [AppComponent]

projects/demo/src/styles.scss

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
/* You can add global styles to this file, and also import other style files */
2+
ngx-input-counter.custom {
3+
font-family: 'Segoe UI', Verdana, sans-serif;
4+
.ngx-input-counter-container {
5+
border-radius: 500px;
6+
text-align: center;
7+
.ngx-input-counter-value {
8+
width: 30px;
9+
display: inline-block;
10+
font-weight: bold;
11+
font-size: 18px;
12+
}
13+
}
14+
15+
.ngx-input-counter-button {
16+
border: 1px solid #999;
17+
border-radius: 100%;
18+
background-color: white;
19+
width: 36px;
20+
height: 36px;
21+
text-align: center;
22+
font-weight: 600;
23+
font-size: 20px;
24+
cursor: pointer;
25+
transition: all 0.5s ease;
26+
&:hover {
27+
background-color: #ddd;
28+
box-shadow: 1px 1px 2px 1px #ccc;
29+
}
30+
}
31+
}

projects/ngx-input-counter/README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
# NgxInputCounter
22

3-
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 15.1.0.
3+
The input number with counter for Angular
44

5-
## Code scaffolding
5+
## Getting Started
66

7-
Run `ng generate component component-name --project ngx-input-counter` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project ngx-input-counter`.
8-
> Note: Don't forget to add `--project ngx-input-counter` or else it will be added to the default project in your `angular.json` file.
7+
### Versions
98

10-
## Build
9+
| Angular | ngx-input-counter |
10+
| -------- |:------:|
11+
| >=15.0.0 | v0.0.x |
1112

12-
Run `ng build ngx-input-counter` to build the project. The build artifacts will be stored in the `dist/` directory.
13+
### Instalation
14+
15+
```
16+
npm i ngx-input-counter
17+
```
18+
19+
## Usage
20+
21+
Import the module
22+
23+
```typescript
24+
import { NgxInputCounterModule } from 'ngx-input-counter';
25+
@NgModule({
26+
...
27+
imports: [
28+
...,
29+
NgxInputCounterModule,
30+
],
31+
})
32+
```
1333

1434
## Publishing
1535

projects/ngx-input-counter/src/lib/ngx-input-counter.component.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
.ngx-input-counter-container {
2-
border: 1px solid #999;
3-
width: fit-content;
42
text-align: center;
53
.ngx-input-counter-value {
64
width: 36px;

projects/ngx-input-counter/src/lib/ngx-input-counter.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ export class NgxInputCounterComponent implements ControlValueAccessor {
3939

4040
@Input() value: number = 0;
4141
@Input() step: number = 1;
42-
@Input() min: number | undefined;
42+
@Input() min: number = -Infinity;
4343
@Input() max: number = Infinity;
4444
@Input() disabled: boolean = false;
4545

4646
addItem(step: number) {
47-
this.onInput(this.value + step)
47+
let newValue = this.value + step;
48+
if(this.value < this.min) newValue = this.min
49+
if(this.value > this.max) newValue = this.max
50+
const minValue = newValue >= this.min
51+
if(this.max >= newValue && minValue)
52+
this.onInput(newValue)
4853
}
4954
}

0 commit comments

Comments
 (0)