-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce Formly number field (quantity input with + and - butt…
…ons)
- Loading branch information
Showing
6 changed files
with
134 additions
and
17 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
35 changes: 35 additions & 0 deletions
35
src/app/shared/formly/types/number-field/number-field.component.html
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,35 @@ | ||
<div class="counter-input"> | ||
<button | ||
type="button" | ||
class="btn btn-link decrease-button" | ||
(click)="decrease()" | ||
[disabled]="cannotDecrease" | ||
translate | ||
tabindex="-1" | ||
> | ||
number.decrease.text | ||
</button> | ||
<button | ||
type="button" | ||
class="btn btn-link increase-button" | ||
(click)="increase()" | ||
[disabled]="cannotIncrease" | ||
translate | ||
tabindex="-1" | ||
> | ||
number.increase.text | ||
</button> | ||
<input | ||
type="number" | ||
[formControl]="formControl" | ||
[formlyAttributes]="field" | ||
class="form-control text-center" | ||
[ngClass]="props.inputClass" | ||
[attr.data-testing-id]="field.key" | ||
pattern="[0-9]*" | ||
[min]="props.min" | ||
[max]="props.max" | ||
[step]="props.step" | ||
[attr.aria-label]="props.ariaLabel" | ||
/> | ||
</div> |
31 changes: 31 additions & 0 deletions
31
src/app/shared/formly/types/number-field/number-field.component.scss
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,31 @@ | ||
.counter-input { | ||
position: relative; | ||
|
||
button { | ||
position: absolute; | ||
width: auto; | ||
margin: 0; | ||
} | ||
|
||
.decrease-button { | ||
left: 0; | ||
} | ||
|
||
.increase-button { | ||
right: 0; | ||
} | ||
|
||
// https://www.w3schools.com/howto/howto_css_hide_arrow_number.asp | ||
|
||
/* Chrome, Safari, Edge, Opera */ | ||
input::-webkit-outer-spin-button, | ||
input::-webkit-inner-spin-button { | ||
margin: 0; | ||
appearance: none; | ||
} | ||
|
||
/* Firefox */ | ||
input[type='number'] { | ||
appearance: textfield; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/app/shared/formly/types/number-field/number-field.component.ts
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,41 @@ | ||
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; | ||
import { FieldType, FieldTypeConfig } from '@ngx-formly/core'; | ||
|
||
/** | ||
* Type for a number field | ||
* | ||
* @defaultWrappers form-field-horizontal & validation | ||
*/ | ||
@Component({ | ||
selector: 'ish-number-field', | ||
templateUrl: './number-field.component.html', | ||
styleUrls: ['./number-field.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class NumberFieldComponent extends FieldType<FieldTypeConfig> implements OnInit { | ||
cannotIncrease = false; | ||
cannotDecrease = false; | ||
|
||
ngOnInit(): void { | ||
this.evaluateButtonDisabled(); | ||
} | ||
|
||
increase() { | ||
this.formControl.setValue( | ||
Number.parseInt(this.formControl.value) + (this.field.props.step ? this.field.props.step : 1) | ||
); | ||
this.evaluateButtonDisabled(); | ||
} | ||
|
||
decrease() { | ||
this.formControl.setValue( | ||
Number.parseInt(this.formControl.value) - (this.field.props.step ? this.field.props.step : 1) | ||
); | ||
this.evaluateButtonDisabled(); | ||
} | ||
|
||
private evaluateButtonDisabled() { | ||
this.cannotDecrease = this.field.props.min && this.formControl.value <= this.field.props.min; | ||
this.cannotIncrease = this.field.props.max && this.formControl.value >= this.field.props.max; | ||
} | ||
} |
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