feat: JSON Form widget (#8472)
* initial layout
* updated parser to support nested array
* array field rendering
* changes
* ts fix
* minor revert FormWidget
* modified schema structure
* select and switch fields
* added checkbox field
* added RadioGroupField
* partial DateField and defaults, typing refactoring
* added label and field type change
* minor ts changes
* changes
* modified widget/utils for nested panelConfig, modified schema to object approach
* array/object label support
* hide field configuration when children not present
* added tooltip
* field visibility option
* disabled state
* upgraded tslib, form initial values
* custom field configuration - add/hide/edit
* field configuration - label change
* return input when field configuration reaches max depth
* minor changes
* form - scroll, fixedfooter, enitity defn and other minior changes
* form title
* unregister on unmount
* fixes
* zero state
* fix field padding
* patched updating form values, removed linting warnings
* configured action buttons
* minor fix
* minor change
* property pane - sort fields in field configuration
* refactor include all properties
* checkbox properties
* date properties
* refactor typings and radio group properties
* switch, multselect, select, array, object properties
* minor changes
* default value
* ts fixes
* checkbox field properties implementation
* date field prop implementation
* switch field
* select field and fix deep nested meta properties
* multiselect implementation
* minor change
* input field implementation
* fix position jump on field type change
* initial accordian
* field state property and auto-complete of JSONFormComputeControl
* merge fixes
* renamed FormBuilder to JSONForm
* source data validation minor change
* custom field default value fix
* Editable keys for custom field
* minor fixes
* replaced useFieldArray with custom logic, added widget icon
* array and object accordian with border/background styling
* minor change
* disabled states for array and objects
* default value minor fix
* form level styles
* modified logic for isDisabled for array and object, added disabledWhenInvalid, exposed isValid to fieldState for text input, removed useDisableChildren
* added isValid for all field types
* fixed reset to default values
* debounce form values update
* minor change
* minor change
* fix crash - source data change multi-select to array, fix crash - change of options
* fix positioning
* detect date type in source data
* fix crash - when object is passed to regex input field
* fixed default sourceData path for fields
* accodion keep children mounted on collapse
* jest test for schemaParser
* widget/helper and useRegisterFieldInvalid test
* tests for property config helper and generatePanelPropertyConfig
* fix input field validation not appearing
* fix date field type detection
* rename data -> formData
* handle null/undefined field value change in sourceData
* added null/undefined as valid values for defaultValue text field
* auto detect email field
* set formData default value on initial load
* switch field inline positioning
* field margin fix for row direction
* select full width
* fiex date field default value - out of range
* fix any field type to array
* array default value logic change
* base cypress test changes
* initial json form render cy test
* key sanitization
* fix fieldState update logic
* required design, object/array background color, accordion changes, fix - add new custom field
* minor change
* cypress tests
* fix date formatted value, field state cypress test
* cypress - field properties test and fixes
* rename test file
* fix accessort change to blank value, cypress tests
* fix array field default value for modified accessor
* minor fix
* added animate loading
* fix empty state, add new custom field
* test data fix
* fix warnings
* fix timePrecision visibility
* button styling
* ported input v2
* fix jest tests
* fix cypress tests
* perf changes
* perf improvement
* added comments
* multiselect changes
* input field perf refactor
* array field, object field refactor performance
* checkbox field refactor
* refectored date, radio, select and switch
* fixes
* test fixes
* fixes
* minor fix
* rename field renderer
* remove tracked fieldRenderer field
* cypress test fixes
* cypress changes
* array default value fixes
* arrayfield passedDefaultValue
* auto enabled JS mode for few properties, reverted swith and date property controls
* cypress changes
* added widget sniping mode and fixed object passedDefaultValue
* multiselect v2
* select v2
* fix jest tests
* test fixes
* field limit
* rename field type dropdown texts
* field type changes fixes
* jest fixes
* loading state submit button
* default source data for new widget
* modify limit message
* multiseelct default value changes and cypress fix
* select default value
* keep default value intact on field type change
* TextTable cypress text fix
* review changes
* fixed footer changes
* collapse styles section by default
* fixed footer changes
* form modes
* custom field key rentention
* fixed footer fix in view mode
* non ascii characters
* fix meta merge in dataTreeWidget
* minor fixes
* rename useRegisterFieldInvalid.ts -> useRegisterFieldValidity.ts
* modified dependency injection into evaluated values
* refactored fixedfooter logic
* minor change
* accessor update
* minor change
* fixes
* QA fixes date field, scroll content
* fix phone number field, removed visiblity option from array item
* fix sourceData autocomplete
* reset logic
* fix multiselect reset
* form values hydration on widget drag
* code review changes
* reverted order of merge dataTreeWidget
* fixes
* added button titles, fixed hydration issue
* default value fixes
* upgraded react hook form, modified array-level/field-level default value logic
* fixed select validation
* added icon entity explorer, modified icon align control
* modify accessor validation for mongo db _id
* update email field regex
* review changes
* explicitly handle empty source data validation
2022-03-24 07:13:25 +00:00
|
|
|
import { isNil, isPlainObject, merge } from "lodash";
|
|
|
|
|
import { LabelValueType } from "rc-select/lib/interface/generator";
|
|
|
|
|
|
2022-05-04 09:45:57 +00:00
|
|
|
import {
|
|
|
|
|
ARRAY_ITEM_KEY,
|
|
|
|
|
FieldThemeStylesheet,
|
|
|
|
|
FieldType,
|
|
|
|
|
inverseFieldType,
|
|
|
|
|
Schema,
|
|
|
|
|
SchemaItem,
|
|
|
|
|
} from "./constants";
|
feat: JSON Form widget (#8472)
* initial layout
* updated parser to support nested array
* array field rendering
* changes
* ts fix
* minor revert FormWidget
* modified schema structure
* select and switch fields
* added checkbox field
* added RadioGroupField
* partial DateField and defaults, typing refactoring
* added label and field type change
* minor ts changes
* changes
* modified widget/utils for nested panelConfig, modified schema to object approach
* array/object label support
* hide field configuration when children not present
* added tooltip
* field visibility option
* disabled state
* upgraded tslib, form initial values
* custom field configuration - add/hide/edit
* field configuration - label change
* return input when field configuration reaches max depth
* minor changes
* form - scroll, fixedfooter, enitity defn and other minior changes
* form title
* unregister on unmount
* fixes
* zero state
* fix field padding
* patched updating form values, removed linting warnings
* configured action buttons
* minor fix
* minor change
* property pane - sort fields in field configuration
* refactor include all properties
* checkbox properties
* date properties
* refactor typings and radio group properties
* switch, multselect, select, array, object properties
* minor changes
* default value
* ts fixes
* checkbox field properties implementation
* date field prop implementation
* switch field
* select field and fix deep nested meta properties
* multiselect implementation
* minor change
* input field implementation
* fix position jump on field type change
* initial accordian
* field state property and auto-complete of JSONFormComputeControl
* merge fixes
* renamed FormBuilder to JSONForm
* source data validation minor change
* custom field default value fix
* Editable keys for custom field
* minor fixes
* replaced useFieldArray with custom logic, added widget icon
* array and object accordian with border/background styling
* minor change
* disabled states for array and objects
* default value minor fix
* form level styles
* modified logic for isDisabled for array and object, added disabledWhenInvalid, exposed isValid to fieldState for text input, removed useDisableChildren
* added isValid for all field types
* fixed reset to default values
* debounce form values update
* minor change
* minor change
* fix crash - source data change multi-select to array, fix crash - change of options
* fix positioning
* detect date type in source data
* fix crash - when object is passed to regex input field
* fixed default sourceData path for fields
* accodion keep children mounted on collapse
* jest test for schemaParser
* widget/helper and useRegisterFieldInvalid test
* tests for property config helper and generatePanelPropertyConfig
* fix input field validation not appearing
* fix date field type detection
* rename data -> formData
* handle null/undefined field value change in sourceData
* added null/undefined as valid values for defaultValue text field
* auto detect email field
* set formData default value on initial load
* switch field inline positioning
* field margin fix for row direction
* select full width
* fiex date field default value - out of range
* fix any field type to array
* array default value logic change
* base cypress test changes
* initial json form render cy test
* key sanitization
* fix fieldState update logic
* required design, object/array background color, accordion changes, fix - add new custom field
* minor change
* cypress tests
* fix date formatted value, field state cypress test
* cypress - field properties test and fixes
* rename test file
* fix accessort change to blank value, cypress tests
* fix array field default value for modified accessor
* minor fix
* added animate loading
* fix empty state, add new custom field
* test data fix
* fix warnings
* fix timePrecision visibility
* button styling
* ported input v2
* fix jest tests
* fix cypress tests
* perf changes
* perf improvement
* added comments
* multiselect changes
* input field perf refactor
* array field, object field refactor performance
* checkbox field refactor
* refectored date, radio, select and switch
* fixes
* test fixes
* fixes
* minor fix
* rename field renderer
* remove tracked fieldRenderer field
* cypress test fixes
* cypress changes
* array default value fixes
* arrayfield passedDefaultValue
* auto enabled JS mode for few properties, reverted swith and date property controls
* cypress changes
* added widget sniping mode and fixed object passedDefaultValue
* multiselect v2
* select v2
* fix jest tests
* test fixes
* field limit
* rename field type dropdown texts
* field type changes fixes
* jest fixes
* loading state submit button
* default source data for new widget
* modify limit message
* multiseelct default value changes and cypress fix
* select default value
* keep default value intact on field type change
* TextTable cypress text fix
* review changes
* fixed footer changes
* collapse styles section by default
* fixed footer changes
* form modes
* custom field key rentention
* fixed footer fix in view mode
* non ascii characters
* fix meta merge in dataTreeWidget
* minor fixes
* rename useRegisterFieldInvalid.ts -> useRegisterFieldValidity.ts
* modified dependency injection into evaluated values
* refactored fixedfooter logic
* minor change
* accessor update
* minor change
* fixes
* QA fixes date field, scroll content
* fix phone number field, removed visiblity option from array item
* fix sourceData autocomplete
* reset logic
* fix multiselect reset
* form values hydration on widget drag
* code review changes
* reverted order of merge dataTreeWidget
* fixes
* added button titles, fixed hydration issue
* default value fixes
* upgraded react hook form, modified array-level/field-level default value logic
* fixed select validation
* added icon entity explorer, modified icon align control
* modify accessor validation for mongo db _id
* update email field regex
* review changes
* explicitly handle empty source data validation
2022-03-24 07:13:25 +00:00
|
|
|
|
|
|
|
|
type ConvertFormDataOptions = {
|
|
|
|
|
fromId: keyof SchemaItem | (keyof SchemaItem)[];
|
|
|
|
|
toId: keyof SchemaItem;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function finds the value from the object by using the id provided. The id
|
|
|
|
|
* can be one of the key that is present in the schemaItem (identifier/accessor/defaultValue).
|
|
|
|
|
* This keys value is picked up from the schema item and used for the lookup in the obj.
|
|
|
|
|
* The id is either a single or an array. If it is an array, each key is picked up and tested
|
|
|
|
|
* until a value is found (null would be considered as a value but undefined would mean it's empty).
|
|
|
|
|
*/
|
|
|
|
|
const valueLookup = (
|
|
|
|
|
obj: Record<string, unknown>,
|
|
|
|
|
schemaItem: SchemaItem,
|
|
|
|
|
id: ConvertFormDataOptions["fromId"],
|
|
|
|
|
) => {
|
|
|
|
|
if (typeof id === "string") {
|
|
|
|
|
return obj[schemaItem[id]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const key of id) {
|
|
|
|
|
const value = obj[schemaItem[key]];
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-04 09:45:57 +00:00
|
|
|
export const getFieldStylesheet = (
|
|
|
|
|
fieldType: FieldType,
|
|
|
|
|
fieldThemeStylesheets?: FieldThemeStylesheet,
|
|
|
|
|
) => {
|
|
|
|
|
const fieldTypeKey = inverseFieldType[fieldType];
|
|
|
|
|
return fieldThemeStylesheets && fieldTypeKey in fieldThemeStylesheets
|
|
|
|
|
? fieldThemeStylesheets[fieldTypeKey]
|
|
|
|
|
: {};
|
|
|
|
|
};
|
|
|
|
|
|
feat: JSON Form widget (#8472)
* initial layout
* updated parser to support nested array
* array field rendering
* changes
* ts fix
* minor revert FormWidget
* modified schema structure
* select and switch fields
* added checkbox field
* added RadioGroupField
* partial DateField and defaults, typing refactoring
* added label and field type change
* minor ts changes
* changes
* modified widget/utils for nested panelConfig, modified schema to object approach
* array/object label support
* hide field configuration when children not present
* added tooltip
* field visibility option
* disabled state
* upgraded tslib, form initial values
* custom field configuration - add/hide/edit
* field configuration - label change
* return input when field configuration reaches max depth
* minor changes
* form - scroll, fixedfooter, enitity defn and other minior changes
* form title
* unregister on unmount
* fixes
* zero state
* fix field padding
* patched updating form values, removed linting warnings
* configured action buttons
* minor fix
* minor change
* property pane - sort fields in field configuration
* refactor include all properties
* checkbox properties
* date properties
* refactor typings and radio group properties
* switch, multselect, select, array, object properties
* minor changes
* default value
* ts fixes
* checkbox field properties implementation
* date field prop implementation
* switch field
* select field and fix deep nested meta properties
* multiselect implementation
* minor change
* input field implementation
* fix position jump on field type change
* initial accordian
* field state property and auto-complete of JSONFormComputeControl
* merge fixes
* renamed FormBuilder to JSONForm
* source data validation minor change
* custom field default value fix
* Editable keys for custom field
* minor fixes
* replaced useFieldArray with custom logic, added widget icon
* array and object accordian with border/background styling
* minor change
* disabled states for array and objects
* default value minor fix
* form level styles
* modified logic for isDisabled for array and object, added disabledWhenInvalid, exposed isValid to fieldState for text input, removed useDisableChildren
* added isValid for all field types
* fixed reset to default values
* debounce form values update
* minor change
* minor change
* fix crash - source data change multi-select to array, fix crash - change of options
* fix positioning
* detect date type in source data
* fix crash - when object is passed to regex input field
* fixed default sourceData path for fields
* accodion keep children mounted on collapse
* jest test for schemaParser
* widget/helper and useRegisterFieldInvalid test
* tests for property config helper and generatePanelPropertyConfig
* fix input field validation not appearing
* fix date field type detection
* rename data -> formData
* handle null/undefined field value change in sourceData
* added null/undefined as valid values for defaultValue text field
* auto detect email field
* set formData default value on initial load
* switch field inline positioning
* field margin fix for row direction
* select full width
* fiex date field default value - out of range
* fix any field type to array
* array default value logic change
* base cypress test changes
* initial json form render cy test
* key sanitization
* fix fieldState update logic
* required design, object/array background color, accordion changes, fix - add new custom field
* minor change
* cypress tests
* fix date formatted value, field state cypress test
* cypress - field properties test and fixes
* rename test file
* fix accessort change to blank value, cypress tests
* fix array field default value for modified accessor
* minor fix
* added animate loading
* fix empty state, add new custom field
* test data fix
* fix warnings
* fix timePrecision visibility
* button styling
* ported input v2
* fix jest tests
* fix cypress tests
* perf changes
* perf improvement
* added comments
* multiselect changes
* input field perf refactor
* array field, object field refactor performance
* checkbox field refactor
* refectored date, radio, select and switch
* fixes
* test fixes
* fixes
* minor fix
* rename field renderer
* remove tracked fieldRenderer field
* cypress test fixes
* cypress changes
* array default value fixes
* arrayfield passedDefaultValue
* auto enabled JS mode for few properties, reverted swith and date property controls
* cypress changes
* added widget sniping mode and fixed object passedDefaultValue
* multiselect v2
* select v2
* fix jest tests
* test fixes
* field limit
* rename field type dropdown texts
* field type changes fixes
* jest fixes
* loading state submit button
* default source data for new widget
* modify limit message
* multiseelct default value changes and cypress fix
* select default value
* keep default value intact on field type change
* TextTable cypress text fix
* review changes
* fixed footer changes
* collapse styles section by default
* fixed footer changes
* form modes
* custom field key rentention
* fixed footer fix in view mode
* non ascii characters
* fix meta merge in dataTreeWidget
* minor fixes
* rename useRegisterFieldInvalid.ts -> useRegisterFieldValidity.ts
* modified dependency injection into evaluated values
* refactored fixedfooter logic
* minor change
* accessor update
* minor change
* fixes
* QA fixes date field, scroll content
* fix phone number field, removed visiblity option from array item
* fix sourceData autocomplete
* reset logic
* fix multiselect reset
* form values hydration on widget drag
* code review changes
* reverted order of merge dataTreeWidget
* fixes
* added button titles, fixed hydration issue
* default value fixes
* upgraded react hook form, modified array-level/field-level default value logic
* fixed select validation
* added icon entity explorer, modified icon align control
* modify accessor validation for mongo db _id
* update email field regex
* review changes
* explicitly handle empty source data validation
2022-03-24 07:13:25 +00:00
|
|
|
const convertObjectTypeToFormData = (
|
|
|
|
|
schema: Schema,
|
|
|
|
|
formValue: unknown,
|
|
|
|
|
options: ConvertFormDataOptions,
|
|
|
|
|
) => {
|
|
|
|
|
if (formValue && typeof formValue === "object") {
|
|
|
|
|
const formData: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
|
|
Object.values(schema).forEach((schemaItem) => {
|
|
|
|
|
const value = valueLookup(
|
|
|
|
|
formValue as Record<string, unknown>,
|
|
|
|
|
schemaItem,
|
|
|
|
|
options.fromId,
|
|
|
|
|
);
|
|
|
|
|
const toKey = schemaItem[options.toId];
|
|
|
|
|
formData[toKey] = convertSchemaItemToFormData(schemaItem, value, options);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const convertArrayTypeToFormData = (
|
|
|
|
|
schema: Schema,
|
|
|
|
|
formValues: unknown,
|
|
|
|
|
options: ConvertFormDataOptions,
|
|
|
|
|
) => {
|
|
|
|
|
if (formValues && Array.isArray(formValues)) {
|
|
|
|
|
const formData: unknown[] = [];
|
|
|
|
|
const arraySchemaItem = schema[ARRAY_ITEM_KEY];
|
|
|
|
|
|
|
|
|
|
formValues.forEach((formValue, index) => {
|
|
|
|
|
formData[index] = convertSchemaItemToFormData(
|
|
|
|
|
arraySchemaItem,
|
|
|
|
|
formValue,
|
|
|
|
|
options,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return formData.filter((d) => d !== undefined);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function iterates through the schemaItem and returns a value
|
|
|
|
|
* that matches with the the value param passed. The return value key-value pairs
|
|
|
|
|
* are determined by what the fromKey and toKey values are. If the fromKey is accessor
|
|
|
|
|
* and toKey is identifier then it will look in the input value object with the identifier of the
|
|
|
|
|
* schemaItem and would set it as the accessor value of schemaItem.
|
|
|
|
|
*
|
|
|
|
|
* This helps to transform formData from having accessor as key to identifier as keys and vice-versa
|
|
|
|
|
*
|
|
|
|
|
* If schemaItem
|
|
|
|
|
* {
|
|
|
|
|
* identifier: "address",
|
|
|
|
|
* accessor: "addresses",
|
|
|
|
|
* fieldType: "object",
|
|
|
|
|
* children: {
|
|
|
|
|
* "line1": {
|
|
|
|
|
* identifier: "line1",
|
|
|
|
|
* accessor: "line1",
|
|
|
|
|
* originalIdentifier: "line1"
|
|
|
|
|
* },
|
|
|
|
|
* "pincode": {
|
|
|
|
|
* accessor: "zipcode"
|
|
|
|
|
* identifier: "pincode",
|
|
|
|
|
* originalIdentifier: "pincode"
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* Example 1
|
|
|
|
|
* ----------
|
|
|
|
|
* fromKey = identifier
|
|
|
|
|
* toKey = accessor
|
|
|
|
|
*
|
|
|
|
|
* value
|
|
|
|
|
* {
|
|
|
|
|
* address: {
|
|
|
|
|
* line1: "24th main",
|
|
|
|
|
* pincode: "230123",
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @returns
|
|
|
|
|
* {
|
|
|
|
|
* addresses: {
|
|
|
|
|
* line1: "24th main",
|
|
|
|
|
* pincode: "230123"
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* Example 2
|
|
|
|
|
* ----------
|
|
|
|
|
* fromKey = accessor
|
|
|
|
|
* toKey = identifier
|
|
|
|
|
*
|
|
|
|
|
* value
|
|
|
|
|
* {
|
|
|
|
|
* addresses: {
|
|
|
|
|
* line1: "24th main",
|
|
|
|
|
* zipcode: "230123",
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* @returns
|
|
|
|
|
* {
|
|
|
|
|
* address: {
|
|
|
|
|
* line1: "24th main",
|
|
|
|
|
* pincode: "230123"
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
export const convertSchemaItemToFormData = <TValue>(
|
|
|
|
|
schemaItem: SchemaItem,
|
|
|
|
|
formValue: TValue,
|
|
|
|
|
options: ConvertFormDataOptions,
|
|
|
|
|
) => {
|
|
|
|
|
if (schemaItem.fieldType === FieldType.OBJECT) {
|
|
|
|
|
return convertObjectTypeToFormData(schemaItem.children, formValue, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (schemaItem.fieldType === FieldType.ARRAY) {
|
|
|
|
|
return convertArrayTypeToFormData(schemaItem.children, formValue, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const processObject = (schema: Schema, toKey: keyof SchemaItem) => {
|
|
|
|
|
const obj: Record<string, any> = {};
|
|
|
|
|
Object.values(schema).forEach((schemaItem) => {
|
|
|
|
|
obj[schemaItem[toKey]] = schemaItemDefaultValue(schemaItem, toKey);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const processArray = (schema: Schema, toKey: keyof SchemaItem): any[] => {
|
|
|
|
|
if (schema[ARRAY_ITEM_KEY]) {
|
|
|
|
|
return [schemaItemDefaultValue(schema[ARRAY_ITEM_KEY], toKey)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const schemaItemDefaultValue = (
|
|
|
|
|
schemaItem: SchemaItem,
|
|
|
|
|
toKey: keyof SchemaItem,
|
|
|
|
|
) => {
|
|
|
|
|
if (schemaItem.fieldType === FieldType.OBJECT) {
|
|
|
|
|
return processObject(schemaItem.children, toKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (schemaItem.fieldType === FieldType.ARRAY) {
|
|
|
|
|
const defaultArrayValue = processArray(schemaItem.children, toKey);
|
|
|
|
|
let sanitizedDefaultValue: unknown[] = [];
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(schemaItem.defaultValue)) {
|
|
|
|
|
const convertedValue = convertSchemaItemToFormData(
|
|
|
|
|
schemaItem,
|
|
|
|
|
schemaItem.defaultValue as unknown[],
|
|
|
|
|
{
|
|
|
|
|
fromId: ["originalIdentifier", "accessor"],
|
|
|
|
|
toId: "accessor",
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(convertedValue)) {
|
|
|
|
|
sanitizedDefaultValue = convertedValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reason for merge
|
|
|
|
|
* - For an array type, the default value of individual fields underneath the array
|
|
|
|
|
* are not present as array field handles whole default data coming from sourceData directly.
|
|
|
|
|
* So the default value we get from processArray(schemaItem.children) will have some value only if
|
|
|
|
|
* the default value of any field under the array is set explicitly by the user in the property pane.
|
|
|
|
|
* Thus we merge both array level default value and any default value the underlying field holds
|
|
|
|
|
* to get a complete defaultValue.
|
|
|
|
|
*/
|
|
|
|
|
return merge(defaultArrayValue, sanitizedDefaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { defaultValue } = schemaItem;
|
|
|
|
|
return defaultValue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function isPrimitive(val: unknown): val is number | string | boolean {
|
|
|
|
|
return val === null || /^[sbn]/.test(typeof val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const validateOptions = (
|
|
|
|
|
values: unknown,
|
|
|
|
|
): values is LabelValueType["value"][] | LabelValueType[] => {
|
|
|
|
|
if (!Array.isArray(values)) return false;
|
|
|
|
|
|
|
|
|
|
let hasPrimitive = false;
|
|
|
|
|
let hasObject = false;
|
|
|
|
|
for (const value of values) {
|
|
|
|
|
if (isNil(value) || Number.isNaN(value)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (isPrimitive(value)) {
|
|
|
|
|
hasPrimitive = true;
|
|
|
|
|
} else if (isPlainObject(value) && "label" in value && "value" in value) {
|
|
|
|
|
hasObject = true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !(hasPrimitive && hasObject);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const mergeAllObjectsInAnArray = (arrObj: Record<string, unknown>[]) => {
|
|
|
|
|
return arrObj.reduce((r, c) => merge(r, c), {});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const countFields = (
|
|
|
|
|
obj:
|
|
|
|
|
| Record<string, unknown>
|
|
|
|
|
| unknown[]
|
|
|
|
|
| string
|
|
|
|
|
| number
|
|
|
|
|
| boolean
|
|
|
|
|
| null
|
|
|
|
|
| undefined,
|
|
|
|
|
) => {
|
|
|
|
|
let count = 0;
|
|
|
|
|
if (!Array.isArray(obj) && !isPlainObject(obj)) return 0;
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
|
if (isPlainObject(obj[0])) {
|
|
|
|
|
const mergedObject = mergeAllObjectsInAnArray(
|
|
|
|
|
obj as Record<string, unknown>[],
|
|
|
|
|
);
|
|
|
|
|
count += countFields(mergedObject) * obj.length;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (obj && typeof obj === "object") {
|
|
|
|
|
Object.values(obj).forEach((objVal) => {
|
|
|
|
|
count += countFields(objVal as Record<string, unknown>) + 1;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isEmpty = (value?: string | null): value is null | undefined => {
|
|
|
|
|
return value === "" || isNil(value);
|
|
|
|
|
};
|