@lifesg/web-frontend-engine 1.0.0-alpha.2 → 1.0.0-alpha.4

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.
Files changed (76) hide show
  1. package/README.md +21 -27
  2. package/cjs/index.js +88 -84
  3. package/cjs/index.js.map +1 -1
  4. package/components/custom/filter/filter/filter.d.ts +2 -0
  5. package/components/custom/filter/filter/types.d.ts +14 -0
  6. package/components/custom/filter/filter-checkbox/filter-checkbox.d.ts +3 -0
  7. package/components/custom/filter/filter-checkbox/types.d.ts +12 -0
  8. package/components/custom/filter/filter-item/filter-item.d.ts +3 -0
  9. package/components/custom/filter/filter-item/types.d.ts +8 -0
  10. package/components/custom/filter/index.d.ts +3 -0
  11. package/components/custom/index.d.ts +1 -0
  12. package/components/elements/alert/alert.d.ts +1 -2
  13. package/components/elements/section/index.d.ts +2 -0
  14. package/components/elements/section/section.d.ts +2 -0
  15. package/components/elements/section/types.d.ts +10 -0
  16. package/components/elements/sections/index.d.ts +2 -0
  17. package/components/elements/sections/sections.d.ts +14 -0
  18. package/components/elements/sections/types.d.ts +5 -0
  19. package/components/elements/text/data.d.ts +12 -12
  20. package/components/elements/text/text.d.ts +1 -2
  21. package/components/elements/wrapper/conditional-renderer.d.ts +5 -1
  22. package/components/elements/wrapper/types.d.ts +11 -2
  23. package/components/elements/wrapper/wrapper.d.ts +2 -11
  24. package/components/fields/checkbox-group/checkbox-group.d.ts +1 -2
  25. package/components/fields/checkbox-group/checkbox-group.styles.d.ts +2 -1
  26. package/components/fields/checkbox-group/types.d.ts +13 -1
  27. package/components/fields/chips/chips.d.ts +1 -2
  28. package/components/fields/chips/types.d.ts +1 -0
  29. package/components/fields/contact-field/contact-field.d.ts +3 -0
  30. package/components/fields/contact-field/data.d.ts +5 -0
  31. package/components/fields/contact-field/index.d.ts +2 -0
  32. package/components/fields/contact-field/types.d.ts +30 -0
  33. package/components/fields/date-field/date-field.d.ts +3 -0
  34. package/components/fields/date-field/index.d.ts +2 -0
  35. package/components/fields/{date-input → date-field}/types.d.ts +2 -2
  36. package/components/fields/index.d.ts +6 -4
  37. package/components/fields/multi-select/multi-select.d.ts +1 -2
  38. package/components/fields/radio-button/radio-button.d.ts +1 -2
  39. package/components/fields/radio-button/radio-button.styles.d.ts +1 -0
  40. package/components/fields/radio-button/types.d.ts +9 -0
  41. package/components/fields/reset-button/index.d.ts +2 -0
  42. package/components/fields/reset-button/reset-button.d.ts +3 -0
  43. package/components/fields/reset-button/types.d.ts +5 -0
  44. package/components/fields/select/select.d.ts +1 -2
  45. package/components/fields/submit-button/submit-button.d.ts +1 -2
  46. package/components/fields/text-field/index.d.ts +2 -0
  47. package/components/fields/text-field/text-field.d.ts +3 -0
  48. package/components/fields/text-field/types.d.ts +8 -0
  49. package/components/fields/textarea/textarea.d.ts +1 -2
  50. package/components/fields/time-field/index.d.ts +2 -0
  51. package/components/fields/time-field/time-field.d.ts +3 -0
  52. package/components/fields/{time-picker → time-field}/types.d.ts +1 -1
  53. package/components/fields/unit-number-field/index.d.ts +2 -0
  54. package/components/fields/unit-number-field/types.d.ts +7 -0
  55. package/components/fields/unit-number-field/unit-number-field.d.ts +3 -0
  56. package/components/frontend-engine/frontend-engine.d.ts +2 -8
  57. package/components/frontend-engine/types.d.ts +76 -20
  58. package/components/frontend-engine/yup/context-provider.d.ts +1 -1
  59. package/components/shared/chip/chip.d.ts +1 -1
  60. package/components/shared/error-messages.d.ts +3 -0
  61. package/components/shared/sanitize/sanitize.d.ts +1 -1
  62. package/index.js +95 -91
  63. package/index.js.map +1 -1
  64. package/package.json +2 -2
  65. package/components/fields/contact-number/contact-number.d.ts +0 -4
  66. package/components/fields/contact-number/data.d.ts +0 -215
  67. package/components/fields/contact-number/index.d.ts +0 -2
  68. package/components/fields/contact-number/types.d.ts +0 -29
  69. package/components/fields/date-input/date-input.d.ts +0 -4
  70. package/components/fields/date-input/index.d.ts +0 -2
  71. package/components/fields/textfield/index.d.ts +0 -2
  72. package/components/fields/textfield/textfield.d.ts +0 -4
  73. package/components/fields/textfield/types.d.ts +0 -8
  74. package/components/fields/time-picker/index.d.ts +0 -2
  75. package/components/fields/time-picker/time-picker.d.ts +0 -4
  76. /package/components/fields/{contact-number → contact-field}/utils.d.ts +0 -0
