summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pipeline_wizard/components/widgets/text.vue
blob: 26235b20ce97d54a3c2a2238717e43dd19ba4fdb (plain)
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
<script>
import { GlFormGroup, GlFormInput } from '@gitlab/ui';
import { uniqueId } from 'lodash';
import { s__ } from '~/locale';

const VALIDATION_STATE = {
  NO_VALIDATION: null,
  INVALID: false,
  VALID: true,
};

export default {
  name: 'TextWidget',
  components: {
    GlFormGroup,
    GlFormInput,
  },
  props: {
    label: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      required: false,
      default: null,
    },
    placeholder: {
      type: String,
      required: false,
      default: null,
    },
    invalidFeedback: {
      type: String,
      required: false,
      default: s__('PipelineWizardInputValidation|This value is not valid'),
    },
    id: {
      type: String,
      required: false,
      default: () => uniqueId('textWidget-'),
    },
    pattern: {
      type: String,
      required: false,
      default: null,
    },
    validate: {
      type: Boolean,
      required: false,
      default: false,
    },
    required: {
      type: Boolean,
      required: false,
      default: false,
    },
    default: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      touched: false,
      value: this.default,
    };
  },
  computed: {
    validationState() {
      if (!this.showValidationState) return VALIDATION_STATE.NO_VALIDATION;
      if (this.isRequiredButEmpty) return VALIDATION_STATE.INVALID;
      return this.needsValidationAndPasses ? VALIDATION_STATE.VALID : VALIDATION_STATE.INVALID;
    },
    showValidationState() {
      return this.touched || this.validate;
    },
    isRequiredButEmpty() {
      return this.required && !this.value;
    },
    needsValidationAndPasses() {
      return !this.pattern || new RegExp(this.pattern).test(this.value);
    },
    invalidFeedbackMessage() {
      return this.isRequiredButEmpty
        ? s__('PipelineWizardInputValidation|This field is required')
        : this.invalidFeedback;
    },
  },
  watch: {
    validationState(v) {
      this.$emit('update:valid', v);
    },
    value(v) {
      this.$emit('input', v.trim());
    },
  },
  created() {
    if (this.default) {
      this.$emit('input', this.value);
    }
  },
};
</script>

<template>
  <div data-testid="text-widget">
    <gl-form-group
      :description="description"
      :invalid-feedback="invalidFeedbackMessage"
      :label="label"
      :label-for="id"
      :state="validationState"
    >
      <gl-form-input
        :id="id"
        v-model="value"
        :placeholder="placeholder"
        :state="validationState"
        type="text"
        @blur="touched = true"
      />
    </gl-form-group>
  </div>
</template>