Skip to content

Commit d7c741c

Browse files
committed
Disable create option for input selects if an entity with this name already exists
1 parent 9502f30 commit d7c741c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

assets/controllers/elements/structural_entity_select_controller.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default class extends Controller {
4848
selectOnTab: true,
4949
maxOptions: null,
5050
create: allowAdd ? this.createItem.bind(this) : false,
51+
createFilter: this.createFilter.bind(this),
5152

5253
// This three options allow us to paste element names with commas: (see issue #538)
5354
maxItems: 1,
@@ -128,6 +129,31 @@ export default class extends Controller {
128129
});
129130
}
130131

132+
createFilter(input) {
133+
134+
//Normalize the input (replace spacing around arrows)
135+
if (input.includes("->")) {
136+
const inputs = input.split("->");
137+
inputs.forEach((value, index) => {
138+
inputs[index] = value.trim();
139+
});
140+
input = inputs.join("->");
141+
} else {
142+
input = input.trim();
143+
}
144+
145+
const options = this._tomSelect.options;
146+
//Iterate over all options and check if the input is already present
147+
for (let index in options) {
148+
const option = options[index];
149+
if (option.path === input) {
150+
return false;
151+
}
152+
}
153+
154+
return true;
155+
}
156+
131157

132158
updateValidity() {
133159
//Mark this input as invalid, if the selected option is disabled

0 commit comments

Comments
 (0)