/
properties.ts
726 lines (622 loc) · 18.6 KB
/
properties.ts
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import { FieldProps } from "./fields";
import { PreviewComponentProps } from "../preview";
import { ChipColor } from "./colors";
import { EntityReference, EntityValues, GeoPoint } from "./entities";
/**
* @category Entity properties
*/
export type CMSType =
| string
| number
| boolean
| Date
| GeoPoint
| EntityReference
| { [Key: string]: any }
| CMSType[];
/**
* @ignore
*/
export type AnyProperty =
StringProperty |
NumberProperty |
BooleanProperty |
TimestampProperty |
GeopointProperty |
ReferenceProperty |
ArrayProperty |
MapProperty;
/**
* @category Entity properties
*/
export type Property<T extends CMSType = CMSType> =
T extends string ? StringProperty :
T extends number ? NumberProperty :
T extends boolean ? BooleanProperty :
T extends Date ? TimestampProperty :
T extends GeoPoint ? GeopointProperty :
T extends EntityReference ? ReferenceProperty :
T extends Array<CMSType> ? ArrayProperty<T> :
T extends { [Key: string]: any } ? MapProperty<T> : AnyProperty;
/**
* @category Entity properties
*/
export type DataType =
| "number"
| "string"
| "boolean"
| "map"
| "array"
| "timestamp"
| "geopoint"
| "reference";
/**
* @category Entity properties
*/
export type MediaType =
| "image"
| "video"
| "audio";
/**
* Interface including all common properties of a CMS property
* @category Entity properties
*/
interface BaseProperty {
/**
* Datatype of the property
*/
dataType: DataType;
/**
* Property title (e.g. Product)
*/
title?: string;
/**
* Property description, always displayed under the field
*/
description?: string;
/**
* Longer description of a field, displayed under a popover
*/
longDescription?: string;
/**
* Width in pixels of this column in the collection view. If not set
* the width is inferred based on the other configurations
*/
columnWidth?: number;
/**
* Is this a read only property. When set to true, it gets rendered as a
* preview.
*/
readOnly?: boolean;
/**
* Is this field disabled. When set to true, it gets rendered as a
* disabled field. You can also specify a configuration for defining the
* behaviour of disabled properties
*/
disabled?: boolean | PropertyDisabledConfig;
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema;
}
/**
* @category Entity properties
*/
export interface PropertyDisabledConfig {
/**
* Enable this flag if you would like to clear the value of the field
* when the corresponding property gets disabled.
*
* This is useful for keeping data consistency when you have conditional
* properties.
*/
clearOnDisabled?: boolean;
/**
* Explanation of why this property is disabled (e.g. a different field
* needs to be enabled)
*/
disabledMessage?: string;
/**
* Set this flag to true if you want to hide this field when disabled
*/
hidden?: boolean;
}
/**
* @category Entity properties
*/
export type EnumType = number | string;
/**
* We use this type to define mapping between string or number values in
* the data source to a label (such in a select dropdown).
* The key in this Record is the value saved in the datasource, and the value in
* this record is the label displayed in the UI.
* You can add additional customization by assigning a `EnumValueConfig` for the
* label instead of a simple string (for enabling or disabling options and
* choosing colors).
* If you need to ensure the order of the elements you can pass a `Map` instead
* of a plain object.
* @category Entity properties
*/
export type EnumValues =
Record<string | number, string | EnumValueConfig>
| Map<string | number, string | EnumValueConfig>;
/**
* Configuration for a particular entry in an `EnumValues`
* @category Entity properties
*/
export interface EnumValueConfig {
label: string;
disabled?: boolean;
color?: ChipColor;
}
/**
* Record of properties of an entity or a map property
* @category Entity properties
*/
export type Properties<M extends { [Key: string]: any } = any> = {
[k in keyof M]: Property<M[k]>;
};
/**
* @category Entity properties
*/
export type PropertyBuilderProps<M extends { [Key: string]: any }> =
{
values: Partial<M>;
previousValues?: Partial<M>;
path: string;
entityId?: string;
};
/**
* @category Entity properties
*/
export type PropertyBuilder<T extends CMSType = CMSType, M = any> = ({
values,
previousValues,
path,
entityId
}: PropertyBuilderProps<M>) => Property<T> | null;
/**
* @category Entity properties
*/
export type PropertyOrBuilder<T extends CMSType = CMSType, M = any> =
Property<T>
| PropertyBuilder<T, M>;
/**
* @category Entity properties
*/
export type PropertiesOrBuilder<M extends { [Key: string]: any }> =
{
[k in keyof M]: PropertyOrBuilder<M[k], M>;
};
/**
* @category Entity properties
*/
export interface NumberProperty extends BaseProperty {
dataType: "number";
/**
* Configure how this field is displayed
*/
config?: NumberFieldConfig;
/**
* Rules for validating this property
*/
validation?: NumberPropertyValidationSchema,
}
/**
* @category Entity properties
*/
export interface BooleanProperty extends BaseProperty {
dataType: "boolean";
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema,
/**
* Configure how this property field is displayed
*/
config?: FieldConfig<boolean>;
}
/**
* @category Entity properties
*/
export interface StringProperty extends BaseProperty {
dataType: "string";
/**
* Configure how this field is displayed
*/
config?: StringFieldConfig;
/**
* Rules for validating this property
*/
validation?: StringPropertyValidationSchema,
}
/**
* @category Entity properties
*/
export interface ArrayProperty<T extends ArrayT[] = any[], ArrayT extends CMSType = any> extends BaseProperty {
dataType: "array";
/**
* The property of this array.
* You can specify any property (except another Array property)
* You can leave this field empty only if you are providing a custom field,
* otherwise an error will be thrown.
*/
of?: Property<ArrayT>;
/**
* Use this field if you would like to have an array of properties.
* It is useful if you need to have values of different types in the same
* array.
* Each entry of the array is an object with the shape:
* ```
* { type: "YOUR_TYPE", value: "YOUR_VALUE"}
* ```
* Note that you can use any property so `value` can take any value (strings,
* numbers, array, objects...)
* You can customise the `type` and `value` fields to suit your needs.
*
* An example use case for this feature may be a blog entry, where you have
* images and text blocks using markdown.
*/
oneOf?: {
/**
* Record of properties, where the key is the `type` and the value
* is the corresponding property
*/
properties: Record<string, Property>;
/**
* Name of the field to use as the discriminator for type
* Defaults to `type`
*/
typeField?: string;
/**
* Name of the field to use as the value
* Defaults to `value`
*/
valueField?: string;
};
/**
* Rules for validating this property
*/
validation?: ArrayPropertyValidationSchema,
/**
* Configure how this property field is displayed
*/
config?: FieldConfig<T>;
}
/**
* @category Entity properties
*/
export interface MapProperty<T extends { [Key: string]: any } = any> extends BaseProperty {
dataType: "map";
/**
* Record of properties included in this map.
*/
properties?: Properties<Partial<T>>; // TODO: this should be Properties<T> but it breaks if building properties without `buildProperties`
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema,
/**
* Properties that are displayed when as a preview
*/
previewProperties?: Extract<keyof T, string>[];
/**
* Configure how this property field is displayed
*/
config?: MapFieldConfig<T>;
}
/**
* @category Entity properties
*/
export interface TimestampProperty extends BaseProperty {
dataType: "timestamp";
/**
* Rules for validating this property
*/
validation?: TimestampPropertyValidationSchema;
/**
* If this flag is set to `on_create` or `on_update` this timestamp is
* updated automatically on creation of the entity only or on every
* update (including creation). Useful for creating `created_on` or
* `updated_on` fields
*/
autoValue?: "on_create" | "on_update"
/**
* Configure how this property field is displayed
*/
config?: FieldConfig<Date>;
}
/**
* @category Entity properties
*/
// TODO: currently this is the only unsupported field
export interface GeopointProperty extends BaseProperty {
dataType: "geopoint";
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema,
/**
* Configure how this property field is displayed
*/
config?: FieldConfig<GeoPoint>;
}
/**
* @category Entity properties
*/
export interface ReferenceProperty<M extends { [Key: string]: any } = any>
extends BaseProperty {
dataType: "reference";
/**
* Absolute collection path of the collection this reference points to.
* The schema of the entity is inferred based on the root navigation, so
* the filters and search delegate existing there are applied to this view
* as well.
* You can set this prop to `false` if the path is not yet know, e.g.
* you are using a property builder and the path depends on a different
* property.
*/
path: string | false;
/**
* Properties that need to be rendered when displaying a preview of this
* reference. If not specified the first 3 are used. Only the first 3
* specified values are considered.
*/
previewProperties?: (keyof M)[];
/**
* Configure how this property field is displayed
*/
config?: FieldConfig<EntityReference>;
}
/**
* Rules to validate any property. Some properties have specific rules
* additionally to these.
* @category Entity properties
*/
export interface PropertyValidationSchema {
/**
* Is this field required
*/
required?: boolean;
/**
* Customize the required message when the property is not set
*/
requiredMessage?: string;
/**
* If the unique flag is set to `true`, you can only have one entity in the
* collection with this value.
*/
unique?: boolean;
/**
* If the uniqueInArray flag is set to `true`, you can only have this value
* once per entry in the parent `ArrayProperty`. It has no effect if this
* property is not a child of an `ArrayProperty`. It works on direct
* children of an `ArrayProperty` or first level children of `MapProperty`
*/
uniqueInArray?: boolean;
}
/**
* Validation rules for numbers
* @category Entity properties
*/
export interface NumberPropertyValidationSchema extends PropertyValidationSchema {
min?: number;
max?: number;
lessThan?: number;
moreThan?: number;
positive?: boolean;
negative?: boolean;
integer?: boolean;
}
/**
* Validation rules for strings
* @category Entity properties
*/
export interface StringPropertyValidationSchema extends PropertyValidationSchema {
length?: number;
min?: number;
max?: number;
matches?: RegExp;
/**
* Message displayed when the input does not satisfy the regex in `matches`
*/
matchesMessage?: string;
email?: boolean;
url?: boolean;
trim?: boolean;
lowercase?: boolean;
uppercase?: boolean;
}
/**
* Validation rules for dates
* @category Entity properties
*/
export interface TimestampPropertyValidationSchema extends PropertyValidationSchema {
min?: Date;
max?: Date;
}
/**
* Validation rules for arrays
* @category Entity properties
*/
export interface ArrayPropertyValidationSchema extends PropertyValidationSchema {
min?: number;
max?: number;
}
/**
* Configure how a field is displayed
* @category Entity properties
*/
export interface FieldConfig<T extends CMSType, CustomProps = any> {
/**
* If you need to render a custom field, you can create a component that
* takes `FieldProps` as props. You receive the value, a function to
* update the value and additional utility props such as if there is an error.
* You can customize it by passing custom props that are received
* in the component.
*/
Field?: React.ComponentType<FieldProps<T, CustomProps>>;
/**
* Configure how a property is displayed as a preview, e.g. in the collection
* view. You can customize it by passing custom props that are received
* in the component.
*/
Preview?: React.ComponentType<PreviewComponentProps<T, CustomProps>>;
/**
* Additional props that are passed to the components defined in `field`
* or in `preview`.
*/
customProps?: CustomProps;
}
/**
* Possible configuration fields for a string property. Note that setting one
* config disables the others.
* @category Entity properties
*/
export interface StringFieldConfig extends FieldConfig<string> {
/**
* Is this string property long enough so it should be displayed in
* a multiple line field. Defaults to false. If set to true,
* the number of lines adapts to the content
*/
multiline?: boolean;
/**
* Should this string property be displayed as a markdown field. If true,
* the field is rendered as a text editors that supports markdown highlight
* syntax. It also includes a preview of the result.
*/
markdown?: boolean;
/**
* You can use the enum values providing a map of possible
* exclusive values the property can take, mapped to the label that it is
* displayed in the dropdown. You can use a simple object with the format
* `value` => `label`, or with the format `value` => `EnumValueConfig` if you
* need extra customization, (like disabling specific options or assigning
* colors). If you need to ensure the order of the elements, you can pass
* a `Map` instead of a plain object.
*/
enumValues?: EnumValues;
/**
* You can specify a `StorageMeta` configuration. It is used to
* indicate that this string refers to a path in Google Cloud Storage.
*/
storageMeta?: StorageMeta;
/**
* If the value of this property is a URL, you can set this flag to true
* to add a link, or one of the supported media types to render a preview
*/
url?: boolean | MediaType;
/**
* Should this string be rendered as a tag instead of just text.
*/
previewAsTag?: boolean;
}
/**
* Additional configuration related to Storage related fields
* @category Entity properties
*/
export interface StorageMeta {
/**
* Media type of this reference, used for displaying the preview
*/
mediaType?: MediaType;
/**
* Absolute path in your bucket. You can specify it directly or use a callback
*/
storagePath: string | ((context: UploadedFileContext) => string);
/**
* File MIME types that can be uploaded to this reference
*/
acceptedFiles?: StorageFileTypes[];
/**
* Specific metadata set in your uploaded file.
* For the default Firebase implementation, the values passed here are of type
* `firebase.storage.UploadMetadata`
*/
metadata?: any,
/**
* You can use this callback to customize the uploaded filename
* @param context
*/
fileName?: (context: UploadedFileContext) => string;
/**
* When set to true, this flag indicates that the download URL of the file
* will be saved in the datasource instead of the Cloud storage path.
* Note that the generated URL may use a token that, if disabled, may
* make the URL unusable and lose the original reference to Cloud Storage,
* so it is not encouraged to use this flag. Defaults to false
*/
storeUrl?: boolean,
/**
* Post process the path
*/
postProcess?: (pathOrUrl: string) => Promise<string>
}
/**
* @category Entity properties
*/
export interface UploadedFileContext {
/**
* Uploaded file
*/
file: File;
/**
* Property field name
*/
name: string;
/**
* Property related to this upload
*/
property: StringProperty | ArrayProperty<string[]>;
/**
* Entity Id is set if the entity already exists
*/
entityId?: string;
/**
* Values of the current entity
*/
values: EntityValues<any>;
/**
* Storage meta specified by the developer
*/
storageMeta: StorageMeta;
}
/**
* Possible configuration fields for a string property. Note that setting one
* config disables the others.
* @category Entity properties
*/
export interface MapFieldConfig<T extends {}> extends FieldConfig<T> {
/**
* Allow the user to add only some of the keys in this map.
* By default all properties of the map have the corresponding field in
* the form view. Setting this flag to true allows to pick only some.
* Useful for map that can have a lot of subproperties that may not be
* needed
*/
pickOnlySomeKeys?: boolean;
}
/**
* MIME types for storage fields
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
* @category Entity properties
*/
export type StorageFileTypes =
"image/*"
| "video/*"
| "audio/*"
| "application/*"
| "text/*"
| "font/*"
| string;
/**
* @category Entity properties
*/
export interface NumberFieldConfig extends FieldConfig<number> {
/**
* You can use the enum values providing a map of possible
* exclusive values the property can take, mapped to the label that it is
* displayed in the dropdown.
*/
enumValues?: EnumValues;
}