Skip to content

Commit 5030fa5

Browse files
committed
Controlled vocabulary support
1 parent 47e3d71 commit 5030fa5

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/components/Form/fields/Autosuggest.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,25 @@
9595
name="option"
9696
:suggestion="suggestion"
9797
/>
98-
<span v-else>{{ suggestion.label }}</span>
98+
<ul v-else>
99+
<li>
100+
{{ suggestion.label }}
101+
</li>
102+
<li v-if="suggestion.identifier?.match(/^http/)">
103+
<a :href="suggestion.identifier" target="_blank" @click.stop>
104+
{{ suggestion.identifier }}
105+
</a>
106+
</li>
107+
<li v-else-if="suggestion.identifier">
108+
{{ suggestion.identifier }}
109+
</li>
110+
<li
111+
v-for="(extraItem, extraItemKey) in suggestion.extraItems ?? {}"
112+
:key="extraItemKey"
113+
>
114+
{{ extraItem }}
115+
</li>
116+
</ul>
99117
</li>
100118
</ComboboxOption>
101119
</template>
@@ -302,6 +320,12 @@ defineExpose({handleFocus});
302320
background: @primary;
303321
transition: width 0.3s;
304322
}
323+
324+
& ul li {
325+
white-space: nowrap;
326+
overflow: hidden;
327+
text-overflow: ellipsis;
328+
}
305329
}
306330
307331
.autosuggest__results-item--highlighted {

src/components/Form/fields/FieldControlledVocab.vue

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
import FieldBaseAutosuggest from './FieldBaseAutosuggest.vue';
33
import debounce from 'debounce';
44
5+
function setSuggestion(value) {
6+
const {name, label = null, identifier = null, ...extraItems} = value;
7+
const suggestion = {
8+
value: value,
9+
label: label ?? name,
10+
identifier: identifier,
11+
...(extraItems ? {extraItems: extraItems} : {}),
12+
};
13+
return suggestion;
14+
}
15+
516
export default {
617
name: 'FieldControlledVocab',
718
extends: FieldBaseAutosuggest,
@@ -15,7 +26,6 @@ export default {
1526
data() {
1627
return {
1728
allSuggestions: [],
18-
suggestionsLoaded: false,
1929
suggestionsLoading: false,
2030
};
2131
},
@@ -31,40 +41,35 @@ export default {
3141
if (this.suggestionsLoading) {
3242
return;
3343
}
34-
if (!this.suggestionsLoaded) {
35-
this.loadSuggestions(this.setSuggestions);
36-
}
44+
this.loadSuggestions(this.setSuggestions);
3745
this.setSuggestions();
3846
},
3947
4048
/**
4149
* Load suggestions from the API
4250
*/
43-
loadSuggestions(successCallback) {
51+
loadSuggestions: debounce(function (successCallback) {
4452
this.suggestionsLoading = true;
4553
$.ajax({
4654
url: this.apiUrl,
4755
type: 'GET',
4856
context: this,
49-
data: this.isMultilingual ? {locale: this.localeKey} : {},
57+
data: {
58+
term: this.inputValue ?? null,
59+
...(this.isMultilingual ? {locale: this.localeKey} : {}),
60+
},
5061
error(r) {
5162
this.ajaxErrorCallback(r);
5263
},
5364
success(r) {
54-
this.allSuggestions = r.map((v) => {
55-
return {
56-
value: v,
57-
label: v,
58-
};
59-
});
60-
this.suggestionsLoaded = true;
65+
this.allSuggestions = r.map((v) => setSuggestion(v));
6166
this.suggestionsLoading = false;
6267
if (successCallback) {
6368
successCallback.apply(this);
6469
}
6570
},
6671
});
67-
},
72+
}, 250),
6873
6974
/**
7075
* Override the parent method to accept any typed
@@ -77,7 +82,7 @@ export default {
7782
this.select(suggestion);
7883
} else if (this.inputValue) {
7984
this.select({
80-
value: this.inputValue,
85+
value: {name: this.inputValue},
8186
label: this.inputValue,
8287
});
8388
}
@@ -99,8 +104,8 @@ export default {
99104
this.suggestions = this.allSuggestions.filter(
100105
(suggestion) =>
101106
!this.inputValue ||
102-
(this.inputValue !== suggestion.value &&
103-
suggestion.value.match(regex)),
107+
(this.inputValue !== suggestion.value.name &&
108+
suggestion.value.name.match(regex)),
104109
);
105110
}, 250),
106111
},

0 commit comments

Comments
 (0)