Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 1x 1x 1x 130x 130x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 82x 82x 81x 81x 81x 81x 81x 81x 88x 13x 88x 67x 67x 67x 67x 88x 75x 75x 66x 126x 126x 126x 130x 75x 48x 48x 48x 48x 48x 11x 11x 11x 11x 32x 11x 11x 11x 19x 19x 262x 262x 262x 262x 262x 262x 262x 8x 15x 15x 32x 32x 139x 139x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { booleanAttribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, ElementRef, inject, input, numberAttribute, OnChanges, output, signal, SimpleChanges, viewChild } from '@angular/core'; import { AbstractControl, ControlValueAccessor, NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors, Validator, ValidatorFn, Validators } from '@angular/forms'; import { SI_FORM_ITEM_CONTROL, SiFormItemControl } from '@siemens/element-ng/form'; import { addIcons, elementMinus, elementPlus, SiIconComponent } from '@siemens/element-ng/icon'; import { SiTranslatePipe, TranslatableString } from '@siemens/element-translate-ng/translate'; import { Subscription, timer } from 'rxjs'; @Component({ selector: 'si-number-input', imports: [SiIconComponent, SiTranslatePipe], templateUrl: './si-number-input.component.html', styleUrl: './si-number-input.component.scss', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: SiNumberInputComponent, multi: true }, { provide: NG_VALIDATORS, useExisting: SiNumberInputComponent, multi: true }, { provide: SI_FORM_ITEM_CONTROL, useExisting: SiNumberInputComponent } ], changeDetection: ChangeDetectionStrategy.OnPush, host: { '[class.show-step-buttons]': 'showButtons()', '[class.disabled]': 'disabled()', '[class.readonly]': 'readonly()' } }) export class SiNumberInputComponent implements OnChanges, ControlValueAccessor, Validator, SiFormItemControl { private static idCounter = 0; private static formatValidator: ValidatorFn = control => { Iif (control.value != null && isNaN(control.value)) { return { numberFormat: true }; } return null; }; /** * The min. value for HTML input * * @defaultValue undefined */ readonly minInput = input<number | undefined, unknown>(undefined, { // eslint-disable-next-line @angular-eslint/no-input-rename alias: 'min', transform: numberAttribute }); /** * The max. value for HTML input * * @defaultValue undefined */ readonly maxInput = input<number | undefined, unknown>(undefined, { // eslint-disable-next-line @angular-eslint/no-input-rename alias: 'max', transform: numberAttribute }); /** * The step size for HTML input * * @defaultValue 1 */ readonly step = input<number | 'any'>(1); /** The value */ readonly value = input<number>(); /** Optional unit label */ readonly unit = input<string>(); /** * Show increment/decrement buttons? * * @defaultValue true */ readonly showButtons = input(true, { transform: booleanAttribute }); /** * The aria-label passed to the input * * @defaultValue undefined */ readonly ariaLabel = input<TranslatableString>(undefined, { alias: 'aria-label' }); /** * ID that is set on the input, e.g. for `<label for="...">` * * @defaultValue * ``` * `__si-number-input-${SiNumberInputComponent.idCounter++}` * ``` */ readonly inputId = input(`__si-number-input-${SiNumberInputComponent.idCounter++}`); readonly id = computed(() => this.inputId()); /** @defaultValue false */ // eslint-disable-next-line @angular-eslint/no-input-rename readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute }); /** @defaultValue false */ readonly readonly = input(false, { transform: booleanAttribute }); /** * The placeholder for input field. */ readonly placeholder = input<TranslatableString>(); readonly valueChange = output<number | undefined>(); readonly inputElement = viewChild.required<ElementRef<HTMLInputElement>>('inputElement'); /** * This ID will be bound to the `aria-describedby` attribute of the number-input. * Use this to reference the element containing the error message(s) for the number-input. * It will be picked by the {@link SiFormItemComponent} if the number-input is used inside a form item. * * @defaultValue * ``` * `${this.id()}-errormessage` * ``` */ readonly errormessageId = input(`${this.id()}-errormessage`); protected canInc = true; protected canDec = true; protected readonly disabled = computed(() => this.disabledInput() || this.disabledNgControl()); private readonly disabledNgControl = signal(false); protected onTouched: () => void = () => {}; protected onChange: (val: any) => void = () => {}; protected validator: ValidatorFn | null = SiNumberInputComponent.formatValidator; protected onValidatorChanged: () => void = () => {}; protected readonly min = computed(() => { const minVal = this.minInput(); return minVal === undefined || isNaN(minVal) ? undefined : minVal; }); protected readonly max = computed(() => { const maxVal = this.maxInput(); return maxVal === undefined || isNaN(maxVal) ? undefined : maxVal; }); protected readonly icons = addIcons({ elementMinus, elementPlus }); private internalValue?: number; private autoUpdate$ = timer(400, 80); private autoUpdateSubs?: Subscription; private changeDetectorRef = inject(ChangeDetectorRef); ngOnChanges(changes: SimpleChanges): void { if (changes.value) { this.writeValueToInput(this.value()); } if (changes.minInput || changes.maxInput) { const minValue = this.min(); const maxValue = this.max(); this.validator = Validators.compose([ minValue != null ? Validators.min(minValue) : null, maxValue != null ? Validators.max(maxValue) : null, SiNumberInputComponent.formatValidator ])!; this.onValidatorChanged(); } this.updateStepButtons(); } /** @internal */ registerOnChange(fn: any): void { this.onChange = fn; } /** @internal */ registerOnTouched(fn: () => void): void { this.onTouched = fn; } /** @internal */ setDisabledState(isDisabled: boolean): void { this.disabledNgControl.set(isDisabled); } /** @internal */ writeValue(value: number | undefined): void { this.writeValueToInput(value); this.updateStepButtons(); this.changeDetectorRef.markForCheck(); } /** @internal */ validate(control: AbstractControl): ValidationErrors | null { return this.validator ? this.validator(control) : null; } /** @internal */ registerOnValidatorChange?(fn: () => void): void { this.onValidatorChanged = fn; } protected modelChanged(): void { const value = this.inputElement().nativeElement.value ? this.inputElement().nativeElement.valueAsNumber : undefined; this.internalValue = value; this.updateStepButtons(); this.onChange(value); this.valueChange.emit(value); } protected autoUpdateStart(event: Event, isIncrement: boolean): void { const mouseButton = (event as MouseEvent).button; Iif (mouseButton) { return; } this.onTouched(); event.preventDefault(); const trigger = isIncrement ? () => this.increment() : () => this.decrement(); this.autoUpdateSubs?.unsubscribe(); this.autoUpdateSubs = this.autoUpdate$.subscribe(trigger); trigger(); } protected autoUpdateStop(): void { this.autoUpdateSubs?.unsubscribe(); this.autoUpdateSubs = undefined; } private updateStepButtons(): void { const stepValue = this.step(); const step = typeof stepValue === 'number' ? stepValue : 1; const max = this.max(); this.canInc = max == null || this.internalValue == null || this.internalValue + step <= max; const min = this.min(); this.canDec = min == null || this.internalValue == null || this.internalValue - step >= min; if (!this.canInc || !this.canDec) { this.autoUpdateStop(); } } private decrement(): void { this.inputElement().nativeElement.stepDown(); this.modelChanged(); } private increment(): void { this.inputElement().nativeElement.stepUp(); this.modelChanged(); } private writeValueToInput(value: number | undefined): void { this.inputElement().nativeElement.value = value == null ? '' : value.toString(); this.internalValue = value; } } |