import { booleanAttribute, ChangeDetectionStrategy, Component, effect, input, model, output, signal, untracked, ViewEncapsulation, } from "@angular/core"; import { num, str } from "@simplysm/core-common"; import { SdButton } from "../button/sd-button"; import { SdTextfield } from "./sd-textfield"; import { NgIcon } from "@ng-icons/core"; import { tablerArrowLeft, tablerEraser } from "@ng-icons/tabler-icons"; @Component({ selector: "sd-numpad", changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, standalone: true, imports: [SdTextfield, SdButton, NgIcon], template: ` @if (useEnterButton()) { } @if (useMinusButton()) { } @for (r of [2, 1, 0]; track r) { @for (c of [0, 1, 2]; track $index) { } }
ENT
-
{{ r * 3 + c + 1 }}
0 .
`, styles: [ /* language=SCSS */ ` sd-numpad { table { width: 100%; border-collapse: collapse; > * > tr > * { border: none; padding: var(--gap-xxs); > sd-button { border: none; } } } } `, ], }) export class SdNumpad { text = signal(undefined); placeholder = input(); value = model(); required = input(false, { transform: booleanAttribute }); inputDisabled = input(false, { transform: booleanAttribute }); useEnterButton = input(false, { transform: booleanAttribute }); useMinusButton = input(false, { transform: booleanAttribute }); enterButtonClick = output(); constructor() { effect(() => { const text = this.text(); const newValue = num.parseFloat(text); if (newValue !== untracked(() => this.value())) { this.value.set(newValue); } }); effect(() => { const value = this.value(); const prevValue = num.parseFloat(untracked(() => this.text())); if (prevValue !== value) { this.text.set(value?.toString()); } }); } onButtonClick(key: string) { if (key === "C") { this.text.set(undefined); } else if (key === "BS") { this.text.update((v) => { const sliced = v?.slice(0, -1); return str.isNullOrEmpty(sliced) ? undefined : sliced; }); } else if (key === "ENT") { this.enterButtonClick.emit(); } else if (key === "Minus") { this.text.update((v) => { if (v != null && v[0] === "-") { return v.slice(1); } else { return "-" + (v ?? ""); } }); } else { this.text.update((v) => (v ?? "") + key); } } protected readonly tablerEraser = tablerEraser; protected readonly tablerArrowLeft = tablerArrowLeft; }