Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: fix groupable custom select. #553

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,26 @@ export class CustomSelectFieldComponent extends FieldType implements OnDestroy,
* @param options List of options.
*/
private _extract_groups(options: Array<SelectOption>): void {
options.forEach((option, index) => {
// Group is found
if (option.group) {
// DEV NOTES :: Why iterate options in reverse
// splicing an array means this array is re-indexed. Looping on an array in normal mode
// will skip half of this array. Using reverse, we don't have this problem.
// source : https://stackoverflow.com/a/9882349/5595377
let idx = options.length
while (idx--) {
const option = options[idx];
if (options[idx].group) {
let groupOption = options.find((item) => item.label === option.group);
// No group exists at this time
if (!groupOption) {
groupOption = { label: option.group, children: [] };
options.push(groupOption);
}

// Add option to group...
groupOption.children.push({ ...option });
// ...and remove it from array.
options.splice(index, 1);
options.splice(idx, 1);
}
});
}
}

/**
Expand Down