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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | /**
* @module module:lib/components/stepped-form/stepped-form.component
* @description Stepped form component module.
* @summary Provides `SteppedFormComponent` which implements a multi-page form
* UI with navigation, validation and submission support. Useful for forms that
* need to be split into logical steps/pages.
*
* @link {@link SteppedFormComponent}
*/
import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { TranslatePipe } from '@ngx-translate/core';
import { IonButton, IonSkeletonText, IonText } from '@ionic/angular/standalone';
import { arrowForwardOutline, arrowBackOutline } from 'ionicons/icons';
import { addIcons } from 'ionicons';
import { UIElementMetadata, ComponentEventNames, UIModelMetadata } from '@decaf-ts/ui-decorators';
import { CrudOperations, OperationKeys } from '@decaf-ts/db-decorators';
import { Dynamic } from '../../engine/decorators';
import { NgxFormService } from '../../services/NgxFormService';
import { getLocaleContext } from '../../i18n/Loader';
import { LayoutComponent } from '../layout/layout.component';
import { NgxFormDirective } from '../../engine/NgxFormDirective';
import { FormParent } from '../../engine/types';
@Dynamic()
@Component({
selector: 'ngx-decaf-stepped-form',
templateUrl: './stepped-form.component.html',
styleUrls: ['./stepped-form.component.scss'],
imports: [
TranslatePipe,
ReactiveFormsModule,
IonSkeletonText,
IonText,
IonButton,
LayoutComponent,
],
standalone: true,
host: { '[attr.id]': 'uid' },
})
export class SteppedFormComponent extends NgxFormDirective implements OnInit, OnDestroy {
/**
* @description Array of UI model metadata for all form fields.
* @summary Contains the complete collection of UI model metadata that defines
* the structure, validation, and presentation of form fields across all pages.
* Each metadata object contains information about field type, validation rules,
* page assignment, and display properties.
*
* @type {UIModelMetadata[]}
* @memberOf SteppedFormComponent
*/
@Input()
override children!:
| UIModelMetadata[]
| { title: string; description: string; items?: UIModelMetadata[] }[];
/**
* @description The locale to be used for translations.
* @summary Specifies the locale identifier to use when translating component text.
* This can be set explicitly via input property to override the automatically derived
* locale from the component name. The locale is typically a language code (e.g., 'en', 'fr')
* or a language-region code (e.g., 'en-US', 'fr-CA') that determines which translation
* set to use for the component's text content.
*
* @type {string}
* @memberOf SteppedFormComponent
*/
@Input()
paginated: boolean = true;
// /**
// * @description Optional action identifier for form submission context.
// * @summary Specifies a custom action name that will be included in the submit event.
// * If not provided, defaults to the standard submit event constant. Used to
// * distinguish between different types of form submissions within the same component.
// *
// * @type {string | undefined}
// */
// @Input()
// action?: string;
/**
* @description Number of pages in the stepped form.
* @summary Represents the total number of steps/pages in the multi-step form.
* This value is automatically calculated based on the page properties of the children
* or can be explicitly set. Each page represents a logical group of form fields.
*
* @type {number}
* @default 1
* @memberOf SteppedFormComponent
*/
@Input()
override pages: number | { title: string; description: string }[] = 1;
/**
* List of titles and descriptions for each page of the stepped form.
* Each object in the array represents a page, containing a title and a description.
*
* @example
* pageTitles = [
* { title: 'Personal Information', description: 'Fill in your personal details.' },
* { title: 'Address', description: 'Provide your residential address.' }
* ];
*/
@Input()
pageTitles: { title: string; description: string }[] = [];
/**
* @description The CRUD operation type for this form.
* @summary Defines the type of operation being performed (CREATE, READ, UPDATE, DELETE).
* This affects form behavior, validation rules, and field accessibility. For example,
* READ operations might disable form fields, while CREATE operations enable all fields.
*
* @type {CrudOperations}
* @default OperationKeys.CREATE
* @memberOf SteppedFormComponent
*/
@Input()
override operation: CrudOperations = OperationKeys.CREATE;
/**
* @description The initial page to display when the form loads.
* @summary Specifies which page of the multi-step form should be shown first.
* This allows starting the form at any step, useful for scenarios like editing
* existing data where you might want to jump to a specific section.
*
* @type {number}
* @default 1
* @memberOf SteppedFormComponent
*/
@Input()
startPage: number = 1;
// /**
// * @description Angular reactive FormGroup or FormArray for form state management.
// * @summary The form instance that manages all form controls, validation, and form state.
// * When using FormArray, each array element represents a page's FormGroup. When using
// * FormGroup, it contains all form controls for the entire stepped form.
// *
// * @type {FormGroup | FormArray | undefined}
// * @memberOf SteppedFormComponent
// */
// @Input()
// formGroup!: FormGroup | FormArray | undefined;
/**
* @description Array representing the structure of pages.
* @summary Contains metadata about each page, including page numbers and indices.
* This array is built during initialization to organize the form fields into
* logical pages and provide navigation structure.
*
* @type {UIModelMetadata[]}
* @memberOf SteppedFormComponent
*/
pagesArray: UIModelMetadata[] = [];
// /**
// * @description Custom event handlers for form actions.
// * @summary A record of event handler functions keyed by event names that can be
// * triggered during form operations. These handlers provide extensibility for
// * custom business logic and can be invoked for various form events and actions.
// *
// * @type {HandlerLike}
// */
// @Input()
// handlers!: HandlerLike;
/**
* @description Event emitter for form submission.
* @summary Emits events when the form is submitted, typically on the last page
* when all validation passes. The emitted event contains the form data and
* event type information for parent components to handle.
*
* @type {EventEmitter<IBaseCustomEvent>}
* @memberOf SteppedFormComponent
*/
// @Output()
// submitEvent: EventEmitter<ICrudFormEvent> = new EventEmitter<ICrudFormEvent>();
/**
* @description Creates an instance of SteppedFormComponent.
* @summary Initializes a new SteppedFormComponent instance and registers the required
* Ionic icons for navigation buttons (forward and back arrows).
*
* @memberOf SteppedFormComponent
*/
constructor() {
super('SteppedFormComponent');
addIcons({ arrowForwardOutline, arrowBackOutline });
this.enableDarkMode = true;
}
/**
* @description Initializes the component after Angular first displays the data-bound properties.
* @summary Sets up the stepped form by organizing children into pages, calculating the total
* number of pages, and initializing the active page. This method processes the UI model metadata
* to create a logical page structure and ensures proper page assignments for all form fields.
*
* @mermaid
* sequenceDiagram
* participant A as Angular Lifecycle
* participant S as SteppedFormComponent
* participant F as Form Service
*
* A->>S: ngOnInit()
* S->>S: Set activeIndex = startPage
* S->>S: Calculate total pages
* S->>S: Assign page props to children
* S->>S: getCurrentFormGroup(activeIndex)
* S->>F: Extract FormGroup for active page
* F-->>S: Return activeFormGroup
*
* @memberOf SteppedFormComponent
*/
async ngOnInit(): Promise<void> {
Iif (!this.locale) this.locale = getLocaleContext('SteppedFormComponent');
this.activeIndex = this.startPage;
if (typeof this.pages === 'object') {
this.pageTitles = this.pages;
} else {
Iif (!this.pageTitles.length)
this.pageTitles = Array.from({ length: this.pages }, () => ({
title: '',
description: '',
}));
}
this.pages = this.pageTitles.length;
if (this.paginated) {
Iif (!this.parentForm)
this.parentForm = (this.formGroup?.root || this.formGroup) as FormParent;
this.children = [
...(this.children as UIModelMetadata[]).map((c) => {
Iif (!c.props) c.props = {};
const page = c.props['page'] || 1;
// prevent page overflow
c.props['page'] = page > this.pages ? this.pages : page;
return c;
}),
];
this.getActivePage(this.activeIndex);
} else {
this.children = this.pageTitles.map((page, index) => {
const pageNumber = index + 1;
const items = (this.children as UIModelMetadata[]).filter(
({ props }: UIElementMetadata) => props?.['page'] === pageNumber,
);
return {
page: pageNumber,
title: page.title,
description: page.description,
items,
};
});
// this.formGroup = new FormGroup(
// (this.formGroup as FormArray).controls.reduce((acc, control, index) => {
// acc[index] = control as FormGroup;
// return acc;
// }, {} as Record<string, FormGroup>)
// );
}
await super.initialize();
}
/**
* @description Cleanup method called when the component is destroyed.
* @summary Unsubscribes from any active timer subscriptions to prevent memory leaks.
* This is part of Angular's component lifecycle and ensures proper resource cleanup.
*
* @memberOf SteppedFormComponent
*/
override async ngOnDestroy(): Promise<void> {
await super.ngOnDestroy();
Iif (this.timerSubscription) this.timerSubscription.unsubscribe();
}
/**
* @description Handles navigation to the next page or form submission.
* @summary Validates the current page's form fields and either navigates to the next page
* or submits the entire form if on the last page. Form validation must pass before
* proceeding. On successful submission, emits a submit event with form data.
*
* @param {boolean} lastPage - Whether this is the last page of the form
* @return {void}
*
* @mermaid
* sequenceDiagram
* participant U as User
* participant S as SteppedFormComponent
* participant F as Form Service
* participant P as Parent Component
*
* U->>S: Click Next/Submit
* S->>F: validateFields(activeFormGroup)
* F-->>S: Return validation result
* alt Not last page and valid
* S->>S: activeIndex++
* S->>S: getCurrentFormGroup(activeIndex)
* else Last page and valid
* S->>F: getFormData(formGroup)
* F-->>S: Return form data
* S->>P: submitEvent.emit({data, name: SUBMIT})
* end
*
* @memberOf SteppedFormComponent
*/
handleNext(lastPage: boolean = false): void {
const isValid = NgxFormService.validateFields(this.formGroup as FormGroup);
// const isValid = this.paginated ?
// NgxFormService.validateFields(this.formGroup as FormGroup) :
// (this.formGroup as FormArray)?.controls.every(control => NgxFormService.validateFields(control as FormGroup));
if (!lastPage) {
Iif (isValid) {
this.activeIndex = this.activeIndex + 1;
this.getActivePage(this.activeIndex);
}
} else {
Iif (isValid) {
const rootForm = this.formGroup?.root || this.formGroup;
const data = Object.assign(
{},
...Object.values(NgxFormService.getFormData(rootForm as FormGroup)),
);
super.submitEventEmit(
data,
'SteppedFormComponent',
this.action || ComponentEventNames.Submit,
this.handlers,
);
}
}
}
/**
* @description Handles navigation to the previous page.
* @summary Moves the user back to the previous page in the stepped form.
* This method decrements the active page number and updates the form
* group and children to display the previous page's content.
*
* @return {void}
*
* @mermaid
* sequenceDiagram
* participant U as User
* participant S as SteppedFormComponent
*
* U->>S: Click Back
* S->>S: activeIndex--
* S->>S: getCurrentFormGroup(activeIndex)
* Note over S: Update active form and children
*
* @memberOf SteppedFormComponent
*/
handleBack(): void {
Iif (!this.paginated) return this.location.back();
this.activeIndex = this.activeIndex - 1;
this.getActivePage(this.activeIndex);
}
// async submit(event?: SubmitEvent, eventName?: string, componentName?: string): Promise<boolean | void> {
// if (event) {
// event.preventDefault();
// event.stopImmediatePropagation();
// }
// if (!NgxFormService.validateFields(this.formGroup as FormGroup))
// return false;
// const data = NgxFormService.getFormData(this.formGroup as FormGroup);
// this.submitEvent.emit({
// data,
// component: componentName || this.componentName,
// name: eventName || this.action || ComponentEventNames.Submit,
// handlers: this.handlers,
// });
// }
}
|