@@ -17,6 +17,7 @@ The input number with counter for Angular
1717
1818| Angular | ngx-input-counter |
1919| -------- | :------:|
20+ | >=16.0.0 | v1.0.x |
2021| >=15.0.0 | v0.0.x |
2122
2223### Instalation
@@ -27,7 +28,7 @@ npm i ngx-input-counter
2728
2829## Usage
2930
30- Import the module
31+ NgModule: Import the module
3132
3233``` typescript
3334import { NgxInputCounterModule } from ' ngx-input-counter' ;
@@ -40,6 +41,21 @@ import { NgxInputCounterModule } from 'ngx-input-counter';
4041})
4142```
4243
44+ Standalone: Import the component, optional: if you are using the directives
45+
46+ ``` ts
47+ import { NgxInputCounterComponent } from ' ngx-input-counter' ;
48+ @NgModule ({
49+ ...
50+ imports : [
51+ ... ,
52+ NgxInputCounterComponent ,
53+ PlusContentDirective ,
54+ MinusContentDirective ,
55+ ],
56+ })
57+ ```
58+
4359Use in your components
4460
4561``` html
@@ -56,6 +72,8 @@ Use in your components
5672| step | ` number ` | ` 1 ` | Transition time for the animation |
5773| minusTemplate | ` TemplateRef ` | ` - ` | Pass a TemplateRef to replace the minus button content |
5874| plusTemplate | ` TemplateRef ` | ` + ` | Pass a TemplateRef to replace the plus button content |
75+ | minusComponent | ` Type ` | ` undefined ` | Pass a Component to replace the minus button content |
76+ | plusComponent | ` Type ` | ` undefined ` | Pass a Component to replace the plus button content |
5977| minusClass | ` string ` | ` 'ngx-input-counter-button' ` | Classes of the minus button |
6078| plusClass | ` string ` | ` 'ngx-input-counter-button' ` | Classes of the plus button |
6179| valueClass | ` string ` | ` 'ngx-input-counter-value' ` | Classes of value text |
@@ -171,6 +189,110 @@ When you use the slot template you can pass classes to override the button class
171189</ng-template >
172190```
173191
192+ ## Global Configuration
193+
194+ You can configure all ` ngx-input-counter ` components in your app using the ` NgxInputCounterService ` provider.
195+ This allows you to set default values for all instances of ` ngx-input-counter ` throughout your application.
196+
197+ ``` ts
198+ constructor (private config : NgxInputCounterService ) {
199+ this .config .min = 0
200+ this .config .valueClass = ' p-2 border border-gray-400 font-monospace'
201+ this .config .minusClass = ' btn border-gray-400 rounded-l-md opacity-50 hover:opacity-40'
202+ this .config .plusClass = ' btn border-gray-400 rounded-r-md opacity-50 hover:opacity-40'
203+ }
204+ ```
205+
206+ These settings will apply to all ` ngx-input-counter ` components in your application. However, you can still override individual values
207+ directly through their input properties in your templates.
208+
209+ ### Advanced Customization
210+
211+ For more advanced use cases, you can use the plusComponent and minusComponent properties to define custom components for the plus and minus buttons:
212+
213+ ``` ts
214+ constructor (private config : NgxInputCounterService ) {
215+ ...
216+ this .config .minusComponent = MinusTemplateComponent ;
217+ this .config .plusComponent = PlusTemplateComponent ;
218+ }
219+ ```
220+
221+ This is useful when you have dynamic or reusable templates for your button content and want to configure them globally via the service.
222+
223+ ### Extending the Component
224+
225+ Another powerful way to customize ` ngx-input-counter ` is by extending the ` NgxInputCounterComponent ` itself.
226+ This allows you to create your own custom input counter component, add new properties, and directly modify the button content templates within your extended component.
227+
228+ ``` ts
229+ @Component ({
230+ selector: ' app-input-counter' ,
231+ standalone: true ,
232+ imports: [NgxInputCounterComponent ],
233+ template: `
234+ <ng-template #plusTemplateIcon>
235+ <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
236+ <path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
237+ </svg>
238+ </ng-template>
239+ <ng-template #minusTemplateIcon let-step="step" let-min="min" let-value="value">
240+ @if (value !== min + step) {
241+ <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
242+ <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" />
243+ </svg>
244+ } @else {
245+ <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="#EF2F35">
246+ <path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
247+ </svg>
248+ }
249+ </ng-template>
250+ <ngx-input-counter class="custom-plus" style="margin: auto;"
251+ [plusTemplate]="plusTemplateIcon"
252+ [minusTemplate]="minusTemplateIcon"
253+ [min]="min"
254+ [max]="max"
255+ [step]="step"
256+ [value]="value"
257+ [disabled]="disabled"
258+ (change)="onInput($event)"
259+ >
260+ </ngx-input-counter>
261+ ` ,
262+ providers: [
263+ {
264+ provide: NG_VALUE_ACCESSOR ,
265+ useExisting: forwardRef (() => AppCounterComponent ),
266+ multi: true
267+ }
268+ ]
269+ })
270+ export class AppCounterComponent extends NgxInputCounterComponent {}
271+ ```
272+
273+ By creating your own component that extends the original, you can:
274+
275+ - Add custom inputs and behavior
276+ - Override the button content templates
277+ - Fully customize the appearance and functionality
278+
279+ You can then use ` <app-input-counter> ` instead of ` <ngx-input-counter> ` in your templates.
280+
281+ > ![ NOTE]
282+ > If you plan to use your extended component with Angular Forms, don't forget to provide the ` NG_VALUE_ACCESSOR `
283+
284+ ``` ts
285+ providers : [
286+ {
287+ provide: NG_VALUE_ACCESSOR ,
288+ useExisting: forwardRef (() => AppCounterComponent ),
289+ multi: true
290+ }
291+ ]
292+ ```
293+
294+ This ensures your custom counter component works properly with ngModel or reactive forms.
295+
174296## Development
175297
176298Clone this repo and install the dependencies. Run ` ng serve ` for a dev server. Navigate to ` http://localhost:4200/ ` . The application will automatically reload if you change any of the source files.
0 commit comments