debian-mirror-gitlab/app/assets/javascripts/listbox/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-03-04 22:38:38 +05:30
import { GlCollapsibleListbox } from '@gitlab/ui';
2022-04-04 11:22:00 +05:30
import Vue from 'vue';
export function parseAttributes(el) {
2023-04-23 21:23:45 +05:30
const { items: itemsString, selected, placement } = el.dataset;
2022-04-04 11:22:00 +05:30
const items = JSON.parse(itemsString);
const { className } = el;
2023-04-23 21:23:45 +05:30
return { items, selected, placement, className };
2022-04-04 11:22:00 +05:30
}
export function initListbox(el, { onChange } = {}) {
if (!el) return null;
2023-04-23 21:23:45 +05:30
const { items, selected, placement, className } = parseAttributes(el);
2022-04-04 11:22:00 +05:30
return new Vue({
el,
data() {
return {
selected,
};
},
computed: {
text() {
return items.find(({ value }) => value === this.selected)?.text;
},
},
render(h) {
2023-03-04 22:38:38 +05:30
return h(GlCollapsibleListbox, {
2022-11-25 23:54:43 +05:30
props: {
items,
2023-04-23 21:23:45 +05:30
placement,
2022-11-25 23:54:43 +05:30
selected: this.selected,
toggleText: this.text,
},
class: className,
on: {
select: (selectedValue) => {
this.selected = selectedValue;
const selectedItem = items.find(({ value }) => value === selectedValue);
if (typeof onChange === 'function') {
onChange(selectedItem);
}
2022-04-04 11:22:00 +05:30
},
},
2022-11-25 23:54:43 +05:30
});
2022-04-04 11:22:00 +05:30
},
});
}