71 lines
2.0 KiB
Vue
71 lines
2.0 KiB
Vue
<script setup lang="ts">
|
|
import {
|
|
SearchFieldClearButton,
|
|
SearchFieldSearchIcon,
|
|
SearchFieldGroup,
|
|
SearchFieldInput,
|
|
SearchField,
|
|
Label,
|
|
Description,
|
|
FieldError,
|
|
Button,
|
|
Spinner,
|
|
} from "@heroui-vue/components";
|
|
import { Form } from "@heroui-vue/components";
|
|
import { computed, onBeforeUnmount, ref } from "vue";
|
|
const value = ref("");
|
|
const isSubmitting = ref(false);
|
|
const result = ref("");
|
|
const isInvalid = computed(
|
|
() => value.value.length > 0 && value.value.length < 3,
|
|
);
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
onBeforeUnmount(() => clearTimeout(timer));
|
|
function submit(event: Event) {
|
|
if (value.value.length < 3 || isSubmitting.value) return;
|
|
const query = new FormData(event.currentTarget as HTMLFormElement).get(
|
|
"search",
|
|
);
|
|
isSubmitting.value = true;
|
|
timer = setTimeout(() => {
|
|
result.value = `已模拟提交搜索:${query}`;
|
|
value.value = "";
|
|
isSubmitting.value = false;
|
|
}, 1500);
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Form class="flex w-[280px] flex-col gap-4" @submit.prevent="submit">
|
|
<SearchField
|
|
v-model="value"
|
|
is-required
|
|
:is-invalid="isInvalid"
|
|
name="search"
|
|
>
|
|
<Label>搜索产品</Label>
|
|
<SearchFieldGroup>
|
|
<SearchFieldSearchIcon />
|
|
<SearchFieldInput class="w-full" placeholder="搜索产品…" />
|
|
<SearchFieldClearButton />
|
|
</SearchFieldGroup>
|
|
<FieldError v-if="isInvalid">搜索内容至少需要 3 个字符</FieldError>
|
|
<Description v-else>请输入至少 3 个字符后再搜索</Description>
|
|
</SearchField>
|
|
<Button
|
|
class="w-full"
|
|
:is-disabled="value.length < 3"
|
|
:is-pending="isSubmitting"
|
|
type="submit"
|
|
variant="primary"
|
|
>
|
|
<template v-if="isSubmitting">
|
|
<Spinner color="current" size="sm" />
|
|
搜索中…
|
|
</template>
|
|
<template v-else>搜索</template>
|
|
</Button>
|
|
<p v-if="result" role="status" class="text-sm text-muted">{{ result }}</p>
|
|
</Form>
|
|
</template>
|