|
1 | | -import { Component, Input, HostBinding, EventEmitter, Output } from "@angular/core"; |
| 1 | +import { |
| 2 | + Component, |
| 3 | + Input, |
| 4 | + HostBinding, |
| 5 | + EventEmitter, |
| 6 | + Output, |
| 7 | + TemplateRef |
| 8 | +} from "@angular/core"; |
2 | 9 | import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms"; |
3 | 10 | import { isNullOrUndefined } from "util"; |
4 | 11 |
|
@@ -39,43 +46,51 @@ export class NumberChange { |
39 | 46 | <div *ngIf="helperText" class="bx--form__helper-text">{{helperText}}</div> |
40 | 47 | <div |
41 | 48 | data-numberinput |
42 | | - [attr.data-invalid]="(invalid ? '' : null)" |
| 49 | + [attr.data-invalid]="(invalid ? true : null)" |
43 | 50 | class="bx--number" |
44 | 51 | [ngClass]="{ |
45 | 52 | 'bx--number--light': theme === 'light', |
46 | 53 | 'bx--number--nolabel': !label, |
47 | 54 | 'bx--number--helpertext': helperText, |
48 | 55 | 'bx--skeleton' : skeleton |
49 | 56 | }"> |
50 | | - <input |
51 | | - type="number" |
52 | | - [id]="id" |
53 | | - [value]="value" |
54 | | - [min]="min" |
55 | | - [max]="max" |
56 | | - [disabled]="disabled" |
57 | | - [required]="required" |
58 | | - (input)="onNumberInputChange($event)"/> |
59 | | - <div *ngIf="!skeleton" class="bx--number__controls"> |
60 | | - <button |
61 | | - class="bx--number__control-btn up-icon" |
62 | | - type="button" |
63 | | - aria-live="polite" |
64 | | - aria-atomic="true" |
65 | | - (click)="onIncrement()"> |
66 | | - <ibm-icon-caret-up16></ibm-icon-caret-up16> |
67 | | - </button> |
68 | | - <button |
69 | | - class="bx--number__control-btn down-icon" |
70 | | - type="button" |
71 | | - aria-live="polite" |
72 | | - aria-atomic="true" |
73 | | - (click)="onDecrement()"> |
74 | | - <ibm-icon-caret-down16></ibm-icon-caret-down16> |
75 | | - </button> |
| 57 | + <label *ngIf="!skeleton && label" [for]="id" class="bx--label">{{label}}</label> |
| 58 | + <div *ngIf="helperText" class="bx--form__helper-text">{{helperText}}</div> |
| 59 | + <div class="bx--number__input-wrapper"> |
| 60 | + <input |
| 61 | + type="number" |
| 62 | + [id]="id" |
| 63 | + [value]="value" |
| 64 | + [min]="min" |
| 65 | + [max]="max" |
| 66 | + [disabled]="disabled" |
| 67 | + [required]="required" |
| 68 | + (input)="onNumberInputChange($event)"/> |
| 69 | + <ibm-icon-warning-filled16 |
| 70 | + *ngIf="!skeleton && invalid" |
| 71 | + class="bx--number__invalid" |
| 72 | + style="display: inherit;"> |
| 73 | + </ibm-icon-warning-filled16> |
| 74 | + <div *ngIf="!skeleton" class="bx--number__controls"> |
| 75 | + <button |
| 76 | + class="bx--number__control-btn up-icon" |
| 77 | + aria-live="polite" |
| 78 | + aria-atomic="true" |
| 79 | + (click)="onIncrement()"> |
| 80 | + <ibm-icon-caret-up16></ibm-icon-caret-up16> |
| 81 | + </button> |
| 82 | + <button |
| 83 | + class="bx--number__control-btn down-icon" |
| 84 | + aria-live="polite" |
| 85 | + aria-atomic="true" |
| 86 | + (click)="onDecrement()"> |
| 87 | + <ibm-icon-caret-down16></ibm-icon-caret-down16> |
| 88 | + </button> |
| 89 | + </div> |
76 | 90 | </div> |
77 | 91 | <div *ngIf="invalid" class="bx--form-requirement"> |
78 | | - {{invalidText}} |
| 92 | + <ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container> |
| 93 | + <ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template> |
79 | 94 | </div> |
80 | 95 | </div> |
81 | 96 | `, |
@@ -142,7 +157,7 @@ export class Number implements ControlValueAccessor { |
142 | 157 | /** |
143 | 158 | * Sets the invalid text. |
144 | 159 | */ |
145 | | - @Input() invalidText; |
| 160 | + @Input() invalidText: string | TemplateRef<any>; |
146 | 161 | /** |
147 | 162 | * Emits event notifying other classes when a change in state occurs in the input. |
148 | 163 | */ |
@@ -181,6 +196,13 @@ export class Number implements ControlValueAccessor { |
181 | 196 | this.onTouched = fn; |
182 | 197 | } |
183 | 198 |
|
| 199 | + /** |
| 200 | + * Sets the disabled state through the model |
| 201 | + */ |
| 202 | + setDisabledState(isDisabled: boolean) { |
| 203 | + this.disabled = isDisabled; |
| 204 | + } |
| 205 | + |
184 | 206 | /** |
185 | 207 | * Called when number input is blurred. Needed to properly implement `ControlValueAccessor`. |
186 | 208 | * @memberof Number |
@@ -228,4 +250,8 @@ export class Number implements ControlValueAccessor { |
228 | 250 | this.value = event.target.value; |
229 | 251 | this.emitChangeEvent(); |
230 | 252 | } |
| 253 | + |
| 254 | + protected isTemplate(value) { |
| 255 | + return value instanceof TemplateRef; |
| 256 | + } |
231 | 257 | } |
0 commit comments