@@ -1,13 +1,15 @@
1
- import { ControllerFieldState, ControllerRenderProps, UseFormSetValue, ValidationMode } from "react-hook-form";
1
+ import { ControllerFieldState, ControllerRenderProps, UseFormReset, UseFormSetValue, ValidationMode } from "react-hook-form";
2
+ import { IFilterSchema } from "../custom/filter/filter/types";
2
3
  import { IAlertSchema, ITextSchema } from "../elements";
4
+ import { ISectionSchema } from "../elements/section";
3
5
  import { IWrapperSchema } from "../elements/wrapper";
4
- import { ICheckboxGroupSchema, IChipsSchema, IContactNumberSchema, IDateInputSchema, IEmailSchema, IMultiSelectSchema, INumberSchema, IRadioButtonGroupSchema, ISelectSchema, ISubmitButtonSchema, ITextareaSchema, ITextfieldSchema, ITimePickerSchema } from "../fields";
6
+ import { ICheckboxGroupSchema, IChipsSchema, IContactFieldSchema, IDateFieldSchema, IEmailFieldSchema, IMultiSelectSchema, INumericFieldSchema, IRadioButtonGroupSchema, IResetButtonSchema, ISelectSchema, ISubmitButtonSchema, ITextFieldSchema, ITextareaSchema, ITimeFieldSchema, IUnitNumberFieldSchema } from "../fields";
5
7
  import { IYupValidationRule, TRenderRules, TYupSchemaType } from "./yup";
6
8
  export type { IYupValidationRule } from "./yup";
