debian-mirror-gitlab/app/assets/javascripts/ci_variable_list/components/ci_environments_dropdown.vue

84 lines
2.2 KiB
Vue
Raw Normal View History

2020-04-22 19:07:51 +05:30
<script>
2021-01-03 14:25:43 +05:30
import { GlDropdown, GlDropdownItem, GlDropdownDivider, GlSearchBoxByType } from '@gitlab/ui';
2020-04-22 19:07:51 +05:30
import { mapGetters } from 'vuex';
2020-10-24 23:57:45 +05:30
import { __, sprintf } from '~/locale';
2020-04-22 19:07:51 +05:30
export default {
name: 'CiEnvironmentsDropdown',
components: {
2021-01-03 14:25:43 +05:30
GlDropdown,
GlDropdownItem,
GlDropdownDivider,
2020-04-22 19:07:51 +05:30
GlSearchBoxByType,
},
props: {
value: {
type: String,
required: false,
default: '',
},
},
data() {
return {
searchTerm: this.value || '',
};
},
computed: {
...mapGetters(['joinedEnvironments']),
composedCreateButtonLabel() {
return sprintf(__('Create wildcard: %{searchTerm}'), { searchTerm: this.searchTerm });
},
shouldRenderCreateButton() {
return this.searchTerm && !this.joinedEnvironments.includes(this.searchTerm);
},
filteredResults() {
const lowerCasedSearchTerm = this.searchTerm.toLowerCase();
return this.joinedEnvironments.filter(resultString =>
resultString.toLowerCase().includes(lowerCasedSearchTerm),
);
},
},
watch: {
value(newVal) {
this.searchTerm = newVal;
},
},
methods: {
selectEnvironment(selected) {
this.$emit('selectEnvironment', selected);
this.searchTerm = '';
},
createClicked() {
this.$emit('createClicked', this.searchTerm);
this.searchTerm = '';
},
isSelected(env) {
return this.value === env;
},
},
};
</script>
<template>
2021-01-03 14:25:43 +05:30
<gl-dropdown :text="value">
2021-01-29 00:20:46 +05:30
<gl-search-box-by-type v-model.trim="searchTerm" data-testid="ci-environment-search" />
2021-01-03 14:25:43 +05:30
<gl-dropdown-item
2020-04-22 19:07:51 +05:30
v-for="environment in filteredResults"
:key="environment"
2021-01-03 14:25:43 +05:30
:is-checked="isSelected(environment)"
is-check-item
2020-04-22 19:07:51 +05:30
@click="selectEnvironment(environment)"
>
{{ environment }}
2021-01-03 14:25:43 +05:30
</gl-dropdown-item>
<gl-dropdown-item v-if="!filteredResults.length" ref="noMatchingResults">{{
2020-04-22 19:07:51 +05:30
__('No matching results')
2021-01-03 14:25:43 +05:30
}}</gl-dropdown-item>
2020-04-22 19:07:51 +05:30
<template v-if="shouldRenderCreateButton">
2021-01-03 14:25:43 +05:30
<gl-dropdown-divider />
2021-01-29 00:20:46 +05:30
<gl-dropdown-item data-testid="create-wildcard-button" @click="createClicked">
2020-04-22 19:07:51 +05:30
{{ composedCreateButtonLabel }}
2021-01-03 14:25:43 +05:30
</gl-dropdown-item>
2020-04-22 19:07:51 +05:30
</template>
2021-01-03 14:25:43 +05:30
</gl-dropdown>
2020-04-22 19:07:51 +05:30
</template>