debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/split_button.vue

87 lines
1.7 KiB
Vue
Raw Normal View History

2019-12-26 22:10:19 +05:30
<script>
import _ from 'underscore';
import { GlDropdown, GlDropdownDivider, GlDropdownItem } from '@gitlab/ui';
const isValidItem = item =>
_.isString(item.eventName) && _.isString(item.title) && _.isString(item.description);
export default {
components: {
GlDropdown,
GlDropdownDivider,
GlDropdownItem,
},
props: {
actionItems: {
type: Array,
required: true,
validator(value) {
return value.length > 1 && value.every(isValidItem);
},
},
menuClass: {
type: String,
required: false,
default: '',
},
2020-01-01 13:55:28 +05:30
variant: {
type: String,
required: false,
default: 'secondary',
},
2019-12-26 22:10:19 +05:30
},
data() {
return {
selectedItem: this.actionItems[0],
};
},
computed: {
dropdownToggleText() {
return this.selectedItem.title;
},
},
methods: {
triggerEvent() {
this.$emit(this.selectedItem.eventName);
},
2020-03-09 13:42:32 +05:30
changeSelectedItem(item) {
this.selectedItem = item;
this.$emit('change', item);
},
2019-12-26 22:10:19 +05:30
},
};
</script>
<template>
<gl-dropdown
:menu-class="`dropdown-menu-selectable ${menuClass}`"
split
:text="dropdownToggleText"
2020-01-01 13:55:28 +05:30
:variant="variant"
2019-12-26 22:10:19 +05:30
v-bind="$attrs"
@click="triggerEvent"
>
<template v-for="(item, itemIndex) in actionItems">
<gl-dropdown-item
:key="item.eventName"
:active="selectedItem === item"
active-class="is-active"
2020-03-09 13:42:32 +05:30
@click="changeSelectedItem(item)"
2019-12-26 22:10:19 +05:30
>
<strong>{{ item.title }}</strong>
<div>{{ item.description }}</div>
</gl-dropdown-item>
<gl-dropdown-divider
v-if="itemIndex < actionItems.length - 1"
:key="`${item.eventName}-divider`"
/>
</template>
</gl-dropdown>
</template>