Files
heroui-vue/apps/docs/demos/combo-box-custom-validation.vue
T
maoyingjie 3e9be13b19 fix(pickers): repair data-slot selectors and align demos with the baseline
The CSS port prefixed data-slot attribute values with hv-, so the compiled
selectors never matched the DOM and the ComboBox trigger chevron never
rotated. Only class names get the prefix; slot values stay byte-identical.

Also commit typed text as the value when allowsCustomValue finds no record,
restore the baseline search-field shell in all 24 Autocomplete demos, and
correct both doc pages against the pinned upstream sources.
2026-10-25 17:46:32 +08:00

73 lines
1.7 KiB
Vue

<script setup lang="ts">
import { ref } from "vue";
import {
Button,
ComboBox,
ComboBoxInputGroup,
ComboBoxPopover,
ComboBoxTrigger,
FieldError,
Form,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
} from "@heroui-vue/components";
const animals = [
{ id: "aardvark", name: "土豚" },
{ id: "cat", name: "猫" },
{ id: "dog", name: "狗" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "panda", name: "熊猫" },
{ id: "snake", name: "蛇" },
];
function validate(value: string | number | (string | number)[] | null) {
if (value == null) {
return "请选择一种动物";
}
if (value === "snake") {
return "不允许选择蛇";
}
return true;
}
const message = ref("");
function onSubmit() {
message.value = "表单提交成功!";
}
</script>
<template>
<Form class="flex w-[256px] flex-col gap-4" @submit.prevent="onSubmit">
<ComboBox is-required class="w-full" name="animal" :validate="validate">
<Label>最喜欢的动物</Label>
<ComboBoxInputGroup>
<Input placeholder="搜索动物…" />
<ComboBoxTrigger />
</ComboBoxInputGroup>
<ComboBoxPopover>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</ComboBoxPopover>
<FieldError />
</ComboBox>
<Button class="w-fit" type="submit">提交</Button>
<p v-if="message" class="text-sm text-muted" role="status">{{ message }}</p>
</Form>
</template>