Skip to content

Commit 0284fe2

Browse files
committed
Add input prepend/append functionality
1 parent 40a4d37 commit 0284fe2

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

Diff for: backend/src/components/core/CustomInput.vue

+58-23
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
11
<template>
22
<div>
33
<label class="sr-only">{{ label }}</label>
4-
<template v-if="type === 'textarea'">
4+
<div class="mt-1 flex rounded-md shadow-sm">
5+
<span v-if="prepend"
6+
class="inline-flex items-center px-3 rounded-l-md border border-r-0 border-gray-300 bg-gray-50 text-gray-500 text-sm">
7+
{{ prepend }}
8+
</span>
9+
<template v-if="type === 'textarea'">
510
<textarea :name="name"
611
:required="required"
712
:value="props.modelValue"
813
@input="emit('update:modelValue', $event.target.value)"
9-
class="block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
14+
:class="inputClasses"
1015
:placeholder="label"></textarea>
11-
</template>
12-
<template v-else-if="type === 'file'">
13-
<input :type="type"
14-
:name="name"
15-
:required="required"
16-
:value="props.modelValue"
17-
@input="emit('change', $event.target.files[0])"
18-
class="block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
19-
:placeholder="label"/>
20-
</template>
21-
<template v-else>
22-
<input :type="type"
23-
:name="name"
24-
:required="required"
25-
:value="props.modelValue"
26-
@input="emit('update:modelValue', $event.target.value)"
27-
class="block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
28-
:placeholder="label"
29-
step="0.01"/>
30-
</template>
16+
</template>
17+
<template v-else-if="type === 'file'">
18+
<input :type="type"
19+
:name="name"
20+
:required="required"
21+
:value="props.modelValue"
22+
@input="emit('change', $event.target.files[0])"
23+
:class="inputClasses"
24+
:placeholder="label"/>
25+
</template>
26+
<template v-else>
27+
<input :type="type"
28+
:name="name"
29+
:required="required"
30+
:value="props.modelValue"
31+
@input="emit('update:modelValue', $event.target.value)"
32+
:class="inputClasses"
33+
:placeholder="label"
34+
step="0.01"/>
35+
</template>
36+
<span v-if="append"
37+
class="inline-flex items-center px-3 rounded-r-md border border-l-0 border-gray-300 bg-gray-50 text-gray-500 text-sm">
38+
{{ append }}
39+
</span>
40+
</div>
3141
</div>
3242
</template>
3343

3444
<script setup>
3545
46+
import {computed} from "vue";
47+
3648
const props = defineProps({
3749
modelValue: [String, Number, File],
3850
label: String,
@@ -41,7 +53,30 @@ const props = defineProps({
4153
default: 'text'
4254
},
4355
name: String,
44-
required: Boolean
56+
required: Boolean,
57+
prepend: {
58+
type: String,
59+
default: ''
60+
},
61+
append: {
62+
type: String,
63+
default: ''
64+
}
65+
})
66+
67+
const inputClasses = computed(() => {
68+
const cls = [
69+
`block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm`,
70+
];
71+
72+
if (props.append && !props.prepend) {
73+
cls.push(`rounded-l-md`)
74+
} else if (props.prepend && !props.append) {
75+
cls.push(`rounded-r-md`)
76+
} else if (!props.prepend && !props.append) {
77+
cls.push('rounded-md')
78+
}
79+
return cls.join(' ')
4580
})
4681
4782
const emit = defineEmits(['update:modelValue', 'change'])

Diff for: backend/src/views/Products/ProductModal.vue

+6-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<CustomInput class="mb-2" v-model="product.title" label="Product Title"/>
4848
<CustomInput type="file" class="mb-2" label="Product Image" @change="file => product.image = file"/>
4949
<CustomInput type="textarea" class="mb-2" v-model="product.description" label="Description"/>
50-
<CustomInput type="number" class="mb-2" v-model="product.price" label="Price"/>
50+
<CustomInput type="number" class="mb-2" v-model="product.price" label="Price" prepend="$"/>
5151
</div>
5252
<footer class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
5353
<button type="submit"
@@ -56,7 +56,8 @@
5656
</button>
5757
<button type="button"
5858
class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm"
59-
@click="show = false" ref="cancelButtonRef">Cancel
59+
@click="closeModal" ref="cancelButtonRef">
60+
Cancel
6061
</button>
6162
</footer>
6263
</form>
@@ -89,13 +90,12 @@ const loading = ref(false)
8990
const props = defineProps({
9091
modelValue: Boolean,
9192
product: {
92-
required: false,
93+
required: true,
9394
type: Object,
94-
default: {}
9595
}
9696
})
9797

98-
const emit = defineEmits(['update:modelValue'])
98+
const emit = defineEmits(['update:modelValue', 'close'])
9999

100100
const show = computed({
101101
get: () => props.modelValue,
@@ -114,6 +114,7 @@ onUpdated(() => {
114114

115115
function closeModal() {
116116
show.value = false
117+
emit('close')
117118
}
118119

119120
function onSubmit() {

Diff for: backend/src/views/Products/ProductsTable.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
{{ product.title }}
6767
</td>
6868
<td class="border-b p-2">
69-
{{ product.price }}
69+
${{ product.price }}
7070
</td>
7171
<td class="border-b p-2 ">
7272
{{ product.updated_at }}

0 commit comments

Comments
 (0)