검색 2글자 이상

This commit is contained in:
dyhj625 2025-03-27 14:43:01 +09:00
parent fdf439246e
commit 948199c724

View File

@ -1,17 +1,21 @@
<template>
<form @submit.prevent="search">
<div class="input-group mb-3 d-flex">
<input
type="text"
class="form-control"
placeholder="Search"
v-model="searchQuery"
@keyup.enter="search"
@input="preventLeadingSpace"
/>
<button type="button" class="btn btn-primary" @click="search">
<button
type="submit"
class="btn btn-primary"
>
<i class="bx bx-search bx-md"></i>
</button>
</div>
</form>
</template>
<script setup>
@ -28,15 +32,31 @@ const props = defineProps({
const emits = defineEmits(["update:data"]);
const searchQuery = ref("");
const search = function () {
// Type Number maxlength
if (searchQuery.value.length > props.maxlength) {
searchQuery.value = searchQuery.value.slice(0, props.maxlength);
// ( or )
const search = () => {
const trimmedQuery = searchQuery.value.trimStart();
if (trimmedQuery === "") {
emits("update:data", "");
return;
}
if (trimmedQuery.length < 2 ) {
alert("검색어는 최소 2글자 이상 입력해주세요.");
searchQuery.value = '';
return;
}
//
if (trimmedQuery.length > props.maxlength) {
searchQuery.value = trimmedQuery.slice(0, props.maxlength);
} else {
searchQuery.value = trimmedQuery;
}
emits("update:data", searchQuery.value);
};
const preventLeadingSpace = function () {
//
const preventLeadingSpace = () => {
searchQuery.value = searchQuery.value.trimStart();
};
</script>