summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/monitoring/components/date_time_picker/date_time_picker_input.vue
blob: 0388a6190d915064172093d6154cc4fec3c0cd6a (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
<script>
import _ from 'underscore';
import { s__, sprintf } from '~/locale';
import { GlFormGroup, GlFormInput } from '@gitlab/ui';
import { dateFormats } from '~/monitoring/constants';

const inputGroupText = {
  invalidFeedback: sprintf(s__('Format: %{dateFormat}'), {
    dateFormat: dateFormats.dateTimePicker.format,
  }),
  placeholder: dateFormats.dateTimePicker.format,
};

export default {
  components: {
    GlFormGroup,
    GlFormInput,
  },
  props: {
    state: {
      default: null,
      required: true,
      validator: prop => typeof prop === 'boolean' || prop === null,
    },
    value: {
      default: null,
      required: false,
      validator: prop => typeof prop === 'string' || prop === null,
    },
    label: {
      type: String,
      default: '',
      required: true,
    },
    id: {
      type: String,
      required: false,
      default: () => _.uniqueId('dateTimePicker_'),
    },
  },
  data() {
    return {
      inputGroupText,
    };
  },
  computed: {
    invalidFeedback() {
      return this.state ? '' : this.inputGroupText.invalidFeedback;
    },
    inputState() {
      // When the state is valid we want to show no
      // green outline. Hence passing null and not true.
      if (this.state === true) {
        return null;
      }
      return this.state;
    },
  },
  methods: {
    onInputBlur(e) {
      this.$emit('input', e.target.value.trim() || null);
    },
  },
};
</script>

<template>
  <gl-form-group :label="label" label-size="sm" :label-for="id" :invalid-feedback="invalidFeedback">
    <gl-form-input
      :id="id"
      :value="value"
      :state="inputState"
      :placeholder="inputGroupText.placeholder"
      @blur="onInputBlur"
    />
  </gl-form-group>
</template>