7
9
  export interface IFrontendEngineProps<V = undefined> {
8
10
  /** HTML class attribute that is applied on the `<form>` element */
9
11
  className?: string;
10
- /** JSON configuration to define the fields and functionalities of the form */
12
+ /** JSON configuration to define the components and functionalities of the form */
11
13
  data?: IFrontendEngineData<V> | undefined;
12
14
  /** Fires every time a value changes in any fields */
13
15
  onChange?: ((values: TFrontendEngineValues, isValid?: boolean | undefined) => unknown) | undefined;
@@ -19,8 +21,14 @@ export interface IFrontendEngineData<V = undefined> {
19
21
  className?: string | undefined;
20
22
  /** Fields' initial values on mount. The key of each field needs to match the id used in the field */
21
23
  defaultValues?: TFrontendEngineValues | undefined;
22
- /** All elements within the form in key-value format, key refers to the id of the field while value refers to the JSON schema of the field */
23
- fields: Record<string, TFrontendEngineFieldSchema<V>>;
24
+ /**
25
+ * Specifies the components to be rendered
26
+ *
27
+ * All components within the form are in key-value format, key refers to the id of the components while value refers to its JSON schema
28
+ *
29
+ * Note: sections accept only section `uiType`, the subsequent children accepts uiType other than section
30
+ * */
31
+ sections: Record<string, ISectionSchema<V>>;
24
32
  /** Unique HTML id attribute that is applied on the `<form>` element */
25
33
  id?: string | undefined;
26
34
  /** Validation strategy when inputs with errors get re-validated after a user submits the form (onSubmit event) */
@@ -28,7 +36,7 @@ export interface IFrontendEngineData<V = undefined> {
28
36
  /** Validation strategy before a user submits the form (onSubmit event) */
29
37
  validationMode?: TValidationMode | undefined;
30
38
  }
31
- export type TFrontendEngineFieldSchema<V = undefined> = ITextareaSchema<V> | ITextfieldSchema<V> | IEmailSchema<V> | INumberSchema<V> | ISubmitButtonSchema | ISelectSchema<V> | IMultiSelectSchema<V> | ICheckboxGroupSchema<V> | IDateInputSchema<V> | IWrapperSchema | IContactNumberSchema<V> | IRadioButtonGroupSchema<V> | ITimePickerSchema<V> | IChipsSchema<V> | IAlertSchema | ITextSchema;
39
+ export type TFrontendEngineFieldSchema<V = undefined> = ITextareaSchema<V> | ITextFieldSchema<V> | IEmailFieldSchema<V> | INumericFieldSchema<V> | ISubmitButtonSchema | ISelectSchema<V> | IMultiSelectSchema<V> | ICheckboxGroupSchema<V> | IDateFieldSchema<V> | IWrapperSchema | IContactFieldSchema<V> | IRadioButtonGroupSchema<V> | ITimeFieldSchema<V> | IChipsSchema<V> | IUnitNumberFieldSchema<V> | IAlertSchema | ITextSchema | IResetButtonSchema | IFilterSchema | ICustomComponentJsonSchema<V>;
32
40
  export type TFrontendEngineValues<T = any> = Record<keyof T, T[keyof T]>;
33
41
  export type TRevalidationMode = Exclude<keyof ValidationMode, "onTouched" | "all">;
34
42
  export type TValidationMode = keyof ValidationMode;
@@ -37,19 +45,22 @@ export type TErrorPayload = Record<string, TErrorMessage>;
37
45
  export interface IFrontendEngineRef extends HTMLFormElement {
38
46
  /** gets form values */
39
47
  getValues: () => TFrontendEngineValues;
40
- /** set field value by id */
48
+ /** sets field value by id */
41
49
  setValue: UseFormSetValue<TFrontendEngineValues>;
42
- /** check if form is valid */
50
+ /** checks if form is valid */
43
51
  isValid: () => boolean;
44
52
  /** triggers form submission */
45
53
  submit: () => void;
54
+ /** adds custom validation rule */
46
55
  addCustomValidation: (type: TYupSchemaType | "mixed", name: string, fn: (value: unknown, arg: unknown) => boolean) => void;
47
- /** allow setting of custom errors thrown by endpoints */
56
+ /** allows setting of custom errors thrown by endpoints */
48
57
  setErrors: (errors: TErrorPayload) => void;
58
+ /** resets the form to the default state */
59
+ reset: UseFormReset<TFrontendEngineValues>;
49
60
  }
50
61
  export interface IFrontendEngineBaseFieldJsonSchema<T, V = undefined, U = undefined> {
51
- /** defines what kind of field to be rendered */
52
- fieldType: T;
62
+ /** defines what kind of component to be rendered */
63
+ uiType: T;
53
64
  /** caption for the field */
54
65
  label: string;
55
66
  /** render conditions
@@ -58,32 +69,62 @@ export interface IFrontendEngineBaseFieldJsonSchema<T, V = undefined, U = undefi
58
69
  showIf?: TRenderRules[] | undefined;
59
70
  /** validation config, can be customised by passing generics */
60
71
  validation?: (V | U | IYupValidationRule)[];
72
+ /** escape hatch for other form / frontend engines to have unsupported attributes */
73
+ customOptions?: Record<string, unknown> | undefined;
74
+ }
75
+ /**
76
+ * to support custom components from other form / frontend engines
77
+ */
78
+ export interface ICustomComponentJsonSchema<T> {
79
+ referenceKey: T;
80
+ uiType?: never | undefined;
81
+ }
82
+ export interface ICustomFieldJsonSchema<T, V = undefined, U = undefined> extends ICustomComponentJsonSchema<T> {
83
+ validation?: (V | U | IYupValidationRule)[];
84
+ /** render conditions
85
+ * - need to fulfil at least 1 object in array (OR condition)
86
+ * - in order for an object to be valid, need to fulfil all conditions in that object (AND condition) */
87
+ showIf?: TRenderRules[] | undefined;
61
88
  }
62
89
  /**
63
90
  * JSON keys to omit from field schema when extending from other interfaces
64
91
  * - keys already defined in `IFrontendEngineBaseFieldJsonSchema` to prevent collision
65
92
  * - some inherited HTML attributes
66
93
  */
67
- export type TFrontendEngineFieldJsonSchemaOmitKeys = "id" | "label" | "validation" | "fieldType" | "showIf" | "children" | "value";
94
+ export type TFrontendEngineFieldJsonSchemaOmitKeys = "id" | "label" | "validation" | "uiType" | "showIf" | "children" | "value";
68
95
  export interface IFrontendEngineElementJsonSchema<T> extends Omit<IFrontendEngineBaseFieldJsonSchema<T>, "label" | "validation"> {
69
96
  }
70
97
  type UnionOptionalKeys<T = undefined> = T extends string | number | symbol ? TFrontendEngineFieldJsonSchemaOmitKeys | T : TFrontendEngineFieldJsonSchemaOmitKeys;
98
+ /**
99
+ * Omits clashing keys between native props and frontend engine
100
+ */
71
101
  export type TComponentOmitProps<T, V = undefined> = Omit<T, UnionOptionalKeys<V>>;
102
+ /**
103
+ * Field types
104
+ * - components that can contain values that can get submitted
105
+ */
72
106
  export declare enum EFieldType {
73
107
  TEXTAREA = "Textarea",
74
- TEXT = "TextField",
75
- NUMERIC = "TextField",
76
- EMAIL = "TextField",
108
+ "TEXT-FIELD" = "TextField",
109
+ "NUMERIC-FIELD" = "TextField",
110
+ "EMAIL-FIELD" = "TextField",
77
111
  SUBMIT = "SubmitButton",
78
112
  SELECT = "Select",
79
113
  "MULTI-SELECT" = "MultiSelect",
80
- DATE = "DateInput",
114
+ "DATE-FIELD" = "DateField",
81
115
  CHECKBOX = "CheckboxGroup",
82
- CONTACT = "ContactNumber",
116
+ "CONTACT-FIELD" = "ContactField",
83
117
  RADIO = "RadioButtonGroup",
84
- TIME = "TimePicker",
85
- CHIPS = "Chips"
118
+ "TIME-FIELD" = "TimeField",
119
+ CHIPS = "Chips",
120
+ RESET = "ResetButton",
121
+ "UNIT-NUMBER-FIELD" = "UnitNumberField"
86
122
  }
123
+ /**
124
+ * Non-field types
125
+ * - components that do not have values
126
+ * - typically used for layouts and messages
127
+ */
87
128
  export declare enum EElementType {
88
129
  ALERT = "Alert",
89
130
  "TEXT-D1" = "Text",
@@ -100,7 +141,6 @@ export declare enum EElementType {
100
141
  "TEXT-XSMALL" = "Text",
101
142
  DIV = "Wrapper",
102
143
  SPAN = "Wrapper",
103
- SECTION = "Wrapper",
104
144
  HEADER = "Wrapper",
105
145
  FOOTER = "Wrapper",
106
146
  H1 = "Wrapper",
@@ -111,7 +151,23 @@ export declare enum EElementType {
111
151
  H6 = "Wrapper",
112
152
  P = "Wrapper"
113
153
  }
154
+ /**
155
+ * Custom element types
156
+ * - components that do not have uiType and have specific schema to render
157
+ */
158
+ export declare enum ECustomElementType {
159
+ FILTER = "Filter",
160
+ "FILTER-ITEM" = "FilterItem"
161
+ }
162
+ export declare enum ECustomFieldType {
163
+ "FILTER-CHECKBOX" = "FilterCheckbox"
164
+ }
114
165
  export interface IGenericFieldProps<T = TFrontendEngineFieldSchema> extends Partial<ControllerFieldState>, Partial<ControllerRenderProps> {
115
166
  id: string;
116
167
  schema: T;
117
168
  }
169
+ /**
170
+ * prevents inferrence
171
+ * https://stackoverflow.com/questions/56687668/a-way-to-disable-type-argument-inference-in-generics
172
+ */
173
+ export type TNoInfer<T, U> = [T][T extends U ? 0 : never];
@@ -8,5 +8,5 @@ interface IProps {
8
8
  children: ReactElement;
9
9
  }
10
10
  export declare const YupContext: React.Context<IYupContext>;
11
- export declare const YupProvider: ({ children }: IProps) => JSX.Element;
11
+ export declare const YupProvider: ({ children }: IProps) => import("react/jsx-runtime").JSX.Element;
12
12
  export {};
@@ -2,5 +2,5 @@ import React from "react";
2
2
  import { IChipButtonProps } from "./types";
3
3
  interface IProps extends React.ButtonHTMLAttributes<HTMLButtonElement>, IChipButtonProps {
4
4
  }
5
- export declare const Chip: ({ children, ...otherProps }: IProps) => JSX.Element;
5
+ export declare const Chip: ({ children, ...otherProps }: IProps) => import("react/jsx-runtime").JSX.Element;
6
6
  export {};
@@ -18,6 +18,9 @@ export declare const ERROR_MESSAGES: {
18
18
  MAX_DATE: (date: string) => string;
19
19
  INVALID: string;
20
20
  };
21
+ UNIT_NUMBER: {
22
+ INVALID: string;
23
+ };
21
24
  GENERIC: {
22
25
  INVALID: string;
23
26
  UNSUPPORTED: string;
@@ -3,5 +3,5 @@ interface IProps {
3
3
  id?: string;
4
4
  children: string | React.ReactNode;
5
5
  }
6
- export declare const Sanitize: (props: IProps) => JSX.Element;
6
+ export declare const Sanitize: (props: IProps) => import("react/jsx-runtime").JSX.Element;
7
7
  export {};