summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/rich_content_editor/modals/add_image/upload_image_tab.vue
blob: 739f8b502c978fd3f28571f1215045d9fff25879 (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
<script>
import { __ } from '~/locale';
import { GlFormGroup } from '@gitlab/ui';
import { MAX_FILE_SIZE } from '../../constants';

export default {
  components: {
    GlFormGroup,
  },
  data() {
    return {
      file: null,
      fileError: null,
    };
  },
  fileLabel: __('Select file'),
  methods: {
    onInput(event) {
      [this.file] = event.target.files;

      this.validateFile();

      if (!this.fileError) {
        this.$emit('input', this.file);
      }
    },
    validateFile() {
      this.fileError = null;

      if (!this.file) {
        this.fileError = __('Please choose a file');
      } else if (this.file.size > MAX_FILE_SIZE) {
        this.fileError = __('Maximum file size is 2MB. Please select a smaller file.');
      }
    },
  },
};
</script>
<template>
  <gl-form-group
    class="gl-mt-5 gl-mb-3"
    :label="$options.fileLabel"
    label-for="file-input"
    :state="!Boolean(fileError)"
    :invalid-feedback="fileError"
  >
    <input
      id="file-input"
      ref="fileInput"
      class="gl-mt-3 gl-mb-2"
      type="file"
      accept="image/*"
      @input="onInput"
    />
  </gl-form-group>
</template>