Skip to content

Commit

Permalink
fix: added the validation for section with dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulrajtm committed Sep 28, 2024
1 parent 9da63b7 commit a66f8d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
40 changes: 36 additions & 4 deletions packages/crayons-core/src/utils/draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Draggable {
bubbles: false,
detail: {
droppedElement: dragElement,
dropToId: this.dragContainer.id, // Return the drop container ID
dragContainer: this.dragContainer, // Return the drop container ID
},
})
);
Expand All @@ -106,10 +106,42 @@ export class Draggable {
const dragId = this.dragContainer.id;
const isSection = dragId.includes('sectionIdentifier-');
const isFieldTypeNotAllowed = inValidTypesForSection.includes(
dragElement.dataProvider.type
dragElement.dataProvider?.type
);

// Check if the DROPDOWN is default field
const isDropdownDefaultField =
dragElement.dataProvider?.type === 'DROPDOWN' &&
!dragElement.dataProvider.custom;

const isNewField =
dragElement.dataProvider?.choices?.length > 0 &&
dragElement.dataProvider?.choices[0]?.value === '';

// Allow dropping if it's a new DROPDOWN field
const isAllowedNewDropdown =
dragElement.dataProvider?.type === 'DROPDOWN' && isNewField;

// Check if the DROPDOWN has field_options with hasSections
const hasSectionsInFieldOptions =
dragElement.dataProvider?.type === 'DROPDOWN' &&
dragElement.dataProvider?.field_options &&
dragElement.dataProvider?.field_options.has_sections;

// Check if the section already has the maximum number of fields
const isSectionFieldLimitExceeded =
this.dragContainer?.children?.length > 15;

// Check if the field is required
const isFieldRequired = dragElement.dataProvider.required;
return isSection && (isFieldTypeNotAllowed || isFieldRequired);
return (
isSection &&
(isFieldTypeNotAllowed ||
isFieldRequired ||
isSectionFieldLimitExceeded ||
(isDropdownDefaultField && !isAllowedNewDropdown) ||
hasSectionsInFieldOptions)
);
}

private onDragLeave = (e) => {
Expand Down Expand Up @@ -156,7 +188,7 @@ export class Draggable {
bubbles: false,
detail: {
droppedElement: dragElement,
dropToId: this.dragContainer.id, // Return the drop container ID
dragContainer: this.dragContainer, // Return the drop container ID
},
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,15 @@ export class FormBuilder {
const objDetail = event.detail,
elField = objDetail.droppedElement;
let elFieldType = elField.dataProvider.type;

if (objDetail.dragContainer.children.length > 15) {
elFieldType = 'MAX_LIMIT';
}
if (elField.dataProvider.required) {
elFieldType = 'REQUIRED';
}

if (!elField.dataProvider.custom && elField.dataProvider.label) {
elFieldType = 'DEFAULT';
elFieldType =
elFieldType === 'DEPENDENT_FIELD' ? 'DEPENDENT_FIELD' : 'DEFAULT';
}

this.dragErrorMessages = {
Expand Down
3 changes: 2 additions & 1 deletion packages/crayons-i18n/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@
"DEPENDENT_FIELD" : "Dependent fields cannot be added into sections",
"RELATIONSHIP" : "Lookup fields cannot be added into sections",
"REQUIRED" : "Fields marked as required cannot be added into sections",
"DEFAULT" : "Default fields cannot be added into sections"
"DEFAULT" : "Default fields cannot be added into sections",
"MAX_LIMIT": "Sections cannot contain more than 15 fields"
}
}
},
Expand Down

0 comments on commit a66f8d8

Please sign in to comment.