aboutsummaryrefslogtreecommitdiff
path: root/design-documents/054-dynamic-form.rst
blob: 5567fc60c208def7317486d55580e660d57882cf (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
DD 54: Dynamic Form
###################

Summary
=======

This document outlines the design of forms defined in the 
backend in a JSON file which will be rendered by a client
for asking information to a person.


Motivation
==========

Currently, creating a new form for a web app involves coding a new
page with HTML, CSS, and JS. Exchange AML requires multiple forms,
and different instances may have distinct forms based on jurisdiction.
Being able to define forms in a JSON file that a client software 
(not just web SPA) could use to ask the information helps to change
it without requiring a new upgrade of the client app.


Requirements
============

A form consist of a layout, a set of fields and metadata required to 
recognice which form configuration was used to produce the saved value.

Layout requirements
-------------------

* **editable by system admin**: System admins should be able to create new forms
  or edit current one shipped with the source.

* **accesibility**: Forms should meet accessibility level AA.

* **responsive**: Forms should be responsive and function on all devices.

* **metadata**: Generated form information should contain enough data
  to handle multiple form versions.

Fields requirements
-------------------

* **validations**: Each field may require custom validation

* **custom data type**: A field may consist of a list, string, number, or a
  complex composite structure.


Metadata requirements
---------------------

* **identification**: the form configuration instance should have an unique
  non reusable id.

* **label**: the form should have a name recognizable for the user

* **version**: the same form, with the same id, could be updated. This will
  increase the version number. A newer form should support older forms.

Proposed Solutions
==================

The propose solution defines the structure of a form, the fields and additional
type form-configuration which links a form with a set of fields.

Form metadata
-------------

This is the root object of the configuration.

.. code-block:: typescript

  type FormMetadata = {
    label: string;
    id: string;
    version: number;
    config: FormConfiguration;
  };


Form configuration
------------------

Defies a basic structure and the set of fields the form is going to have.

The ``FormConfiguration`` is an enumerated type which list can be extended in the 
future.

.. code-block:: typescript

  type FormConfiguration = DoubleColumnForm;

  type DoubleColumnForm = {
    type: "double-column";
    design: DoubleColumnFormSection[];
  }

  type DoubleColumnFormSection = {
    title: string;
    description?: string;
    fields: UIFormElementConfig[];
  };


Form fields
-----------

A form can have two type of element: decorative/informative or input.

An example of a decorative element is a grouping element which make all the fields
inside the group look into the same section. This element will not allow the user
to enter information and won't produce any value in the resulting JSON.

An example of an input field is a text field which allows the user to enter text.
This element should have an ``id`` which will point into the location in which the
value will be stored in the resulting JSON. Note that two field in the same form
with the same ``id`` will result in undefined behavior.

The ``UIFormElementConfig`` is an enumerated type with all type of fields supported

.. code-block:: typescript

  type UIFormElementConfig =
    | UIFormElementGroup
    | UIFormElementCaption
    | UIFormFieldAbsoluteTime
    | UIFormFieldAmount
    | UIFormFieldArray
    | UIFormFieldChoiseHorizontal
    | UIFormFieldChoiseStacked
    | UIFormFieldFile
    | UIFormFieldInteger
    | UIFormFieldSelectMultiple
    | UIFormFieldSelectOne
    | UIFormFieldText
    | UIFormFieldTextArea
    | UIFormFieldToggle;


All form elements should extend from ``UIFieldElementDescription`` which defines a base
configuration to show a field.

.. code-block:: typescript

  type UIFieldElementDescription = {
    /* label if the field, visible for the user */
    label: string;

    /* long text to be shown on user demand */
    tooltip?: string;

    /* short text to be shown close to the field, usually below and dimmer*/
    help?: string;

    /* name of the field, useful for a11y */
    name: string;

    /* if the field should be initially hidden */
    hidden?: boolean;
    
  };

That will be enough for a decorative form element (like group element or 
a text element) but if it defines an input field then it should extend
from ``UIFormFieldBaseConfig`` which add more information to the previously
defined ``UIFieldElementDescription``.


.. code-block:: typescript

  type UIFormFieldBaseConfig = UIFieldElementDescription & {
    /* example to be shown inside the field */
    placeholder?: string;

    /* show a mark as required */
    required?: boolean;

    /* readonly and dim */
    disabled?: boolean;

    /* conversion id to convert the string into the value type
        the id should be known to the ui impl
    */
    converterId?: string;

    /* property id of the form */
    id: UIHandlerId;
  };

  /**
   * string which defined a json path 
   * 
   */
  type UIHandlerId = string


The ``id`` property defines the location in which this information is going 
to be saved in the JSON result. Formally formally, it should be a ``dot-selector``

.. code-block:: ini

  dot-selector    = "." dot-member-name
  dot-member-name = name-first *name-char
  name-first = ALPHA / "_" 
  name-char = DIGIT / name-first

  DIGIT           =  %x30-39              ; 0-9
  ALPHA           =  %x41-5A / %x61-7A    ; A-Z / a-z


All the input fields will create a string value located where the id
points, unless a ``convertedId`` is specified. The ``convertedId`` is a reference
to a converter that the client software implements. For example, an input field
with ``convertedId = "Taler.Amount"`` will transform the value the user 
entered into a *AmountString* with the currency in the configuration.


Description of supported fields
-------------------------------

All of this fields defines an UI handler which help the user to input 
the value with as handy as possible. The type of the field doesn't define
the type of the value in the resulting JSON, that's defined by the ``converterId``.

Decorative elements
```````````````````

To show some additional text

.. code-block:: typescript

  type UIFormElementCaption = { type: "caption" } & UIFieldElementDescription;

To group fields in the UI and maybe show a collapsable handler.

.. code-block:: typescript

  type UIFormElementGroup = {
    type: "group";
    fields: UIFormElementConfig[];
  } & UIFieldElementDescription;

Example
'''''''

.. code-block:: json

  {
      "label": "Example form",
      "id": "example",
      "version": 1,
      "config": {
        "type": "double-column",
        "design": [
          {
            "title": "Decorative elements",
            "fields": [
              {
                "type": "caption",
                "name": "cap",
                "label": "This is a caption"
              },
              {
                "type": "group",
                "name": "group",
                "label": "The first name and last name are in a group",
                "fields": [
                  {
                    "type": "text",
                    "name": "firstName",
                    "id": ".person.name",
                    "label": "First name"
                  },
                  {
                    "type": "text",
                    "name": "lastName",
                    "id": ".person.lastName",
                    "label": "Last name"
                  }
                ]
              }
            ]
          }
        ]
      }
    }

.. image:: ../screenshots/dynamic-forms.decorative-elements.png
  :width: 400

Time input
``````````

This may be rendered as a calendar

.. code-block:: typescript

  type UIFormFieldAbsoluteTime = {
    type: "absoluteTimeText";
    max?: TalerProtocolTimestamp;
    min?: TalerProtocolTimestamp;
    pattern: string;
  } & UIFormFieldBaseConfig;

.. code-block:: json

  {
      "label": "Example form",
      "id": "example",
      "version": 1,
      "config": {
        "type": "double-column",
        "design": [
          {
            "title": "Time inputs",
            "fields": [
              {
                "type": "absoluteTime",
                "name": "thedate",
                "id": ".birthdate",
                "converterId": "Taler.AbsoluteTime",
                "help": "the day you born",
                "pattern":"dd/MM/yyyy",
                "label": "Birthdate"
              }
            ]
          }
        ]
      }
    }

.. image:: ../screenshots/dynamic-forms.time.png
  :width: 400


Amount input
````````````

Money input.

.. code-block:: typescript

  type UIFormFieldAmount = {
    type: "amount";
    max?: Integer;
    min?: Integer;
    currency: string;
  } & UIFormFieldBaseConfig;

.. code-block:: json

  {
      "label": "Example form",
      "id": "example",
      "version": 1,
      "config": {
        "type": "double-column",
        "design": [
          {
            "title": "Amount inputs",
            "fields": [
              {
                "type": "amount",
                "name": "thedate",
                "id": ".amount",
                "converterId": "Taler.Amount",
                "help": "how much do you have?",
                "currency":"EUR",
                "label": "Amount"
              }
            ]
          }
        ]
      }
    }

.. image:: ../screenshots/dynamic-forms.amount.png
  :width: 400


List input
``````````

This input allows to enter more than element in the same field, and the 
resulting JSON will have a json list. The UI should show the elements 
already present in the list, and for that it will use ``labelFieldId``.

.. code-block:: typescript

  type UIFormFieldArray = {
    type: "array";
    // id of the field shown when the array is collapsed
    labelFieldId: UIHandlerId;
    fields: UIFormElementConfig[];
  } & UIFormFieldBaseConfig;

.. code-block:: json

  {
      "label": "Example form",
      "id": "example",
      "version": 1,
      "config": {
        "type": "double-column",
        "design": [
          {
            "title": "Amount inputs",
            "fields": [
              {
                "type": "array",
                "name": "people",
                "id": ".people",
                "help": "who is coming to the party?",
                "labelFieldId": ".name",
                "fields": [{
                    "type": "text",
                    "name": "firstName",
                    "id": ".name",
                    "label": "First name"
                  },
                  {
                    "type": "text",
                    "name": "lastName",
                    "id": ".lastName",
                    "label": "Last name"
                  }],
              }
            ]
          }
        ]
      }
    }


.. image:: ../screenshots/dynamic-forms.list.png
  :width: 400

Choice input
````````````

To be used when the user need to choose on predefined values

.. code-block:: typescript

  interface SelectUiChoice {
    label: string;
    description?: string;
    value: string;
  }

A set of buttons next to each other

.. code-block:: typescript

  type UIFormFieldChoiseHorizontal = {
    type: "choiceHorizontal";
    choices: Array<SelectUiChoice>;
  } & UIFormFieldBaseConfig;


A set of buttons next on top of each other

.. code-block:: typescript

  type UIFormFieldChoiseStacked = {
    type: "choiceStacked";
    choices: Array<SelectUiChoice>;
  } & UIFormFieldBaseConfig;

A drop down list to select one of the elements

.. code-block:: typescript

  type UIFormFieldSelectOne = {
    type: "selectOne";
    choices: Array<SelectUiChoice>;
  } & UIFormFieldBaseConfig;

A drop down list to select multiple of the element, which
will produce a list of values in the resulting JSON.

.. code-block:: typescript

  type UIFormFieldSelectMultiple = {
    type: "selectMultiple";
    max?: Integer;
    min?: Integer;
    unique?: boolean;
    choices: Array<SelectUiChoice>;
  } & UIFormFieldBaseConfig;


.. code-block:: json

  {
      "label": "Example form",
      "id": "example",
      "version": 1,
      "config": {
        "type": "double-column",
        "design": [
          {
            "title": "Choice inputs",
            "fields": [
              {
                "type": "choiceHorizontal",
                "name": "food",
                "label": "Food",
                "id": ".food",
                "choices": [
                  {
                    "value": "meat",
                    "label": "Meat"
                  },
                  {
                    "value": "sushi",
                    "label": "Sushi"
                  },
                  {
                    "value": "taco",
                    "label": "Taco"
                  },
                  {
                    "value": "salad",
                    "label": "Salad"
                  }
                ]
              }
            ]
          }
        ]
      }
    }

.. image:: ../screenshots/dynamic-forms.choice.png
  :width: 400

File input
``````````

.. code-block:: typescript

  type UIFormFieldFile = {
    type: "file";
    maxBytes?: Integer;
    minBytes?: Integer;
    // comma-separated list of one or more file types
    // https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept#unique_file_type_specifiers
    accept?: string;
  } & UIFormFieldBaseConfig;


.. code-block:: json

  {
    "label": "Example form",
    "id": "example",
    "version": 1,
    "config": {
      "type": "double-column",
      "design": [
        {
          "title": "File inputs",
          "fields": [
            {
              "type": "file",
              "name": "photo",
              "id": ".photo",
              "label": "Photo",
              "accept": "*.png"
            }
          ]
        }
      ]
    }
  }

.. image:: ../screenshots/dynamic-forms.file.png
  :width: 400

Number input
````````````

.. code-block:: typescript

  type UIFormFieldInteger = {
    type: "integer";
    max?: Integer;
    min?: Integer;
  } & UIFormFieldBaseConfig;


.. image:: ../screenshots/dynamic-forms.number.png
  :width: 400

Text input
``````````

A simple line of text

.. code-block:: typescript

  type UIFormFieldText = { type: "text" } & UIFormFieldBaseConfig;

A bigger multi-line of text

.. code-block:: typescript

  type UIFormFieldTextArea = { type: "textArea" } & UIFormFieldBaseConfig;


.. image:: ../screenshots/dynamic-forms.text.png
  :width: 400

Boolean input
`````````````

.. code-block:: typescript

  type UIFormFieldToggle = { type: "toggle" } & UIFormFieldBaseConfig;


.. code-block:: json

  {
    "label": "Example form",
    "id": "example",
    "version": 1,
    "config": {
      "type": "double-column",
      "design": [
        {
          "title": "Boolean inputs",
          "fields": [
            {
              "type": "toggle",
              "name": "the_question",
              "id": ".the_question",
              "label": "Yes or no?"
            }
          ]
        }
      ]
    }
  }

.. image:: ../screenshots/dynamic-forms.boolean.png
  :width: 400

Examples
========



Q / A
=====