All files / app/assets/javascripts/releases/components tag_field_new.vue

78.26% Statements 18/23
54.54% Branches 6/11
71.42% Functions 10/14
78.26% Lines 18/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144    3x                                     13x           28x     19x     26x     20x                   13x                       1x 1x     7x   7x 6x 6x 6x   1x     7x     3x           7x                                                                                                                            
<script>
import { GlButton, GlTruncate, GlIcon, GlFormGroup, GlPopover } from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapState, mapActions, mapGetters } from 'vuex';
import { __, s__ } from '~/locale';
import { ESC_KEY } from '~/lib/utils/keys';
 
import TagSearch from './tag_search.vue';
import TagCreate from './tag_create.vue';
 
export default {
  components: {
    GlTruncate,
    GlButton,
    GlIcon,
    GlFormGroup,
    GlPopover,
    TagSearch,
    TagCreate,
  },
  data() {
    return { id: 'release-tag-name', newTagName: '', show: false, isInputDirty: false };
  },
  computed: {
    ...mapState('editNew', ['release', 'showCreateFrom']),
    ...mapGetters('editNew', ['validationErrors', 'isSearching', 'isCreating']),
    title() {
      return this.isCreating ? this.$options.i18n.createTitle : this.$options.i18n.selectTitle;
    },
    showTagNameValidationError() {
      return this.isInputDirty && !this.validationErrors.tagNameValidation.isValid;
    },
    tagFeedback() {
      return this.validationErrors.tagNameValidation.validationErrors[0];
    },
    buttonText() {
      return this.release?.tagName || s__('Release|Search or create tag name');
    },
    buttonVariant() {
      return this.showTagNameValidationError ? 'danger' : 'default';
    },
    createText() {
      return this.newTagName ? this.$options.i18n.createTag : this.$options.i18n.typeNew;
    },
  },
  mounted() {
    this.newTagName = this.release?.tagName || '';
  },
  methods: {
    ...mapActions('editNew', [
      'setSearching',
      'setCreating',
      'setNewTag',
      'setExistingTag',
      'updateReleaseTagName',
      'fetchTagNotes',
    ]),
    startCreate(query) {
      this.newTagName = query;
      this.setCreating();
    },
    selected(tag) {
      this.updateReleaseTagName(tag);
 
      if (this.isSearching) {
        this.fetchTagNotes(tag);
        this.setExistingTag();
        this.newTagName = '';
      } else {
        this.setNewTag();
      }
 
      this.hidePopover();
    },
    markInputAsDirty() {
      this.isInputDirty = true;
    },
    showPopover() {
      this.show = true;
    },
    hidePopover() {
      this.show = false;
      // gl-button doesn't expose focus method, but we can find button element by id
      document.getElementById(this.id)?.focus();
    },
    onPopoverKeyUp(e) {
      Iif (e.code === ESC_KEY) {
        this.hidePopover();
      }
    },
  },
  i18n: {
    selectTitle: __('Tags'),
    createTitle: s__('Release|Create tag'),
    label: __('Tag name'),
    required: __('(required)'),
    create: __('Create'),
    cancel: __('Cancel'),
  },
};
</script>
<template>
  <div class="row">
    <gl-form-group
      class="col-md-4 col-sm-10"
      :label="$options.i18n.label"
      :label-for="id"
      :optional-text="$options.i18n.required"
      :state="!showTagNameValidationError"
      :invalid-feedback="tagFeedback"
      optional
    >
      <gl-button :id="id" class="gl-w-30 gl-px-0!" @click="showPopover">
        <span class="gl-w-28 gl-display-flex gl-justify-content-space-between">
          <gl-truncate :text="buttonText" class="gl-max-w-26" />
          <gl-icon class="gl-button-icon gl-new-dropdown-chevron" name="chevron-down" />
        </span>
      </gl-button>
      <gl-popover
        :show="show"
        :target="id"
        :title="title"
        :css-classes="['gl-z-index-200', 'release-tag-selector']"
        placement="bottom"
        triggers="manual"
        container="content-body"
        show-close-button
        @close-button-clicked="hidePopover"
        @hide.once="markInputAsDirty"
      >
        <div class="gl-border-t-solid gl-border-t-1 gl-border-gray-200" @keyup="onPopoverKeyUp">
          <tag-create
            v-if="isCreating"
            v-model="newTagName"
            @create="selected(newTagName)"
            @cancel="setSearching"
          />
          <tag-search v-else v-model="newTagName" @create="startCreate" @select="selected" />
        </div>
      </gl-popover>
    </gl-form-group>
  </div>
</template>