-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-demo.component.ts
More file actions
252 lines (238 loc) · 11.5 KB
/
Copy pathinput-demo.component.ts
File metadata and controls
252 lines (238 loc) · 11.5 KB
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
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { DemoPageComponent, DemoSectionComponent } from '../../../shared/demo-page.component';
import { SdInput } from '@sdcorejs/angular/forms/input';
@Component({
selector: 'app-input-demo',
standalone: true,
imports: [DemoPageComponent, DemoSectionComponent, FormsModule, ReactiveFormsModule, SdInput],
template: `
<demo-page
#demoPage
title="Input"
description="sd-input – ô nhập liệu một dòng. Hỗ trợ helper text, kiểu (text/number/password/email), trạng thái disabled / readonly / viewed và validator chuẩn.">
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-co-ban') {
<demo-section
heading="Cơ bản"
[props]="[{ name: '[(model)]', value: 'two-way' }]"
note="Bind hai chiều với [(model)] và FormGroup chia sẻ.">
<div style="width: 320px">
<sd-input
label="Họ và tên"
placeholder="Nhập họ tên..."
helperText="Tên đầy đủ theo CMND"
[(model)]="basic"
[form]="form"></sd-input>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-validator') {
<demo-section
heading="Validator"
[props]="[
{ name: 'required', value: 'true' },
{ name: 'type', value: 'email' },
{ name: 'minlength', value: '6' },
]"
note="Bấm Kiểm tra để hiện lỗi inline.">
<div style="width: 320px; display:flex; flex-direction:column; gap:12px">
<sd-input
label="required + type=email"
placeholder="vd: a@b.com"
type="email"
[(model)]="email"
[form]="formValid"
required></sd-input>
<sd-input
label="required + minlength=6"
type="password"
[(model)]="password"
[form]="formValid"
required
[minlength]="6"></sd-input>
<div style="display:flex; gap:8px">
<button type="button" (click)="check()">Kiểm tra</button>
<button type="button" (click)="reset()">Đặt lại</button>
</div>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-cac-trang-thai-bao-loi-inline') {
<demo-section
heading="Các trạng thái báo lỗi (inline)"
[props]="[
{ name: 'required', value: 'true' },
{ name: 'minlength', value: '6' },
{ name: 'pattern', value: 'regex' },
{ name: '[validator]', value: 'fn' },
{ name: 'inlineError', value: 'text' },
]"
note="Mỗi ô minh hoạ một loại lỗi. Bấm Hiện lỗi để mark touched — lỗi xuất hiện dưới ô (đỏ). Ô [validator] cấm chữ 'admin'; gõ admin để thấy lỗi.">
<div style="width: 340px; display:flex; flex-direction:column; gap:12px">
<sd-input label="required (để trống)" [(model)]="errRequired" [form]="formErr" required></sd-input>
<sd-input label="minlength = 6" [(model)]="errMinLen" [form]="formErr" [minlength]="6"></sd-input>
<sd-input
label="pattern = 10 chữ số"
placeholder="vd: 0987654321"
[(model)]="errPattern"
[form]="formErr"
pattern="^\\d{10}$"
patternErrorMessage="Phải gồm đúng 10 chữ số"></sd-input>
<sd-input label="[validator] (cấm 'admin')" [(model)]="errValidator" [form]="formErr" [validator]="forbidAdmin"></sd-input>
<sd-input
label="inlineError (lỗi do cha truyền)"
[(model)]="errInline"
[form]="formErr"
[inlineError]="serverError()"></sd-input>
<div style="display:flex; gap:8px">
<button type="button" (click)="showErr()">Hiện lỗi</button>
<button type="button" (click)="resetErr()">Đặt lại</button>
</div>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-bao-loi-dang-icon-hideinlineerror') {
<demo-section
heading="Báo lỗi dạng icon (hideInlineError)"
[props]="[
{ name: 'hideInlineError', value: 'true' },
{ name: '[validator]', value: 'fn' },
]"
note="Khi hideInlineError=true: không có dòng lỗi dưới ô — thay vào đó icon ⚠ đỏ nằm sát mép phải, message hiện qua tooltip khi hover. Các ô đã có giá trị nên nút xoá (×) cũng hiện cạnh icon lỗi (xoá nằm bên trái, icon lỗi sát mép phải).">
<div style="width: 340px; display:flex; flex-direction:column; gap:12px">
<sd-input label="minlength = 6" [(model)]="iconMinLen" [form]="formIcon" [minlength]="6" hideInlineError></sd-input>
<sd-input
label="pattern = 10 chữ số"
[(model)]="iconPattern"
[form]="formIcon"
pattern="^\\d{10}$"
patternErrorMessage="Phải gồm đúng 10 chữ số"
hideInlineError></sd-input>
<sd-input
label="[validator] (cấm 'admin')"
[(model)]="iconValidator"
[form]="formIcon"
[validator]="forbidAdmin"
hideInlineError></sd-input>
<div style="display:flex; gap:8px">
<button type="button" (click)="showIcon()">Hiện lỗi</button>
<button type="button" (click)="resetIcon()">Đặt lại</button>
</div>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-trang-thai') {
<demo-section
heading="Trạng thái"
[props]="[
{ name: 'disabled', value: 'true' },
{ name: 'readonly', value: 'true' },
{ name: 'viewed', value: 'true' },
]"
note="Ba trạng thái không cho chỉnh sửa.">
<div style="display:flex; gap:16px; flex-wrap:wrap; width:100%">
<sd-input style="width: 220px" label="disabled" [(model)]="lockedA" [form]="form" disabled></sd-input>
<sd-input style="width: 220px" label="readonly" [(model)]="lockedB" [form]="form" readonly></sd-input>
<sd-input style="width: 220px" label="viewed" [(model)]="lockedC" [form]="form" viewed></sd-input>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-kich-thuoc') {
<demo-section
heading="Kích thước"
[props]="[{ name: 'size', value: 'sm' }, { name: 'required', value: 'true' }, { name: 'helperText', value: 'string' }]"
note="Label, tooltip và dấu bắt buộc cùng hàng ở size sm, kể cả khi có lỗi.">
<div style="width: 320px; max-width: 100%; display: grid; gap: 16px">
<sd-input label="sm" size="sm" placeholder="VD: NV001" [(model)]="codeSm" [form]="form"></sd-input>
<sd-input size="sm" label="Họ và tên" helperText="Nhập họ và tên đầy đủ" required inlineError="Vui lòng nhập thông tin"></sd-input>
<sd-input size="sm" label="Trường bắt buộc" required></sd-input>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-chinh-sua-noi-tuyen') {
<demo-section
heading="Chỉnh sửa nội tuyến"
[props]="[{ name: 'viewed', value: 'inline' }]"
note="Input trong suốt nhìn như text; bấm/focus là gõ trực tiếp (không có panel). Hover đậm nền.">
<div style="width: 260px; font-size:13px; color:#555">
Họ tên: <sd-input [viewed]="'inline'" [(model)]="lockedB" [form]="form"></sd-input>
</div>
</demo-section>
}
@if (!demoPage.focusedSectionId || demoPage.focusedSectionId === 'example-input-mask-raw-model-display-value') {
<demo-section
heading="Input mask: raw model / display value"
[props]="[
{ name: 'mask', value: 'VN_PHONE' },
{ name: 'model', value: maskedPhone() ?? 'null' },
]"
note="Màn hình hiển thị khoảng cách, nhưng model, sdChange và FormGroup chỉ nhận chuỗi số raw.">
<div style="width: 320px">
<sd-input label="Điện thoại" mask="VN_PHONE" [(model)]="maskedPhone" [form]="form"></sd-input>
</div>
</demo-section>
}
</demo-page>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class InputDemoComponent {
form = new FormGroup({});
formValid = new FormGroup({});
formErr = new FormGroup({});
formIcon = new FormGroup({});
basic = signal<string | null>('Nguyễn Văn A');
email = signal<string | null>(null);
password = signal<string | null>(null);
lockedA = signal<string | null>('Không thể sửa');
lockedB = signal<string | null>('Chỉ đọc');
lockedC = signal<string | null>('Chế độ xem');
codeSm = signal<string | null>(null);
maskedPhone = signal<string | null>('0901234567');
// Error-state demo (inline)
errRequired = signal<string | null>(null);
errMinLen = signal<string | null>('abc');
errPattern = signal<string | null>('12ab');
errValidator = signal<string | null>('admin');
errInline = signal<string | null>('user@corp.vn');
serverError = signal<string>('Email đã tồn tại trong hệ thống');
// Error-state demo (icon / hideInlineError) — pre-filled invalid values so the
// clear button also shows next to the error icon (demonstrates suffix ordering).
iconMinLen = signal<string | null>('abc');
iconPattern = signal<string | null>('12ab');
iconValidator = signal<string | null>('admin');
// why: SdCustomValidator = (value) => string | Promise<string>. Trả chuỗi rỗng = hợp lệ,
// trả message = lỗi. async để minh hoạ luồng validator bất đồng bộ ([validator] dùng async validator).
forbidAdmin = async (value: any): Promise<string> =>
(value ?? '').toString().trim().toLowerCase() === 'admin' ? 'Không được dùng "admin"' : '';
check() {
this.formValid.markAllAsTouched();
}
reset() {
this.formValid.reset();
this.formValid.markAsUntouched();
}
// why: "Đặt lại" KHÔNG dùng fg.reset() — reset() set control về null (rỗng) → minlength/pattern/
// validator không bắt lỗi trên giá trị rỗng → bấm "Hiện lỗi" lần nữa không thấy lỗi. Thay vào đó
// gieo lại đúng các giá trị sai mẫu (qua [(model)] signal) rồi markAsUntouched → demo lặp lại được.
showErr() {
this.formErr.markAllAsTouched();
}
resetErr() {
this.errRequired.set(null);
this.errMinLen.set('abc');
this.errPattern.set('12ab');
this.errValidator.set('admin');
this.errInline.set('user@corp.vn');
this.formErr.markAsUntouched();
}
showIcon() {
this.formIcon.markAllAsTouched();
}
resetIcon() {
this.iconMinLen.set('abc');
this.iconPattern.set('12ab');
this.iconValidator.set('admin');
this.formIcon.markAsUntouched();
}
}