Browse Source

refactor: components

Muhammad Iqbal Afandi 3 years ago
parent
commit
8084ad3236

+ 27
- 41
resources/js/components/AppAutoComplete.vue View File

@@ -1,31 +1,16 @@
1 1
 <script setup>
2
-import { computed } from 'vue'
2
+import { computed, onMounted } from 'vue'
3 3
 import { Inertia } from '@inertiajs/inertia'
4 4
 
5 5
 const props = defineProps({
6 6
   label: {
7 7
     type: String,
8
-    required: true,
9
-  },
10
-  labelClass: {
11
-    type: String,
12
-  },
13
-  disabled: {
14
-    type: Boolean,
15
-    default: false,
16
-  },
17
-  placeholder: {
18
-    type: String,
19
-    required: true,
8
+    required: false,
20 9
   },
21 10
   error: {
22 11
     type: String,
23 12
     default: null,
24 13
   },
25
-  field: {
26
-    type: String,
27
-    required: true,
28
-  },
29 14
   empty: {
30 15
     type: Boolean,
31 16
     default: false,
@@ -34,55 +19,56 @@ const props = defineProps({
34 19
     type: String,
35 20
     required: true,
36 21
   },
37
-  suggestions: {
38
-    type: Array,
39
-    required: true,
40
-  },
41
-  modelValue: null,
42 22
 })
43 23
 
44 24
 const emit = defineEmits(['update:modelValue'])
45 25
 
46 26
 const isError = computed(() => (props.error ? true : false))
47 27
 
48
-const forLabel = computed(() => props.label.toLowerCase().replace(/\s+/g, '-'))
49
-
50 28
 const ariaDescribedbyLabel = computed(
51
-  () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
29
+  () => props.label?.toLowerCase().replace(/\s+/g, '-') + '-error'
52 30
 )
53 31
 
32
+let param = props.refreshData.slice(0, -1).replace('-', '_')
33
+
34
+const removeParams = (...params) => {
35
+  const urlParams = new URLSearchParams(location.search)
36
+
37
+  params.forEach((value) => urlParams.delete(value))
38
+
39
+  history.replaceState({}, '', `${location.pathname}?${urlParams}`)
40
+}
41
+
42
+const onInput = (event) => {
43
+  if (event.target.value === '') {
44
+    removeParams(param)
45
+  }
46
+
47
+  emit('update:modelValue', event.target.value)
48
+}
49
+
54 50
 const onComplete = (event) => {
55 51
   Inertia.reload({
56 52
     data: {
57
-      [props.refreshData.slice(0, -1)]: event.query,
53
+      [param]: event.query,
58 54
     },
59 55
     only: [props.refreshData],
60 56
   })
61 57
 }
62
-
63
-const onSelect = (event) => {
64
-  emit('update:modelValue', event.value)
65
-}
66 58
 </script>
67 59
 
68 60
 <template>
69 61
   <div class="field">
70
-    <label :for="forLabel" :class="labelClass">{{ label }}</label>
62
+    <label>{{ label }}</label>
71 63
 
72 64
     <AutoComplete
73 65
       forceSelection
74 66
       class="w-full"
75 67
       inputClass="w-full"
76
-      :model-value="modelValue"
77
-      :id="forLabel"
78 68
       :class="{ 'p-invalid': isError }"
79
-      :field="field"
80
-      :placeholder="placeholder"
81
-      :suggestions="suggestions"
82
-      :auto-highlight="true"
83
-      :disabled="disabled"
84
-      @input="$emit('update:modelValue', $event.target.value)"
85
-      @item-select="onSelect"
69
+      v-bind="$attrs"
70
+      @input="onInput"
71
+      @item-select="$emit('update:modelValue', $event.value)"
86 72
       @complete="onComplete"
87 73
     >
88 74
       <template #item="slotProps">
@@ -98,7 +84,7 @@ const onSelect = (event) => {
98 84
           'mb-2': empty,
99 85
           'p-error': isError,
100 86
         }"
101
-        :id="ariaDescribedbyLabel"
87
+        :aria-describedby="ariaDescribedbyLabel"
102 88
       >
103 89
         {{ error }}
104 90
       </small>

+ 1
- 0
resources/js/components/AppDateRangeFilter.vue View File

@@ -69,6 +69,7 @@ watch(dates, (value) => {
69 69
     class="w-full"
70 70
     selection-mode="range"
71 71
     date-format="dd/mm/yy"
72
+    :placeholder="$attrs.placeholder"
72 73
     :manual-input="false"
73 74
     v-model="dates"
74 75
   />

+ 6
- 69
resources/js/components/AppDropdown.vue View File

@@ -6,100 +6,37 @@ const props = defineProps({
6 6
     type: String,
7 7
     required: false,
8 8
   },
9
-  labelClass: {
10
-    type: String,
11
-  },
12
-  disabled: {
13
-    type: Boolean,
14
-    default: false,
15
-  },
16
-  placeholder: {
17
-    type: String,
18
-    required: true,
19
-  },
20 9
   error: {
21 10
     type: String,
22 11
     default: null,
23 12
   },
24
-  optionLabel: {
25
-    type: String,
26
-    default: 'label',
27
-  },
28
-  optionValue: {
29
-    type: String,
30
-    default: 'value',
31
-  },
32
-  optionDisabled: {
33
-    type: String,
34
-    default: 'disabled',
35
-  },
36
-  optionGroupChildren: {
37
-    type: String,
38
-    default: null,
39
-  },
40
-  optionGroupLabel: {
41
-    type: String,
42
-    default: null,
43
-  },
44
-  options: {
45
-    type: Array,
46
-    required: true,
47
-  },
48
-  modelValue: null,
49 13
 })
50 14
 
51 15
 const isError = computed(() => (props.error ? true : false))
52 16
 
53
-const forLabel = computed(() =>
54
-  props.label ? props.label.toLowerCase().replace(/\s+/g, '-') : null
55
-)
56
-
57 17
 const ariaDescribedbyLabel = computed(
58 18
   () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
59 19
 )
60
-
61
-const selectedDropdownLabel = (value) => {
62
-  const result = props.options.find((option) => {
63
-    if (option !== null) {
64
-      return option[props.optionValue] == value
65
-    }
66
-  })
67
-
68
-  if (result) {
69
-    return result[props.optionLabel]
70
-  }
71
-}
72 20
 </script>
73 21
 
74 22
 <template>
75 23
   <div class="field">
76
-    <label v-if="label" :for="forLabel" :class="labelClass">{{ label }}</label>
24
+    <label v-if="label">{{ label }}</label>
77 25
 
78 26
     <Dropdown
79 27
       class="w-full"
28
+      option-label="label"
29
+      option-value="value"
30
+      option-disabled="disabled"
80 31
       :class="{ 'p-invalid': isError }"
81
-      :id="forLabel"
82
-      :option-disabled="optionDisabled"
83
-      :option-group-children="optionGroupChildren"
84
-      :option-group-label="optionGroupLabel"
85
-      :option-label="optionLabel"
86
-      :option-value="optionValue"
87
-      :placeholder="placeholder"
88
-      :options="options"
89
-      :model-value="modelValue"
90
-      :disabled="disabled"
32
+      v-bind="$attrs"
91 33
       @change="$emit('update:modelValue', $event.value)"
92 34
     >
93
-      <template #value="slotProps">
94
-        <div v-if="slotProps.value">
95
-          {{ selectedDropdownLabel(slotProps.value) }}
96
-        </div>
97
-      </template>
98 35
     </Dropdown>
99 36
 
100 37
     <small
101 38
       v-if="error"
102
-      :id="ariaDescribedbyLabel"
39
+      :aria-describedby="ariaDescribedbyLabel"
103 40
       :class="{ 'p-error': isError }"
104 41
     >
105 42
       {{ error }}

+ 3
- 23
resources/js/components/AppEditor.vue View File

@@ -6,31 +6,14 @@ const props = defineProps({
6 6
     type: String,
7 7
     required: true,
8 8
   },
9
-  labelClass: {
10
-    type: String,
11
-  },
12
-  readOnly: {
13
-    type: Boolean,
14
-    required: false,
15
-  },
16
-  placeholder: {
17
-    type: String,
18
-    required: true,
19
-  },
20 9
   error: {
21 10
     type: String,
22 11
     default: null,
23 12
   },
24
-  editorStyle: null,
25
-  modelValue: null,
26 13
 })
27 14
 
28 15
 const isError = computed(() => (props.error ? true : false))
29 16
 
30
-const forLabel = computed(() =>
31
-  props.label ? props.label.toLowerCase().replace(/\s+/g, '-') : null
32
-)
33
-
34 17
 const ariaDescribedbyLabel = computed(
35 18
   () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
36 19
 )
@@ -38,13 +21,10 @@ const ariaDescribedbyLabel = computed(
38 21
 
39 22
 <template>
40 23
   <div class="field">
41
-    <label v-if="label" :for="forLabel" :class="labelClass">{{ label }}</label>
24
+    <label v-if="label">{{ label }}</label>
42 25
 
43 26
     <Editor
44
-      :read-only="readOnly"
45
-      :model-value="modelValue"
46
-      :editor-style="editorStyle"
47
-      :placeholder="placeholder"
27
+      v-bind="$attrs"
48 28
       @text-change="$emit('update:modelValue', $event.htmlValue)"
49 29
     >
50 30
       <template #toolbar>
@@ -54,7 +34,7 @@ const ariaDescribedbyLabel = computed(
54 34
 
55 35
     <small
56 36
       v-if="error"
57
-      :id="ariaDescribedbyLabel"
37
+      :aria-describedby="ariaDescribedbyLabel"
58 38
       :class="{ 'p-error': isError }"
59 39
     >
60 40
       {{ error }}

+ 3
- 75
resources/js/components/AppInputNumber.vue View File

@@ -6,72 +6,14 @@ const props = defineProps({
6 6
     type: String,
7 7
     required: true,
8 8
   },
9
-  labelClass: {
10
-    type: String,
11
-  },
12
-  disabled: {
13
-    type: Boolean,
14
-    default: false,
15
-  },
16
-  placeholder: {
17
-    type: String,
18
-    required: true,
19
-  },
20 9
   error: {
21 10
     type: String,
22 11
     default: null,
23 12
   },
24
-  useGrouping: {
25
-    type: Boolean,
26
-    default: true,
27
-  },
28
-  mode: {
29
-    type: String,
30
-    default: 'decimal',
31
-  },
32
-  currency: {
33
-    type: String,
34
-    default: undefined,
35
-  },
36
-  locale: {
37
-    type: String,
38
-    default: undefined,
39
-  },
40
-  currencyDisplay: {
41
-    type: String,
42
-    default: undefined,
43
-  },
44
-  showButtons: {
45
-    type: Boolean,
46
-    default: false,
47
-  },
48
-  min: {
49
-    type: Number,
50
-    default: null,
51
-  },
52
-  max: {
53
-    type: Number,
54
-    default: null,
55
-  },
56
-  step: {
57
-    type: Number,
58
-    default: 1,
59
-  },
60
-  prefix: {
61
-    type: String,
62
-    default: null,
63
-  },
64
-  suffix: {
65
-    type: String,
66
-    default: null,
67
-  },
68
-  modelValue: null,
69 13
 })
70 14
 
71 15
 const isError = computed(() => (props.error ? true : false))
72 16
 
73
-const forLabel = computed(() => props.label.toLowerCase().replace(/\s+/g, '-'))
74
-
75 17
 const ariaDescribedbyLabel = computed(
76 18
   () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
77 19
 )
@@ -79,33 +21,19 @@ const ariaDescribedbyLabel = computed(
79 21
 
80 22
 <template>
81 23
   <div class="field">
82
-    <label :for="forLabel" :class="labelClass">{{ label }}</label>
24
+    <label>{{ label }}</label>
83 25
 
84 26
     <InputNumber
85 27
       class="w-full"
86 28
       input-class="w-full"
87
-      :currency="currency"
88
-      :currency-display="currencyDisplay"
89
-      :locale="locale"
90 29
       :class="{ 'p-invalid': isError }"
91
-      :id="forLabel"
92
-      :placeholder="placeholder"
93
-      :model-value="modelValue"
94
-      :disabled="disabled"
95
-      :prefix="prefix"
96
-      :suffix="suffix"
97
-      :step="step"
98
-      :min="min"
99
-      :max="max"
100
-      :mode="mode"
101
-      :use-grouping="useGrouping"
102
-      :show-buttons="showButtons"
30
+      v-bind="$attrs"
103 31
       @input="$emit('update:modelValue', $event.value)"
104 32
     />
105 33
 
106 34
     <small
107 35
       v-if="error"
108
-      :id="ariaDescribedbyLabel"
36
+      :aria-describedby="ariaDescribedbyLabel"
109 37
       :class="{ 'p-error': isError }"
110 38
     >
111 39
       {{ error }}

+ 3
- 25
resources/js/components/AppInputText.vue View File

@@ -6,32 +6,14 @@ const props = defineProps({
6 6
     type: String,
7 7
     required: true,
8 8
   },
9
-  labelClass: {
10
-    type: String,
11
-  },
12
-  disabled: {
13
-    type: Boolean,
14
-    default: false,
15
-  },
16
-  placeholder: {
17
-    type: String,
18
-    required: true,
19
-  },
20
-  type: {
21
-    type: String,
22
-    default: 'text',
23
-  },
24 9
   error: {
25 10
     type: String,
26 11
     default: null,
27 12
   },
28
-  modelValue: null,
29 13
 })
30 14
 
31 15
 const isError = computed(() => (props.error ? true : false))
32 16
 
33
-const forLabel = computed(() => props.label.toLowerCase().replace(/\s+/g, '-'))
34
-
35 17
 const ariaDescribedbyLabel = computed(
36 18
   () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
37 19
 )
@@ -39,22 +21,18 @@ const ariaDescribedbyLabel = computed(
39 21
 
40 22
 <template>
41 23
   <div class="field">
42
-    <label :for="forLabel" :class="labelClass">{{ label }}</label>
24
+    <label>{{ label }}</label>
43 25
 
44 26
     <InputText
45 27
       class="w-full"
46
-      :type="type"
47 28
       :class="{ 'p-invalid': isError }"
48
-      :id="forLabel"
49
-      :model-value="modelValue"
50
-      :placeholder="placeholder"
51
-      :disabled="disabled"
29
+      v-bind="$attrs"
52 30
       @input="$emit('update:modelValue', $event.target.value)"
53 31
     />
54 32
 
55 33
     <small
56 34
       v-if="error"
57
-      :id="ariaDescribedbyLabel"
35
+      :aria-describedby="ariaDescribedbyLabel"
58 36
       :class="{ 'p-error': isError }"
59 37
     >
60 38
       {{ error }}

+ 3
- 41
resources/js/components/AppPassword.vue View File

@@ -6,44 +6,14 @@ const props = defineProps({
6 6
     type: String,
7 7
     required: true,
8 8
   },
9
-  labelClass: {
10
-    type: String,
11
-  },
12
-  disabled: {
13
-    type: Boolean,
14
-    default: false,
15
-  },
16
-  placeholder: {
17
-    type: String,
18
-    required: true,
19
-  },
20
-  promptLabel: {
21
-    type: String,
22
-    default: 'Masukan kata sandi',
23
-  },
24
-  weakLabel: {
25
-    type: String,
26
-    default: 'Ah Lemah',
27
-  },
28
-  mediumLabel: {
29
-    type: String,
30
-    default: 'Lumayan',
31
-  },
32
-  strongLabel: {
33
-    type: String,
34
-    default: 'Wow Kuat',
35
-  },
36 9
   error: {
37 10
     type: String,
38 11
     default: null,
39 12
   },
40
-  modelValue: null,
41 13
 })
42 14
 
43 15
 const isError = computed(() => (props.error ? true : false))
44 16
 
45
-const forLabel = computed(() => props.label.toLowerCase().replace(/\s+/g, '-'))
46
-
47 17
 const ariaDescribedbyLabel = computed(
48 18
   () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
49 19
 )
@@ -51,27 +21,19 @@ const ariaDescribedbyLabel = computed(
51 21
 
52 22
 <template>
53 23
   <div class="field">
54
-    <label :for="forLabel" :class="labelClass">{{ label }}</label>
24
+    <label>{{ label }}</label>
55 25
 
56 26
     <Password
57 27
       class="w-full"
58 28
       input-class="w-full"
59 29
       :class="{ 'p-invalid': isError }"
60
-      :promptLabel="promptLabel"
61
-      :weakLabel="weakLabel"
62
-      :mediumLabel="mediumLabel"
63
-      :strongLabel="strongLabel"
64
-      :disabled="disabled"
65
-      :id="forLabel"
66
-      :placeholder="placeholder"
67
-      :toggleMask="true"
68
-      :model-value="modelValue"
30
+      v-bind="$attrs"
69 31
       @input="$emit('update:modelValue', $event.target.value)"
70 32
     />
71 33
 
72 34
     <small
73 35
       v-if="error"
74
-      :id="ariaDescribedbyLabel"
36
+      :aria-describedby="ariaDescribedbyLabel"
75 37
       :class="{ 'p-error': isError }"
76 38
     >
77 39
       {{ error }}

+ 1
- 1
resources/js/components/AppResetFilter.vue View File

@@ -14,5 +14,5 @@ const reset = () => {
14 14
 </script>
15 15
 
16 16
 <template>
17
-  <Button label="reset" class="p-button-link" @click="reset()" />
17
+  <Button label="reset" class="p-button-link" @click="reset" />
18 18
 </template>

+ 3
- 31
resources/js/components/AppTextArea.vue View File

@@ -6,36 +6,14 @@ const props = defineProps({
6 6
     type: String,
7 7
     required: true,
8 8
   },
9
-  labelClass: {
10
-    type: String,
11
-  },
12
-  disabled: {
13
-    type: Boolean,
14
-    default: false,
15
-  },
16
-  placeholder: {
17
-    type: String,
18
-    required: true,
19
-  },
20 9
   error: {
21 10
     type: String,
22 11
     default: null,
23 12
   },
24
-  rows: {
25
-    type: Number,
26
-    default: 5,
27
-  },
28
-  cols: {
29
-    type: Number,
30
-    default: 30,
31
-  },
32
-  modelValue: null,
33 13
 })
34 14
 
35 15
 const isError = computed(() => (props.error ? true : false))
36 16
 
37
-const forLabel = computed(() => props.label.toLowerCase().replace(/\s+/g, '-'))
38
-
39 17
 const ariaDescribedbyLabel = computed(
40 18
   () => props.label.toLowerCase().replace(/\s+/g, '-') + '-error'
41 19
 )
@@ -43,24 +21,18 @@ const ariaDescribedbyLabel = computed(
43 21
 
44 22
 <template>
45 23
   <div class="field">
46
-    <label :for="forLabel" :class="labelClass">{{ label }}</label>
24
+    <label>{{ label }}</label>
47 25
 
48 26
     <Textarea
49 27
       class="w-full"
50 28
       :class="{ 'p-invalid': isError }"
51
-      :id="forLabel"
52
-      :model-value="modelValue"
53
-      :placeholder="placeholder"
54
-      :disabled="disabled"
55
-      :auto-resize="true"
56
-      :rows="rows"
57
-      :cols="cols"
29
+      v-bind="$attrs"
58 30
       @input="$emit('update:modelValue', $event.target.value)"
59 31
     />
60 32
 
61 33
     <small
62 34
       v-if="error"
63
-      :id="ariaDescribedbyLabel"
35
+      :aria-describedby="ariaDescribedbyLabel"
64 36
       :class="{ 'p-error': isError }"
65 37
     >
66 38
       {{ error }}