feat: migrate Toast and Table with reviewed state focus and SSR contracts
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableColumn,
|
||||
TableContent,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TableScrollContainer,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
interface User {
|
||||
id: number;
|
||||
name: string;
|
||||
role: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
const roles = [
|
||||
"软件工程师",
|
||||
"高级工程师",
|
||||
"资深工程师",
|
||||
"产品经理",
|
||||
"设计师",
|
||||
"数据分析师",
|
||||
"测试工程师",
|
||||
"DevOps 工程师",
|
||||
"营销经理",
|
||||
"销售代表",
|
||||
];
|
||||
const firstNames = [
|
||||
"Emma",
|
||||
"Liam",
|
||||
"Olivia",
|
||||
"Noah",
|
||||
"Ava",
|
||||
"James",
|
||||
"Sophia",
|
||||
"Oliver",
|
||||
"Isabella",
|
||||
"Lucas",
|
||||
"Mia",
|
||||
"Ethan",
|
||||
"Charlotte",
|
||||
"Mason",
|
||||
"Amelia",
|
||||
"Logan",
|
||||
"Harper",
|
||||
"Alexander",
|
||||
"Ella",
|
||||
"Benjamin",
|
||||
];
|
||||
const lastNames = [
|
||||
"Smith",
|
||||
"Johnson",
|
||||
"Williams",
|
||||
"Brown",
|
||||
"Jones",
|
||||
"Garcia",
|
||||
"Miller",
|
||||
"Davis",
|
||||
"Rodriguez",
|
||||
"Martinez",
|
||||
"Anderson",
|
||||
"Taylor",
|
||||
"Thomas",
|
||||
"Jackson",
|
||||
"White",
|
||||
"Harris",
|
||||
"Clark",
|
||||
"Lewis",
|
||||
"Robinson",
|
||||
"Walker",
|
||||
];
|
||||
|
||||
function generateUsers(count: number): User[] {
|
||||
const users: User[] = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
const firstName = firstNames[i % firstNames.length];
|
||||
const lastName =
|
||||
lastNames[Math.floor(i / firstNames.length) % lastNames.length];
|
||||
users.push({
|
||||
email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@acme.com`,
|
||||
id: i + 1,
|
||||
name: `${firstName} ${lastName}`,
|
||||
role: roles[i % roles.length],
|
||||
});
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
const users = generateUsers(1000);
|
||||
const ITEM_HEIGHT = 48;
|
||||
const rowKeys = users.map((user) => user.id);
|
||||
const VIEWPORT = 320;
|
||||
const OVERSCAN = 8;
|
||||
const scrollTop = ref(0);
|
||||
const scrollEl = ref<HTMLElement | null>(null);
|
||||
|
||||
const startIndex = computed(() =>
|
||||
Math.max(0, Math.floor(scrollTop.value / ITEM_HEIGHT) - OVERSCAN),
|
||||
);
|
||||
const endIndex = computed(() =>
|
||||
Math.min(
|
||||
users.length,
|
||||
Math.ceil((scrollTop.value + VIEWPORT) / ITEM_HEIGHT) + OVERSCAN,
|
||||
),
|
||||
);
|
||||
const visible = computed(() => users.slice(startIndex.value, endIndex.value));
|
||||
const padTop = computed(() => startIndex.value * ITEM_HEIGHT);
|
||||
const padBottom = computed(() => (users.length - endIndex.value) * ITEM_HEIGHT);
|
||||
|
||||
function focusRow(_key: string | number, index: number) {
|
||||
const node = scrollEl.value;
|
||||
if (!node) return;
|
||||
const top = index * ITEM_HEIGHT;
|
||||
if (top < node.scrollTop) node.scrollTop = top;
|
||||
else if (top + ITEM_HEIGHT > node.scrollTop + VIEWPORT - 44)
|
||||
node.scrollTop = top + ITEM_HEIGHT - VIEWPORT + 44;
|
||||
scrollTop.value = node.scrollTop;
|
||||
}
|
||||
function onScroll(event: Event) {
|
||||
scrollTop.value = (event.target as HTMLElement).scrollTop;
|
||||
}
|
||||
onMounted(() => {
|
||||
scrollEl.value?.addEventListener("scroll", onScroll, { passive: true });
|
||||
});
|
||||
onUnmounted(() => {
|
||||
scrollEl.value?.removeEventListener("scroll", onScroll);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Table>
|
||||
<div
|
||||
ref="scrollEl"
|
||||
class="h-[320px] w-full overflow-auto"
|
||||
data-slot="table-virtual-viewport"
|
||||
>
|
||||
<TableContent
|
||||
aria-label="虚拟化表格"
|
||||
class="min-w-[600px] table-fixed"
|
||||
:row-keys="rowKeys"
|
||||
:aria-rowcount="users.length + 1"
|
||||
@focus-row="focusRow"
|
||||
>
|
||||
<TableHeader aria-rowindex="1">
|
||||
<TableColumn id="name" is-row-header>姓名</TableColumn>
|
||||
<TableColumn id="role">角色</TableColumn>
|
||||
<TableColumn id="email">邮箱</TableColumn>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
<tr aria-hidden="true" :style="{ height: padTop + 'px' }" />
|
||||
<TableRow
|
||||
v-for="user in visible"
|
||||
:key="user.id"
|
||||
:row-key="user.id"
|
||||
:aria-rowindex="user.id + 1"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
<TableCell
|
||||
class="h-12 whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
>
|
||||
{{ user.name }}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
class="h-12 whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
>
|
||||
{{ user.role }}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
class="h-12 whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
>
|
||||
{{ user.email }}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<tr aria-hidden="true" :style="{ height: padBottom + 'px' }" />
|
||||
</TableBody>
|
||||
</TableContent>
|
||||
</div>
|
||||
</Table>
|
||||
</template>
|
||||
Reference in New Issue
Block a user