/
properties.ts
776 lines (667 loc) · 20.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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
import { FieldProps } from "./fields";
import { PropertyPreviewProps } from "../preview";
import { ChipColor } from "./colors";
import { EntityReference, EntityValues, GeoPoint } from "./entities";
import {
ResolvedArrayProperty,
ResolvedStringProperty
} from "./resolved_entities";
import { FilterValues } from "./collections";
/**
* @category Entity properties
*/
export type CMSType =
| string
| number
| boolean
| Date
| GeoPoint
| EntityReference
| { [Key: string]: any }
| CMSType[];
/**
* @ignore
*/
export type AnyProperty =
StringProperty |
NumberProperty |
BooleanProperty |
DateProperty |
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 ? DateProperty :
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"
| "date"
| "geopoint"
| "reference";
/**
* Interface including all common properties of a CMS property
* @category Entity properties
*/
export interface BaseProperty<T extends CMSType, CustomProps = any> {
/**
* Datatype of the property
*/
dataType: DataType;
/**
* Property name (e.g. Product)
*/
name?: 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;
/**
* Do not show this property in the collection view
*/
hideFromCollection?: boolean;
/**
* 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 (including custom messages, clear value on
* disabled or hide the field completely)
*/
disabled?: boolean | PropertyDisabledConfig;
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema;
/**
* 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<PropertyPreviewProps<T, CustomProps>>;
/**
* Additional props that are passed to the components defined in `field`
* or in `preview`.
*/
customProps?: CustomProps;
/**
* This value will be set by default for new entities.
*/
defaultValue?: T;
/**
* Should this property be editable in the collection editor.
* If the property has been defined in code, it defaults to `false` otherwise,
* it defaults to `true`.
*/
editable?: boolean;
}
/**
* @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 {@link 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 use an array of {@link EnumValueConfig}
* @category Entity properties
*/
export type EnumValues = EnumValueConfig[]
| Record<string | number, string | EnumValueConfig>;
/**
* Configuration for a particular entry in an `EnumValues`
* @category Entity properties
*/
export interface EnumValueConfig {
id: string | number;
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[keyof M]>;
};
/**
* @category Entity properties
*/
export type PropertyBuilderProps<M extends { [Key: string]: any }> =
{
/**
* Current values of the entity
*/
values: Partial<M>;
/**
* Previous values of the entity before being saved
*/
previousValues?: Partial<M>;
/**
* Current value of this property
*/
propertyValue?: any;
/**
* Index of this property (only for arrays)
*/
index?: number;
/**
* Path of the entity in the data source
*/
path: string;
/**
* Entity ID
*/
entityId?: string;
};
/**
* @category Entity properties
*/
export type PropertyBuilder<T extends CMSType = CMSType, M = any> = ({
values,
previousValues,
propertyValue,
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 PropertiesOrBuilders<M extends { [Key: string]: any } = any> =
{
[k in keyof M]: PropertyOrBuilder<M[k], M>;
};
/**
* @category Entity properties
*/
export interface NumberProperty extends BaseProperty<number> {
dataType: "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;
/**
* Rules for validating this property
*/
validation?: NumberPropertyValidationSchema,
}
/**
* @category Entity properties
*/
export interface BooleanProperty extends BaseProperty<boolean> {
dataType: "boolean";
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema,
}
/**
* @category Entity properties
*/
export interface StringProperty extends BaseProperty<string> {
dataType: "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 `Storage` configuration. It is used to
* indicate that this string refers to a path in Google Cloud Storage.
*/
storage?: StorageConfig;
/**
* 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;
/**
* Does this field include an email
*/
email?: boolean;
/**
* Should this string be rendered as a tag instead of just text.
*/
previewAsTag?: boolean;
/**
* Rules for validating this property
*/
validation?: StringPropertyValidationSchema,
}
/**
* @category Entity properties
*/
export interface ArrayProperty<T extends ArrayT[] = any[], ArrayT extends CMSType = any> extends BaseProperty<T> {
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?: PropertyOrBuilder<ArrayT> | Property[];
/**
* 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: Properties;
/**
* Order in which the properties are displayed.
* If you are specifying your collection as code, the order is the same as the
* one you define in `properties`, and you don't need to specify this prop.
*/
propertiesOrder?: (Extract<keyof T, string>)[];
/**
* 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;
/**
* Should the field be initially expanded. Defaults to `true`
*/
expanded?: boolean;
}
/**
* @category Entity properties
*/
export interface MapProperty<T extends { [Key: string]: unknown } = any> extends BaseProperty<T> {
dataType: "map";
/**
* Record of properties included in this map.
*/
properties?: PropertiesOrBuilders<Partial<T>>; // TODO: this should be Properties<T> but it breaks if building properties without `buildProperties`
/**
* Order in which the properties are displayed.
* If you are specifying your collection as code, the order is the same as the
* one you define in `properties`, and you don't need to specify this prop.
*/
propertiesOrder?: (Extract<keyof T, string>)[];
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema,
/**
* Properties that are displayed when as a preview
*/
previewProperties?: Partial<Extract<keyof T, string>>[];
/**
* Allow the user to add only some 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 sub-properties that may not be
* needed
*/
pickOnlySomeKeys?: boolean;
/**
* Display the child properties as independent columns in the collection
* view
*/
spreadChildren?: boolean;
/**
* Should the field be initially expanded. Defaults to `true`
*/
expanded?: boolean;
}
/**
* @category Entity properties
*/
export interface DateProperty extends BaseProperty<Date> {
dataType: "date";
/**
* Set the granularity of the field to a date or date + time.
* Defaults to `date_time`.
*
*/
mode?: "date" | "date_time";
/**
* Rules for validating this property
*/
validation?: DatePropertyValidationSchema;
/**
* 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"
}
/**
* @category Entity properties
*/
// TODO: currently this is the only unsupported field
export interface GeopointProperty extends BaseProperty<GeoPoint> {
dataType: "geopoint";
/**
* Rules for validating this property
*/
validation?: PropertyValidationSchema,
}
/**
* @category Entity properties
*/
export interface ReferenceProperty extends BaseProperty<EntityReference> {
dataType: "reference";
/**
* Absolute collection path of the collection this reference points to.
* The collection 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.
* Note that you can also use a collection alias.
*/
path: string | false;
/**
* Allow selection of entities that pass the given filter only.
*/
forceFilter?: FilterValues<string>;
/**
* 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?: string[];
}
/**
* 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?: string | RegExp;
/**
* Message displayed when the input does not satisfy the regex in `matches`
*/
matchesMessage?: string;
trim?: boolean;
lowercase?: boolean;
uppercase?: boolean;
}
/**
* Validation rules for dates
* @category Entity properties
*/
export interface DatePropertyValidationSchema extends PropertyValidationSchema {
min?: Date;
max?: Date;
}
/**
* Validation rules for arrays
* @category Entity properties
*/
export interface ArrayPropertyValidationSchema extends PropertyValidationSchema {
min?: number;
max?: number;
}
/**
* Additional configuration related to Storage related fields
* @category Entity properties
*/
export interface StorageConfig {
/**
* File MIME types that can be uploaded to this reference. Don't specify for
* all
*/
acceptedFiles?: FileType[];
/**
* Specific metadata set in your uploaded file.
* For the default Firebase implementation, the values passed here are of type
* `firebase.storage.UploadMetadata`
*/
metadata?: Record<string, unknown>,
/**
* You can use this prop to customize the uploaded filename.
* You can use a function as a callback or a string where you
* specify some placeholders that get replaced with the corresponding values.
* - {file} - Full file name
* - {file.name} - Name of the file without extension
* - {file.ext} - Extension of the file
* - {rand} - Random value used to avoid name collisions
* - {entityId} - ID of the entity
* - {propertyKey} - ID of this property
* - {path} - Path of this entity
*
* @param context
*/
fileName?: string | ((context: UploadedFileContext) => string);
/**
* Absolute path in your bucket.
*
* You can use a function as a callback or a string where you
* specify some placeholders that get replaced with the corresponding values.
* - {file} - Full file name
* - {file.name} - Name of the file without extension
* - {file.ext} - Extension of the file
* - {rand} - Random value used to avoid name collisions
* - {entityId} - ID of the entity
* - {propertyKey} - ID of this property
* - {path} - Path of this entity
*/
storagePath: string | ((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 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,
/**
* Define maximal file size in bytes
*/
maxSize?: number,
/**
* Postprocess the saved value (storage path or URL)
* after it has been resolved.
*/
postProcess?: (pathOrUrl: string) => Promise<string>
}
/**
* @category Entity properties
*/
export interface UploadedFileContext {
/**
* Uploaded file
*/
file: File;
/**
* Property field name
*/
propertyKey: string;
/**
* Property related to this upload
*/
property: ResolvedStringProperty | ResolvedArrayProperty<string[]>;
/**
* Entity ID
*/
entityId?: string;
/**
* Entity path. E.g. `products/PID/locales`
*/
path: string;
/**
* Values of the current entity
*/
values: EntityValues<any>;
/**
* Storage meta specified by the developer
*/
storage: StorageConfig;
}
/**
* 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 FileType =
"image/*"
| "video/*"
| "audio/*"
| "application/*"
| "text/*"
| "font/*"
| string
;