@relax.js/core 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/README.md +194 -188
  2. package/dist/DependencyInjection.d.ts +42 -24
  3. package/dist/collections/Index.d.ts +2 -0
  4. package/dist/collections/index.js +1 -1
  5. package/dist/collections/index.js.map +4 -4
  6. package/dist/collections/index.mjs +1 -1
  7. package/dist/collections/index.mjs.map +4 -4
  8. package/dist/di/index.js +1 -1
  9. package/dist/di/index.js.map +3 -3
  10. package/dist/di/index.mjs +1 -1
  11. package/dist/di/index.mjs.map +3 -3
  12. package/dist/errors.d.ts +20 -0
  13. package/dist/forms/FormValidator.d.ts +1 -20
  14. package/dist/forms/ValidationRules.d.ts +2 -0
  15. package/dist/forms/index.js +1 -1
  16. package/dist/forms/index.js.map +4 -4
  17. package/dist/forms/index.mjs +1 -1
  18. package/dist/forms/index.mjs.map +4 -4
  19. package/dist/html/TableRenderer.d.ts +1 -0
  20. package/dist/html/index.js.map +2 -2
  21. package/dist/html/index.mjs.map +2 -2
  22. package/dist/html/template.d.ts +4 -0
  23. package/dist/http/http.d.ts +1 -0
  24. package/dist/http/index.js.map +2 -2
  25. package/dist/http/index.mjs.map +2 -2
  26. package/dist/index.d.ts +0 -2
  27. package/dist/index.js +3 -3
  28. package/dist/index.js.map +4 -4
  29. package/dist/index.mjs +3 -3
  30. package/dist/index.mjs.map +4 -4
  31. package/dist/routing/index.js +3 -3
  32. package/dist/routing/index.js.map +3 -3
  33. package/dist/routing/index.mjs +3 -3
  34. package/dist/routing/index.mjs.map +3 -3
  35. package/dist/routing/routeTargetRegistry.d.ts +1 -0
  36. package/dist/routing/types.d.ts +2 -1
  37. package/dist/templates/NodeTemplate.d.ts +2 -0
  38. package/dist/utils/index.js +1 -1
  39. package/dist/utils/index.js.map +2 -2
  40. package/dist/utils/index.mjs +1 -1
  41. package/dist/utils/index.mjs.map +2 -2
  42. package/docs/Architecture.md +333 -333
  43. package/docs/DependencyInjection.md +277 -237
  44. package/docs/Errors.md +87 -87
  45. package/docs/GettingStarted.md +231 -231
  46. package/docs/Pipes.md +5 -5
  47. package/docs/Translations.md +167 -312
  48. package/docs/WhyRelaxjs.md +336 -336
  49. package/docs/api/.nojekyll +1 -0
  50. package/docs/api/assets/hierarchy.js +1 -0
  51. package/docs/api/assets/highlight.css +120 -0
  52. package/docs/api/assets/icons.js +18 -0
  53. package/docs/api/assets/icons.svg +1 -0
  54. package/docs/api/assets/main.js +60 -0
  55. package/docs/api/assets/navigation.js +1 -0
  56. package/docs/api/assets/search.js +1 -0
  57. package/docs/api/assets/style.css +1633 -0
  58. package/docs/api/classes/http.WebSocketClient.html +26 -0
  59. package/docs/api/classes/i18n.LocaleChangeEvent.html +66 -0
  60. package/docs/api/classes/index.Blueprint.html +3 -0
  61. package/docs/api/classes/index.BoundNode.html +3 -0
  62. package/docs/api/classes/index.DigitsValidation.html +10 -0
  63. package/docs/api/classes/index.FormValidator.html +32 -0
  64. package/docs/api/classes/index.HttpError.html +13 -0
  65. package/docs/api/classes/index.LinkedList.html +26 -0
  66. package/docs/api/classes/index.NavigateRouteEvent.html +76 -0
  67. package/docs/api/classes/index.Node.html +15 -0
  68. package/docs/api/classes/index.PageSelectedEvent.html +61 -0
  69. package/docs/api/classes/index.Pager.html +4 -0
  70. package/docs/api/classes/index.RangeValidation.html +15 -0
  71. package/docs/api/classes/index.RelaxError.html +17 -0
  72. package/docs/api/classes/index.RequiredValidation.html +10 -0
  73. package/docs/api/classes/index.RouteError.html +11 -0
  74. package/docs/api/classes/index.RouteGuardError.html +12 -0
  75. package/docs/api/classes/index.RouteLink.html +779 -0
  76. package/docs/api/classes/index.RouteTarget.html +788 -0
  77. package/docs/api/classes/index.SSEClient.html +13 -0
  78. package/docs/api/classes/index.SSEDataEvent.html +63 -0
  79. package/docs/api/classes/index.ServiceCollection.html +28 -0
  80. package/docs/api/classes/index.ServiceContainer.html +24 -0
  81. package/docs/api/classes/index.SortChangeEvent.html +61 -0
  82. package/docs/api/classes/index.TableRenderer.html +5 -0
  83. package/docs/api/classes/index.TableSorter.html +4 -0
  84. package/docs/api/enums/index.GuardResult.html +9 -0
  85. package/docs/api/functions/elements.formError.html +6 -0
  86. package/docs/api/functions/elements.selectOne.html +6 -0
  87. package/docs/api/functions/i18n.getCurrentLocale.html +3 -0
  88. package/docs/api/functions/i18n.loadNamespace.html +7 -0
  89. package/docs/api/functions/i18n.loadNamespaces.html +6 -0
  90. package/docs/api/functions/i18n.onMissingTranslation.html +7 -0
  91. package/docs/api/functions/i18n.setLocale.html +7 -0
  92. package/docs/api/functions/i18n.setMessageFormatter.html +7 -0
  93. package/docs/api/functions/i18n.t.html +9 -0
  94. package/docs/api/functions/index.BooleanConverter.html +6 -0
  95. package/docs/api/functions/index.ContainerService.html +13 -0
  96. package/docs/api/functions/index.DateConverter.html +11 -0
  97. package/docs/api/functions/index.Inject.html +16 -0
  98. package/docs/api/functions/index.NumberConverter.html +5 -0
  99. package/docs/api/functions/index.RegisterValidator.html +7 -0
  100. package/docs/api/functions/index.applyPipes.html +17 -0
  101. package/docs/api/functions/index.asyncHandler.html +11 -0
  102. package/docs/api/functions/index.capitalizePipe.html +4 -0
  103. package/docs/api/functions/index.clearPendingNavigations.html +1 -0
  104. package/docs/api/functions/index.compileTemplate.html +26 -0
  105. package/docs/api/functions/index.configure.html +5 -0
  106. package/docs/api/functions/index.createBluePrint.html +1 -0
  107. package/docs/api/functions/index.createConverterFromDataType.html +4 -0
  108. package/docs/api/functions/index.createConverterFromInputType.html +5 -0
  109. package/docs/api/functions/index.createPipeRegistry.html +12 -0
  110. package/docs/api/functions/index.currencyPipe.html +9 -0
  111. package/docs/api/functions/index.datePipe.html +9 -0
  112. package/docs/api/functions/index.daysAgoPipe.html +8 -0
  113. package/docs/api/functions/index.defaultPipe.html +5 -0
  114. package/docs/api/functions/index.defineRoutes.html +8 -0
  115. package/docs/api/functions/index.del.html +8 -0
  116. package/docs/api/functions/index.findRouteByName.html +5 -0
  117. package/docs/api/functions/index.findRouteByUrl.html +4 -0
  118. package/docs/api/functions/index.firstPipe.html +4 -0
  119. package/docs/api/functions/index.generateSequentialId.html +21 -0
  120. package/docs/api/functions/index.get.html +9 -0
  121. package/docs/api/functions/index.getDataConverter.html +11 -0
  122. package/docs/api/functions/index.getParentComponent.html +18 -0
  123. package/docs/api/functions/index.getValidator.html +4 -0
  124. package/docs/api/functions/index.html.html +19 -0
  125. package/docs/api/functions/index.joinPipe.html +5 -0
  126. package/docs/api/functions/index.keysPipe.html +4 -0
  127. package/docs/api/functions/index.lastPipe.html +4 -0
  128. package/docs/api/functions/index.lowercasePipe.html +4 -0
  129. package/docs/api/functions/index.mapFormToClass.html +17 -0
  130. package/docs/api/functions/index.matchRoute.html +5 -0
  131. package/docs/api/functions/index.navigate.html +8 -0
  132. package/docs/api/functions/index.onError.html +8 -0
  133. package/docs/api/functions/index.piecesPipe.html +8 -0
  134. package/docs/api/functions/index.post.html +9 -0
  135. package/docs/api/functions/index.printRoutes.html +2 -0
  136. package/docs/api/functions/index.put.html +9 -0
  137. package/docs/api/functions/index.readData.html +17 -0
  138. package/docs/api/functions/index.registerRouteTarget.html +9 -0
  139. package/docs/api/functions/index.reportError.html +10 -0
  140. package/docs/api/functions/index.request.html +8 -0
  141. package/docs/api/functions/index.resolveValue.html +18 -0
  142. package/docs/api/functions/index.setFetch.html +6 -0
  143. package/docs/api/functions/index.setFormData.html +17 -0
  144. package/docs/api/functions/index.shortenPipe.html +5 -0
  145. package/docs/api/functions/index.startRouting.html +6 -0
  146. package/docs/api/functions/index.ternaryPipe.html +6 -0
  147. package/docs/api/functions/index.trimPipe.html +4 -0
  148. package/docs/api/functions/index.unregisterRouteTarget.html +3 -0
  149. package/docs/api/functions/index.uppercasePipe.html +4 -0
  150. package/docs/api/hierarchy.html +1 -0
  151. package/docs/api/index.html +323 -0
  152. package/docs/api/interfaces/http.SimpleDataEvent.html +3 -0
  153. package/docs/api/interfaces/http.WebSocketAbstraction.html +9 -0
  154. package/docs/api/interfaces/http.WebSocketCodec.html +4 -0
  155. package/docs/api/interfaces/http.WebSocketOptions.html +20 -0
  156. package/docs/api/interfaces/index.CompiledTemplate.html +10 -0
  157. package/docs/api/interfaces/index.DataLoader.html +19 -0
  158. package/docs/api/interfaces/index.EngineConfig.html +11 -0
  159. package/docs/api/interfaces/index.ErrorContext.html +4 -0
  160. package/docs/api/interfaces/index.FormReaderOptions.html +8 -0
  161. package/docs/api/interfaces/index.HttpOptions.html +16 -0
  162. package/docs/api/interfaces/index.HttpResponse.html +17 -0
  163. package/docs/api/interfaces/index.LoadRoute.html +7 -0
  164. package/docs/api/interfaces/index.NavigateOptions.html +7 -0
  165. package/docs/api/interfaces/index.PipeRegistry.html +12 -0
  166. package/docs/api/interfaces/index.RegistrationOptions.html +22 -0
  167. package/docs/api/interfaces/index.RenderTemplate.html +7 -0
  168. package/docs/api/interfaces/index.RequestOptions.html +11 -0
  169. package/docs/api/interfaces/index.Routable.html +10 -0
  170. package/docs/api/interfaces/index.Route.html +13 -0
  171. package/docs/api/interfaces/index.RouteGuard.html +2 -0
  172. package/docs/api/interfaces/index.RouteValue.html +6 -0
  173. package/docs/api/interfaces/index.SSEOptions.html +24 -0
  174. package/docs/api/interfaces/index.ValidationContext.html +8 -0
  175. package/docs/api/interfaces/index.ValidatorOptions.html +14 -0
  176. package/docs/api/media/Architecture.md +333 -0
  177. package/docs/api/media/DependencyInjection.md +277 -0
  178. package/docs/api/media/GettingStarted.md +231 -0
  179. package/docs/api/media/HttpClient.md +459 -0
  180. package/docs/api/media/Pipes.md +211 -0
  181. package/docs/api/media/Routing.md +332 -0
  182. package/docs/api/media/WhyRelaxjs.md +336 -0
  183. package/docs/api/media/forms.md +99 -0
  184. package/docs/api/media/html.md +175 -0
  185. package/docs/api/media/i18n.md +354 -0
  186. package/docs/api/media/utilities.md +143 -0
  187. package/docs/api/media/validation.md +351 -0
  188. package/docs/api/modules/collections_Index.html +1 -0
  189. package/docs/api/modules/di.html +1 -0
  190. package/docs/api/modules/elements.html +1 -0
  191. package/docs/api/modules/forms.html +1 -0
  192. package/docs/api/modules/html.html +1 -0
  193. package/docs/api/modules/http.html +1 -0
  194. package/docs/api/modules/i18n.html +1 -0
  195. package/docs/api/modules/index.html +1 -0
  196. package/docs/api/modules/routing.html +1 -0
  197. package/docs/api/modules/utils.html +1 -0
  198. package/docs/api/modules.html +1 -0
  199. package/docs/api/types/http.WebSocketFactory.html +2 -0
  200. package/docs/api/types/i18n.MessageFormatter.html +3 -0
  201. package/docs/api/types/i18n.MissingTranslationHandler.html +1 -0
  202. package/docs/api/types/index.Constructor.html +7 -0
  203. package/docs/api/types/index.ConverterFunc.html +2 -0
  204. package/docs/api/types/index.DataType.html +2 -0
  205. package/docs/api/types/index.InputType.html +2 -0
  206. package/docs/api/types/index.PipeFunction.html +6 -0
  207. package/docs/api/types/index.RouteData.html +1 -0
  208. package/docs/api/types/index.RouteMatchResult.html +9 -0
  209. package/docs/api/types/index.RouteParamType.html +1 -0
  210. package/docs/api/types/index.RouteSegmentType.html +2 -0
  211. package/docs/api/types/index.SSEEventFactory.html +5 -0
  212. package/docs/api/types/index.ServiceScope.html +10 -0
  213. package/docs/api/types/index.SortColumn.html +3 -0
  214. package/docs/api/variables/i18n.formatICU.html +3 -0
  215. package/docs/api/variables/index.container.html +6 -0
  216. package/docs/api/variables/index.defaultPipes.html +6 -0
  217. package/docs/api/variables/index.internalRoutes.html +1 -0
  218. package/docs/api/variables/index.serviceCollection.html +6 -0
  219. package/docs/api.json +93171 -0
  220. package/docs/elements/dom.md +102 -102
  221. package/docs/forms/creating-form-components.md +924 -924
  222. package/docs/forms/form-api.md +94 -94
  223. package/docs/forms/forms.md +99 -99
  224. package/docs/forms/patterns.md +311 -311
  225. package/docs/forms/reading-writing.md +365 -365
  226. package/docs/forms/validation.md +351 -351
  227. package/docs/html/TableRenderer.md +291 -291
  228. package/docs/html/html.md +175 -175
  229. package/docs/html/index.md +54 -54
  230. package/docs/html/template.md +422 -422
  231. package/docs/http/HttpClient.md +459 -459
  232. package/docs/http/ServerSentEvents.md +184 -184
  233. package/docs/http/index.md +109 -109
  234. package/docs/i18n/i18n.md +49 -4
  235. package/docs/i18n/intl-standard.md +178 -178
  236. package/docs/routing/RouteLink.md +98 -98
  237. package/docs/routing/Routing.md +332 -332
  238. package/docs/routing/layouts.md +207 -207
  239. package/docs/utilities.md +143 -143
  240. package/package.json +4 -3
