ngx-form-stepper 0.0.1

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.
package/README.md ADDED
@@ -0,0 +1,376 @@
1
+ # ngx-form-stepper
2
+
3
+ [🇫🇷 Lire la version française](README.fr.md)
4
+
5
+ **ngx-form-stepper** is an Angular library to create multi-step forms with field-level validations, **extremely typed**.
6
+
7
+ It prevents creating invalid states **at development time**, not at runtime.
8
+
9
+ Intended for Angular developers who want robust, typed, and maintainable forms without complex configuration.
10
+
11
+ ## Why ?
12
+
13
+ - Simple multi-step forms declaration
14
+ - Quick per-field validation setup
15
+ - Impossible to associate a wrong `validator` to an `Input`
16
+ - Values always consistent with their type
17
+ - Unique return keys required
18
+ - **No `as const` needed**
19
+
20
+ ## Quick example
21
+
22
+ ```typescript
23
+ step1 = new Step([
24
+ new Input(InputType.Text, null, 'firstName', 'First name', [required('First name is required')]),
25
+ new Input(InputType.Text, null, 'lastName', 'Last name', [required('Last name is required')]),
26
+ ]);
27
+
28
+ step2 = new Step([
29
+ new Input(InputType.Email, null, 'email', 'E-mail', [
30
+ required('E-mail is required'),
31
+ email('E-mail is invalid'),
32
+ ]),
33
+ new Input(InputType.Password, null, 'password', 'Password', [
34
+ required('Password is required'),
35
+ strongPassword('Password is too weak'),
36
+ ]),
37
+ ]);
38
+
39
+ signupForm = new FormStepper([step1, step2], {
40
+ title: 'Sign in',
41
+ buttonText: { next: 'Next', previous: 'Previous', final: 'Sign up' },
42
+ });
43
+
44
+ onComplete() {
45
+ console.log(signupForm.values);
46
+ }
47
+ ```
48
+
49
+ ```html
50
+ <app-form-stepper [formStepper]="signupForm" (completed)="onComplete()" />
51
+ ```
52
+
53
+ ## Input
54
+
55
+ Each `Input` type only accepts compatible `validators`.
56
+
57
+ Examples :
58
+
59
+ - `email` ❌ forbidden on `number`
60
+ - `minLength` ❌ forbidden on `checkbox`
61
+ - `confirm` ❌ forbidden on `select`
62
+
63
+ And only default values that are compatible.
64
+
65
+ Examples :
66
+
67
+ - `string` ❌ forbidden on `number`
68
+ - `number` ❌ forbidden on `checkbox`
69
+ - `string | number` ❌ forbidden on `select`
70
+
71
+ Return key must be camelCase.
72
+
73
+ Duplicating a `validator` is impossible.
74
+
75
+ ```typescript
76
+ export class Input<
77
+ T extends InputType,
78
+ D extends InputDefaultValue<T>,
79
+ K extends string,
80
+ V extends ValidatorTuple<ValidatorsNamesOfType<T>>
81
+ > {
82
+ readonly defaultValue: D;
83
+
84
+ constructor(
85
+ readonly type: T,
86
+ defaultValue: D,
87
+ readonly returnKey: IsCamelCase<K> extends true ? K : never,
88
+ readonly label: string,
89
+ readonly validators?: HasDuplicateValidators<V> extends true ? never : V
90
+ ) {
91
+ this.defaultValue = (
92
+ type === InputType.Checkbox ? (defaultValue === null ? false : defaultValue) : defaultValue
93
+ ) as D;
94
+ }
95
+ }
96
+
97
+ export enum InputType {
98
+ Text = 'text',
99
+ Password = 'password',
100
+ Email = 'email',
101
+ Number = 'number',
102
+ Tel = 'tel',
103
+ Checkbox = 'checkbox',
104
+ Date = 'date',
105
+ Select = 'select',
106
+ }
107
+ ```
108
+
109
+ ## Validator
110
+
111
+ A `validator` is a function that can be passed to an `Input`. It takes different arguments like conditional values or error text.
112
+
113
+ ```typescript
114
+ export function minLength(min: number, errorText: string): Validator<'minLength'> {
115
+ const name: StandardValidatorNameFn<'minLength'> = (params: { key: string }) =>
116
+ `${params.key}-minLength`;
117
+
118
+ const fn = (params: { key: string }) => (control: AbstractControl<string>) => {
119
+ const customName: StandardValidatorName<'minLength'> = `${params.key}-minLength`;
120
+
121
+ return control.value.length < min ? { [customName]: true } : null;
122
+ };
123
+
124
+ return {
125
+ kind: 'minLength',
126
+ name,
127
+ fn,
128
+ errorText,
129
+ };
130
+ }
131
+
132
+ export type ValidatorsNames =
133
+ | 'required'
134
+ | 'check'
135
+ | 'confirm'
136
+ | 'minLength'
137
+ | 'maxLength'
138
+ | 'min'
139
+ | 'max'
140
+ | 'integer'
141
+ | 'pattern'
142
+ | 'strongPassword'
143
+ | 'email'
144
+ | 'phone'
145
+ | 'minDate'
146
+ | 'maxDate';
147
+ ```
148
+
149
+ ## Select
150
+
151
+ Tuple of one or more `SelectItem`.
152
+
153
+ `currentIndex` must be a valid index of the tuple or null.
154
+
155
+ ```typescript
156
+ select = new Input(
157
+ InputType.Select,
158
+ new Select(
159
+ [
160
+ { label: 'Male', value: 'male' },
161
+ { label: 'Female', value: 'female' },
162
+ ],
163
+ 0
164
+ ),
165
+ 'gender',
166
+ 'Gender'
167
+ );
168
+
169
+ export class Select<T extends SelectItemTuple, I extends number | null> {
170
+ current: SelectItem | null;
171
+
172
+ constructor(readonly items: T, readonly currentIndex: HasIndex<T, I> extends true ? I : never) {
173
+ this.current = currentIndex === null ? null : this.items[currentIndex];
174
+ }
175
+ }
176
+
177
+ export type SelectItem = {
178
+ label: string;
179
+ value: string;
180
+ };
181
+ ```
182
+
183
+ ## Step
184
+
185
+ Impossible to duplicate an `Input` return key.
186
+
187
+ Tuple of one or more `Inputs`.
188
+
189
+ ```typescript
190
+ export class Step<T extends InputTuple> {
191
+ constructor(
192
+ readonly inputs: HasDuplicateReturnKeys<T> extends true ? never : T,
193
+ readonly config?: StepConfig
194
+ ) {}
195
+ }
196
+
197
+ export type StepConfig = Readonly<{
198
+ title: string;
199
+ }>;
200
+ ```
201
+
202
+ ## FormStepper
203
+
204
+ Impossible to duplicate an `Input` return key between two `Steps`.
205
+
206
+ Configuration object depending on the number of `Steps`.
207
+
208
+ Tuple of one or more `Steps`.
209
+
210
+ ```typescript
211
+ export class FormStepper<T extends StepTuple> {
212
+ readonly values: FormStepperValues<T>;
213
+
214
+ constructor(
215
+ readonly steps: HasDuplicateReturnKeys<T> extends true ? never : T,
216
+ readonly config: T extends MultiStepTuple ? MultiStepConfig : SingleStepConfig
217
+ ) {
218
+ this.values = Object.fromEntries(
219
+ steps.flatMap((step) => step.inputs.map((input) => [input.returnKey, input.defaultValue]))
220
+ ) as FormStepperValues<T>;
221
+ }
222
+ }
223
+
224
+ export type SingleStepConfig = Readonly<{
225
+ title?: string;
226
+ actionText?: RedirectItem[];
227
+ buttonText: SingleStepButtonText;
228
+ footerText?: RedirectItem[];
229
+ classNames?: SingleStepClassNames;
230
+ }>;
231
+
232
+ export type MultiStepConfig = Readonly<{
233
+ title?: string;
234
+ actionText?: RedirectItem[];
235
+ buttonText: MultiStepButtonText;
236
+ footerText?: RedirectItem[];
237
+ classNames?: MultiStepClassNames;
238
+ }>;
239
+ ```
240
+
241
+ ## RedirectItem[]
242
+
243
+ A `RedirectItem[]` is an array of strings or `RedirectUrl` objects, a kind of mini TS language to create texts with clickable links.
244
+
245
+ ```typescript
246
+ actionText = ['You already have an account ?', { url: '/signin', urlText: 'Sign in' }];
247
+
248
+ export type RedirectUrl = Readonly<{ url: string; urlText: string }>;
249
+
250
+ export type RedirectText = string;
251
+
252
+ export type RedirectItem = RedirectText | RedirectUrl;
253
+ ```
254
+
255
+ ## ButtonText
256
+
257
+ `buttonText` property of `FormStepper` depends on the number of `Steps`.
258
+
259
+ ```typescript
260
+ export type SingleStepButtonText = string;
261
+
262
+ export type MultiStepButtonText = Readonly<{
263
+ final: string;
264
+ previous: string;
265
+ next: string;
266
+ }>;
267
+ ```
268
+
269
+ ## ClassNames
270
+
271
+ To add your own styles to a `FormStepper`, I recommend creating a separate style file and adding your classes there. You should then import the created file into the global styles file of your app.
272
+
273
+ ```typescript
274
+ classNames: SingleStepClassNames = {
275
+ title: 'fs-title',
276
+ input: {
277
+ error: 'fs-input-error',
278
+ },
279
+ };
280
+
281
+ form = new FormStepper([this.step], { buttonText: 'Submit', classNames: this.classNames });
282
+ ```
283
+
284
+ ```css
285
+ /* app/fs.css */
286
+
287
+ .fs-title {
288
+ color: blue;
289
+ }
290
+
291
+ .fs-input-error {
292
+ color: red;
293
+ }
294
+ ```
295
+
296
+ ```css
297
+ /* styles.css */
298
+
299
+ @import 'app/fs.css';
300
+ ```
301
+
302
+ `classNames` property of `FormStepper` also depends on the number of `Steps`.
303
+
304
+ ```typescript
305
+ export type SingleStepClassNames = DeepPartial<{
306
+ container: string;
307
+ title: string;
308
+ actionText: {
309
+ container: string;
310
+ text: string;
311
+ url: string;
312
+ };
313
+ step: {
314
+ container: string;
315
+ title: string;
316
+ form: string;
317
+ inputContainer: string;
318
+ };
319
+ input: {
320
+ container: string;
321
+ label: string;
322
+ required: string;
323
+ input: string;
324
+ errorContainer: string;
325
+ error: string;
326
+ };
327
+ button: {
328
+ container: string;
329
+ button: string;
330
+ disabled: string;
331
+ };
332
+ footerText: {
333
+ container: string;
334
+ text: string;
335
+ url: string;
336
+ };
337
+ }>;
338
+
339
+ export type MultiStepClassNames = DeepPartial<{
340
+ container: string;
341
+ title: string;
342
+ actionText: {
343
+ container: string;
344
+ text: string;
345
+ url: string;
346
+ };
347
+ step: {
348
+ container: string;
349
+ title: string;
350
+ form: string;
351
+ inputContainer: string;
352
+ };
353
+ input: {
354
+ container: string;
355
+ label: string;
356
+ required: string;
357
+ input: string;
358
+ errorContainer: string;
359
+ error: string;
360
+ };
361
+ button: {
362
+ container: string;
363
+ button: string;
364
+ disabled: string;
365
+ first: string;
366
+ final: string;
367
+ previous: string;
368
+ next: string;
369
+ };
370
+ footerText: {
371
+ container: string;
372
+ text: string;
373
+ url: string;
374
+ };
375
+ }>;
376
+ ```