debian-mirror-gitlab/app/assets/javascripts/ide/components/shared/tokened_input.vue

105 lines
2.3 KiB
Vue
Raw Normal View History

2018-11-18 11:00:15 +05:30
<script>
2020-11-24 15:15:51 +05:30
import { GlIcon } from '@gitlab/ui';
2018-11-18 11:00:15 +05:30
import { __ } from '~/locale';
export default {
components: {
2020-11-24 15:15:51 +05:30
GlIcon,
2018-11-18 11:00:15 +05:30
},
props: {
placeholder: {
type: String,
required: false,
default: __('Search'),
},
tokens: {
type: Array,
required: false,
default: () => [],
},
value: {
type: String,
required: false,
default: '',
},
},
data() {
return {
backspaceCount: 0,
};
},
computed: {
placeholderText() {
2018-12-13 13:39:08 +05:30
return this.tokens.length ? '' : this.placeholder;
2018-11-18 11:00:15 +05:30
},
},
watch: {
tokens() {
this.$refs.input.focus();
},
},
methods: {
onFocus() {
this.$emit('focus');
},
onBlur() {
this.$emit('blur');
},
onInput(evt) {
this.$emit('input', evt.target.value);
},
onBackspace() {
if (!this.value && this.tokens.length) {
this.backspaceCount += 1;
} else {
this.backspaceCount = 0;
return;
}
if (this.backspaceCount > 1) {
this.removeToken(this.tokens[this.tokens.length - 1]);
this.backspaceCount = 0;
}
},
removeToken(token) {
this.$emit('removeToken', token);
},
},
};
</script>
<template>
<div class="filtered-search-wrapper">
<div class="filtered-search-box">
<div class="tokens-container list-unstyled">
2019-02-15 15:39:39 +05:30
<div v-for="token in tokens" :key="token.label" class="filtered-search-token">
2018-11-18 11:00:15 +05:30
<button
class="selectable btn-blank"
type="button"
2019-03-02 22:35:43 +05:30
@click.stop="removeToken(token)"
@keyup.delete="removeToken(token)"
2018-11-18 11:00:15 +05:30
>
2019-02-15 15:39:39 +05:30
<div class="value-container rounded">
<div class="value">{{ token.label }}</div>
2020-11-24 15:15:51 +05:30
<div class="remove-token inverted"><gl-icon :size="10" name="close" /></div>
2018-11-18 11:00:15 +05:30
</div>
</button>
</div>
<div class="input-token">
<input
ref="input"
:placeholder="placeholderText"
:value="value"
type="search"
class="form-control filtered-search"
@input="onInput"
@focus="onFocus"
@blur="onBlur"
@keyup.delete="onBackspace"
/>
</div>
</div>
</div>
</div>
</template>