summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/pikaday.vue
blob: bfeece12077f1baa1106023584caa37dce117643 (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
<script>
  import Pikaday from 'pikaday';
  import { parsePikadayDate, pikadayToString } from '../../lib/utils/datefix';

  export default {
    name: 'DatePicker',
    props: {
      label: {
        type: String,
        required: false,
        default: 'Date picker',
      },
      selectedDate: {
        type: Date,
        required: false,
        default: null,
      },
      minDate: {
        type: Date,
        required: false,
        default: null,
      },
      maxDate: {
        type: Date,
        required: false,
        default: null,
      },
    },
    mounted() {
      this.calendar = new Pikaday({
        field: this.$el.querySelector('.dropdown-menu-toggle'),
        theme: 'gitlab-theme animate-picker',
        format: 'yyyy-mm-dd',
        container: this.$el,
        defaultDate: this.selectedDate,
        setDefaultDate: !!this.selectedDate,
        minDate: this.minDate,
        maxDate: this.maxDate,
        parse: dateString => parsePikadayDate(dateString),
        toString: date => pikadayToString(date),
        onSelect: this.selected.bind(this),
        onClose: this.toggled.bind(this),
      });

      this.$el.append(this.calendar.el);
      this.calendar.show();
    },
    beforeDestroy() {
      this.calendar.destroy();
    },
    methods: {
      selected(dateText) {
        this.$emit('newDateSelected', this.calendar.toString(dateText));
      },
      toggled() {
        this.$emit('hidePicker');
      },
    },
  };
</script>

<template>
  <div class="pikaday-container">
    <div class="dropdown open">
      <button
        type="button"
        class="dropdown-menu-toggle"
        data-toggle="dropdown"
        @click="toggled"
      >
        <span class="dropdown-toggle-text">
          {{ label }}
        </span>
        <i
          class="fa fa-chevron-down"
          aria-hidden="true"
        >
        </i>
      </button>
    </div>
  </div>
</template>