@@ -1,311 +1,311 @@
1
- # Form Patterns
2
-
3
- Common patterns for building forms with RelaxJS utilities.
4
-
5
- ## Multi-Step Forms
6
-
7
- ```typescript
8
- class MultiStepForm {
9
- private currentStep = 1;
10
- private validators: FormValidator[] = [];
11
-
12
- constructor() {
13
- this.setupStepValidation();
14
- }
15
-
16
- setupStepValidation() {
17
- document.querySelectorAll('.form-step').forEach((step, index) => {
18
- const validator = new FormValidator(step as HTMLFormElement, {
19
- useSummary: true,
20
- preventDefault: true,
21
- submitCallback: () => this.nextStep()
22
- });
23
- this.validators.push(validator);
24
- });
25
- }
26
-
27
- nextStep() {
28
- if (this.validators[this.currentStep - 1].validateForm()) {
29
- this.currentStep++;
30
- this.showStep(this.currentStep);
31
- }
32
- }
33
-
34
- showStep(step: number) {
35
- document.querySelectorAll('.form-step').forEach((el, index) => {
36
- (el as HTMLElement).style.display = index + 1 === step ? 'block' : 'none';
37
- });
38
- }
39
- }
40
- ```
41
-
42
- ## Form State Management
43
-
44
- Track changes and auto-save drafts:
45
-
46
- ```typescript
47
- class FormStateManager {
48
- private formData: Record<string, unknown> = {};
49
- private isDirty = false;
50
-
51
- constructor(private form: HTMLFormElement) {
52
- this.captureInitialState();
53
- this.setupChangeTracking();
54
- }
55
-
56
- captureInitialState() {
57
- this.formData = readData(this.form);
58
- }
59
-
60
- setupChangeTracking() {
61
- this.form.addEventListener('input', () => {
62
- this.isDirty = true;
63
- this.updateSaveButton();
64
- });
65
-
66
- // Auto-save every 30 seconds if dirty
67
- setInterval(() => {
68
- if (this.isDirty) {
69
- this.autoSave();
70
- }
71
- }, 30000);
72
- }
73
-
74
- autoSave() {
75
- const currentData = readData(this.form);
76
- localStorage.setItem('form-draft', JSON.stringify(currentData));
77
- this.isDirty = false;
78
- this.showSaveIndicator('Auto-saved');
79
- }
80
-
81
- restoreDraft() {
82
- const draft = localStorage.getItem('form-draft');
83
- if (draft) {
84
- const data = JSON.parse(draft);
85
- setFormData(this.form, data);
86
- }
87
- }
88
-
89
- hasChanges(): boolean {
90
- const current = readData(this.form);
91
- return JSON.stringify(current) !== JSON.stringify(this.formData);
92
- }
93
- }
94
- ```
95
-
96
- ## Web Component Integration
97
-
98
- ```typescript
99
- class UserProfileEditor extends HTMLElement {
100
- private form: HTMLFormElement;
101
- private validator: FormValidator;
102
- private user: UserProfile;
103
-
104
- connectedCallback() {
105
- this.innerHTML = `
106
- <form>
107
- <input name="displayName" placeholder="Display Name" required>
108
- <input name="email" type="email" placeholder="Email" required>
109
- <input name="preferences.notifications" type="checkbox"> Notifications
110
- <input name="preferences.theme" type="radio" value="light"> Light
111
- <input name="preferences.theme" type="radio" value="dark"> Dark
112
- </form>
113
- `;
114
-
115
- this.form = FormValidator.FindForm(this);
116
- this.user = new UserProfile();
117
-
118
- this.validator = new FormValidator(this.form, {
119
- useSummary: true,
120
- autoValidate: true,
121
- submitCallback: () => this.saveProfile()
122
- });
123
-
124
- this.loadUserData();
125
- }
126
-
127
- async loadUserData() {
128
- const userData = await fetchUserProfile();
129
- setFormData(this.form, userData);
130
- }
131
-
132
- saveProfile() {
133
- mapFormToClass(this.form, this.user);
134
-
135
- if (this.user.displayName.length < 2) {
136
- this.validator.addErrorToSummary('Display Name', 'Must be at least 2 characters');
137
- return;
138
- }
139
-
140
- updateUserProfile(this.user);
141
- }
142
- }
143
-
144
- customElements.define('user-profile-editor', UserProfileEditor);
145
- ```
146
-
147
- ## Dynamic Survey Form
148
-
149
- Handle forms with dynamic question types:
150
-
151
- ```typescript
152
- class SurveyResponse {
153
- userId: number = 0;
154
- responses: Record<string, string | number | boolean> = {};
155
- completedAt: Date = new Date();
156
- }
157
-
158
- const surveyForm = document.querySelector('#survey-form') as HTMLFormElement;
159
- const response = new SurveyResponse();
160
-
161
- // Custom data extraction for complex question types
162
- document.querySelectorAll('[data-question-type="rating"]').forEach(element => {
163
- (element as any).getData = function() {
164
- const stars = this.querySelectorAll('.star.selected');
165
- return stars.length;
166
- };
167
- });
168
-
169
- const validator = new FormValidator(surveyForm, {
170
- autoValidate: true,
171
- customChecks: (form) => {
172
- const required = form.querySelectorAll('[data-required]');
173
- required.forEach(field => {
174
- const input = field as HTMLInputElement;
175
- if (!input.value && input.type !== 'checkbox') {
176
- const questionText = field.getAttribute('data-question');
177
- validator.addErrorToSummary(questionText, 'This question is required');
178
- }
179
- });
180
- },
181
- submitCallback: () => {
182
- response.responses = readData(surveyForm);
183
- response.completedAt = new Date();
184
- submitSurvey(response);
185
- }
186
- });
187
- ```
188
-
189
- ## Unsaved Changes Warning
190
-
191
- Warn users before leaving with unsaved changes:
192
-
193
- ```typescript
194
- class UnsavedChangesGuard {
195
- private initialData: string;
196
-
197
- constructor(private form: HTMLFormElement) {
198
- this.initialData = JSON.stringify(readData(form));
199
- this.setupWarning();
200
- }
201
-
202
- setupWarning() {
203
- window.addEventListener('beforeunload', (e) => {
204
- if (this.hasUnsavedChanges()) {
205
- e.preventDefault();
206
- e.returnValue = '';
207
- }
208
- });
209
- }
210
-
211
- hasUnsavedChanges(): boolean {
212
- return JSON.stringify(readData(this.form)) !== this.initialData;
213
- }
214
-
215
- markAsSaved() {
216
- this.initialData = JSON.stringify(readData(this.form));
217
- }
218
- }
219
- ```
220
-
221
- ## Form Reset with Confirmation
222
-
223
- ```typescript
224
- class ResettableForm {
225
- private initialData: Record<string, unknown>;
226
-
227
- constructor(private form: HTMLFormElement) {
228
- this.initialData = readData(form);
229
- this.setupResetButton();
230
- }
231
-
232
- setupResetButton() {
233
- const resetBtn = this.form.querySelector('[type="reset"]');
234
- resetBtn?.addEventListener('click', (e) => {
235
- e.preventDefault();
236
- if (confirm('Are you sure you want to reset the form?')) {
237
- setFormData(this.form, this.initialData);
238
- }
239
- });
240
- }
241
- }
242
- ```
243
-
244
- ## Conditional Field Visibility
245
-
246
- ```typescript
247
- class ConditionalForm {
248
- constructor(private form: HTMLFormElement) {
249
- this.setupConditionalFields();
250
- }
251
-
252
- setupConditionalFields() {
253
- this.form.querySelectorAll('[data-show-when]').forEach(field => {
254
- const condition = field.getAttribute('data-show-when');
255
- const [fieldName, expectedValue] = condition.split('=');
256
-
257
- const triggerField = this.form.querySelector(`[name="${fieldName}"]`);
258
- triggerField?.addEventListener('change', () => {
259
- const currentValue = (triggerField as HTMLInputElement).value;
260
- (field as HTMLElement).style.display =
261
- currentValue === expectedValue ? 'block' : 'none';
262
- });
263
- });
264
- }
265
- }
266
- ```
267
-
268
- Usage:
269
-
270
- ```html
271
- <form>
272
- <select name="contactMethod">
273
- <option value="email">Email</option>
274
- <option value="phone">Phone</option>
275
- </select>
276
-
277
- <input name="email" data-show-when="contactMethod=email" placeholder="Email">
278
- <input name="phone" data-show-when="contactMethod=phone" placeholder="Phone">
279
- </form>
280
- ```
281
-
282
- ## Form Data Comparison
283
-
284
- ```typescript
285
- class FormComparison {
286
- static getDiff(
287
- original: Record<string, unknown>,
288
- current: Record<string, unknown>
289
- ): Record<string, { old: unknown; new: unknown }> {
290
- const diff: Record<string, { old: unknown; new: unknown }> = {};
291
-
292
- for (const key in current) {
293
- if (JSON.stringify(original[key]) !== JSON.stringify(current[key])) {
294
- diff[key] = {
295
- old: original[key],
296
- new: current[key]
297
- };
298
- }
299
- }
300
-
301
- return diff;
302
- }
303
- }
304
-
305
- // Usage
306
- const original = readData(form);
307
- // ... user makes changes ...
308
- const current = readData(form);
309
- const changes = FormComparison.getDiff(original, current);
310
- console.log('Changed fields:', changes);
311
- ```
1
+ # Form Patterns
2
+
3
+ Common patterns for building forms with RelaxJS utilities.
4
+
5
+ ## Multi-Step Forms
6
+
7
+ ```typescript
8
+ class MultiStepForm {
9
+ private currentStep = 1;
10
+ private validators: FormValidator[] = [];
11
+
12
+ constructor() {
13
+ this.setupStepValidation();
14
+ }
15
+
16
+ setupStepValidation() {
17
+ document.querySelectorAll('.form-step').forEach((step, index) => {
18
+ const validator = new FormValidator(step as HTMLFormElement, {
19
+ useSummary: true,
20
+ preventDefault: true,
21
+ submitCallback: () => this.nextStep()
22
+ });
23
+ this.validators.push(validator);
24
+ });
25
+ }
26
+
27
+ nextStep() {
28
+ if (this.validators[this.currentStep - 1].validateForm()) {
29
+ this.currentStep++;
30
+ this.showStep(this.currentStep);
31
+ }
32
+ }
33
+
34
+ showStep(step: number) {
35
+ document.querySelectorAll('.form-step').forEach((el, index) => {
36
+ (el as HTMLElement).style.display = index + 1 === step ? 'block' : 'none';
37
+ });
38
+ }
39
+ }
40
+ ```
41
+
42
+ ## Form State Management
43
+
44
+ Track changes and auto-save drafts:
45
+
46
+ ```typescript
47
+ class FormStateManager {
48
+ private formData: Record<string, unknown> = {};
49
+ private isDirty = false;
50
+
51
+ constructor(private form: HTMLFormElement) {
52
+ this.captureInitialState();
53
+ this.setupChangeTracking();
54
+ }
55
+
56
+ captureInitialState() {
57
+ this.formData = readData(this.form);
58
+ }
59
+
60
+ setupChangeTracking() {
61
+ this.form.addEventListener('input', () => {
62
+ this.isDirty = true;
63
+ this.updateSaveButton();
64
+ });
65
+
66
+ // Auto-save every 30 seconds if dirty
67
+ setInterval(() => {
68
+ if (this.isDirty) {
69
+ this.autoSave();
70
+ }
71
+ }, 30000);
72
+ }
73
+
74
+ autoSave() {
75
+ const currentData = readData(this.form);
76
+ localStorage.setItem('form-draft', JSON.stringify(currentData));
77
+ this.isDirty = false;
78
+ this.showSaveIndicator('Auto-saved');
79
+ }
80
+
81
+ restoreDraft() {
82
+ const draft = localStorage.getItem('form-draft');
83
+ if (draft) {
84
+ const data = JSON.parse(draft);
85
+ setFormData(this.form, data);
86
+ }
87
+ }
88
+
89
+ hasChanges(): boolean {
90
+ const current = readData(this.form);
91
+ return JSON.stringify(current) !== JSON.stringify(this.formData);
92
+ }
93
+ }
94
+ ```
95
+
96
+ ## Web Component Integration
97
+
98
+ ```typescript
99
+ class UserProfileEditor extends HTMLElement {
100
+ private form: HTMLFormElement;
101
+ private validator: FormValidator;
102
+ private user: UserProfile;
103
+
104
+ connectedCallback() {
105
+ this.innerHTML = `
106
+ <form>
107
+ <input name="displayName" placeholder="Display Name" required>
108
+ <input name="email" type="email" placeholder="Email" required>
109
+ <input name="preferences.notifications" type="checkbox"> Notifications
110
+ <input name="preferences.theme" type="radio" value="light"> Light
111
+ <input name="preferences.theme" type="radio" value="dark"> Dark
112
+ </form>
113
+ `;
114
+
115
+ this.form = FormValidator.FindForm(this);
116
+ this.user = new UserProfile();
117
+
118
+ this.validator = new FormValidator(this.form, {
119
+ useSummary: true,
120
+ autoValidate: true,
121
+ submitCallback: () => this.saveProfile()
122
+ });
123
+
124
+ this.loadUserData();
125
+ }
126
+
127
+ async loadUserData() {
128
+ const userData = await fetchUserProfile();
129
+ setFormData(this.form, userData);
130
+ }
131
+
132
+ saveProfile() {
133
+ mapFormToClass(this.form, this.user);
134
+
135
+ if (this.user.displayName.length < 2) {
136
+ this.validator.addErrorToSummary('Display Name', 'Must be at least 2 characters');
137
+ return;
138
+ }
139
+
140
+ updateUserProfile(this.user);
141
+ }
142
+ }
143
+
144
+ customElements.define('user-profile-editor', UserProfileEditor);
145
+ ```
146
+
147
+ ## Dynamic Survey Form
148
+
149
+ Handle forms with dynamic question types:
150
+
151
+ ```typescript
152
+ class SurveyResponse {
153
+ userId: number = 0;
154
+ responses: Record<string, string | number | boolean> = {};
155
+ completedAt: Date = new Date();
156
+ }
157
+
158
+ const surveyForm = document.querySelector('#survey-form') as HTMLFormElement;
159
+ const response = new SurveyResponse();
160
+
161
+ // Custom data extraction for complex question types
162
+ document.querySelectorAll('[data-question-type="rating"]').forEach(element => {
163
+ (element as any).getData = function() {
164
+ const stars = this.querySelectorAll('.star.selected');
165
+ return stars.length;
166
+ };
167
+ });
168
+
169
+ const validator = new FormValidator(surveyForm, {
170
+ autoValidate: true,
171
+ customChecks: (form) => {
172
+ const required = form.querySelectorAll('[data-required]');
173
+ required.forEach(field => {
174
+ const input = field as HTMLInputElement;
175
+ if (!input.value && input.type !== 'checkbox') {
176
+ const questionText = field.getAttribute('data-question');
177
+ validator.addErrorToSummary(questionText, 'This question is required');
178
+ }
179
+ });
180
+ },
181
+ submitCallback: () => {
182
+ response.responses = readData(surveyForm);
183
+ response.completedAt = new Date();
184
+ submitSurvey(response);
185
+ }
186
+ });
187
+ ```
188
+
189
+ ## Unsaved Changes Warning
190
+
191
+ Warn users before leaving with unsaved changes:
192
+
193
+ ```typescript
194
+ class UnsavedChangesGuard {
195
+ private initialData: string;
196
+
197
+ constructor(private form: HTMLFormElement) {
198
+ this.initialData = JSON.stringify(readData(form));
199
+ this.setupWarning();
200
+ }
201
+
202
+ setupWarning() {
203
+ window.addEventListener('beforeunload', (e) => {
204
+ if (this.hasUnsavedChanges()) {
205
+ e.preventDefault();
206
+ e.returnValue = '';
207
+ }
208
+ });
209
+ }
210
+
211
+ hasUnsavedChanges(): boolean {
212
+ return JSON.stringify(readData(this.form)) !== this.initialData;
213
+ }
214
+
215
+ markAsSaved() {
216
+ this.initialData = JSON.stringify(readData(this.form));
217
+ }
218
+ }
219
+ ```
220
+
221
+ ## Form Reset with Confirmation
222
+
223
+ ```typescript
224
+ class ResettableForm {
225
+ private initialData: Record<string, unknown>;
226
+
227
+ constructor(private form: HTMLFormElement) {
228
+ this.initialData = readData(form);
229
+ this.setupResetButton();
230
+ }
231
+
232
+ setupResetButton() {
233
+ const resetBtn = this.form.querySelector('[type="reset"]');
234
+ resetBtn?.addEventListener('click', (e) => {
235
+ e.preventDefault();
236
+ if (confirm('Are you sure you want to reset the form?')) {
237
+ setFormData(this.form, this.initialData);
238
+ }
239
+ });
240
+ }
241
+ }
242
+ ```
243
+
244
+ ## Conditional Field Visibility
245
+
246
+ ```typescript
247
+ class ConditionalForm {
248
+ constructor(private form: HTMLFormElement) {
249
+ this.setupConditionalFields();
250
+ }
251
+
252
+ setupConditionalFields() {
253
+ this.form.querySelectorAll('[data-show-when]').forEach(field => {
254
+ const condition = field.getAttribute('data-show-when');
255
+ const [fieldName, expectedValue] = condition.split('=');
256
+
257
+ const triggerField = this.form.querySelector(`[name="${fieldName}"]`);
258
+ triggerField?.addEventListener('change', () => {
259
+ const currentValue = (triggerField as HTMLInputElement).value;
260
+ (field as HTMLElement).style.display =
261
+ currentValue === expectedValue ? 'block' : 'none';
262
+ });
263
+ });
264
+ }
265
+ }
266
+ ```
267
+
268
+ Usage:
269
+
270
+ ```html
271
+ <form>
272
+ <select name="contactMethod">
273
+ <option value="email">Email</option>
274
+ <option value="phone">Phone</option>
275
+ </select>
276
+
277
+ <input name="email" data-show-when="contactMethod=email" placeholder="Email">
278
+ <input name="phone" data-show-when="contactMethod=phone" placeholder="Phone">
279
+ </form>
280
+ ```
281
+
282
+ ## Form Data Comparison
283
+
284
+ ```typescript
285
+ class FormComparison {
286
+ static getDiff(
287
+ original: Record<string, unknown>,
288
+ current: Record<string, unknown>
289
+ ): Record<string, { old: unknown; new: unknown }> {
290
+ const diff: Record<string, { old: unknown; new: unknown }> = {};
291
+
292
+ for (const key in current) {
293
+ if (JSON.stringify(original[key]) !== JSON.stringify(current[key])) {
294
+ diff[key] = {
295
+ old: original[key],
296
+ new: current[key]
297
+ };
298
+ }
299
+ }
300
+
301
+ return diff;
302
+ }
303
+ }
304
+
305
+ // Usage
306
+ const original = readData(form);
307
+ // ... user makes changes ...
308
+ const current = readData(form);
309
+ const changes = FormComparison.getDiff(original, current);
310
+ console.log('Changed fields:', changes);
311
+ ```