All files / ee/app/assets/javascripts/groups/settings/components comma_separated_list_token_selector.vue

100% Statements 25/25
100% Branches 11/11
100% Functions 12/12
100% Lines 25/25

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 145 146 147 148 149  1x                                                                                               18x               27x     27x 12x     15x 2x     13x         7x     7x                 18x 18x   18x 14x     4x             9x 7x     7x 7x             9x 8x       13x 13x 13x     1x                                                      
<script>
import { GlTokenSelector } from '@gitlab/ui';
import { isEmpty } from 'lodash';
 
export default {
  name: 'CommaSeparatedListTokenSelector',
  hiddenInput: null,
  components: { GlTokenSelector },
  props: {
    hiddenInputId: {
      type: String,
      required: true,
    },
    ariaLabelledby: {
      type: String,
      required: true,
    },
    placeholder: {
      type: String,
      required: false,
      default: null,
    },
    regexValidator: {
      type: RegExp,
      required: false,
      default: null,
    },
    disallowedValues: {
      type: Array,
      required: false,
      default: () => [],
    },
    regexErrorMessage: {
      type: String,
      required: false,
      default: '',
    },
    disallowedValueErrorMessage: {
      type: String,
      required: false,
      default: '',
    },
    customErrorMessage: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      selectedTokens: [],
      textInputValue: '',
      hideErrorMessage: true,
    };
  },
  computed: {
    tokenIsValid() {
      return this.computedErrorMessage === '';
    },
    computedErrorMessage() {
      if (this.regexValidator !== null && this.textInputValue.match(this.regexValidator) === null) {
        return this.regexErrorMessage;
      }
 
      if (!isEmpty(this.disallowedValues) && this.disallowedValues.includes(this.textInputValue)) {
        return this.disallowedValueErrorMessage;
      }
 
      return this.customErrorMessage;
    },
  },
  watch: {
    selectedTokens(newValue) {
      this.$options.hiddenInput.value = newValue.map((token) => token.name).join(',');
 
      // Dispatch `input` event so form submit button becomes active
      this.$options.hiddenInput.dispatchEvent(
        new Event('input', {
          bubbles: true,
          cancelable: true,
        }),
      );
    },
  },
  mounted() {
    const hiddenInput = document.getElementById(this.hiddenInputId);
    this.$options.hiddenInput = hiddenInput;
 
    if (hiddenInput.value === '') {
      return;
    }
 
    this.selectedTokens = hiddenInput.value.split(/,\s*/).map((token, index) => ({
      id: index,
      name: token,
    }));
  },
  methods: {
    handleEnter(event) {
      if (this.textInputValue !== '' && !this.tokenIsValid) {
        this.hideErrorMessage = false;
 
        // Trigger a focus event on the token selector to explicitly open the dropdown and display the error message
        this.$nextTick(() => {
          this.$refs.tokenSelector.$el
            .querySelector('input[type="text"]')
            .dispatchEvent(new Event('focus'));
        });
      }
 
      // Prevent form from submitting when adding a token
      if (event.target.value !== '') {
        event.preventDefault();
      }
    },
    handleTextInput(value) {
      this.hideErrorMessage = true;
      this.textInputValue = value;
      this.$emit('text-input', value);
    },
    handleBlur() {
      this.hideErrorMessage = true;
    },
  },
};
</script>
 
<template>
  <gl-token-selector
    ref="tokenSelector"
    v-model="selectedTokens"
    container-class="gl-h-auto!"
    :allow-user-defined-tokens="tokenIsValid"
    :hide-dropdown-with-no-items="hideErrorMessage"
    :aria-labelledby="ariaLabelledby"
    :placeholder="placeholder"
    @keydown.enter="handleEnter"
    @text-input="handleTextInput"
    @blur="handleBlur"
  >
    <template #user-defined-token-content="{ inputText }">
      <slot name="user-defined-token-content" :input-text="inputText"></slot>
    </template>
    <template #no-results-content>
      <span class="gl-text-red-500">{{ computedErrorMessage }}</span>
    </template>
  </gl-token-selector>
</template>