feat: migrate Toast and Table with reviewed state focus and SSR contracts
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
mergeProps,
|
||||
provide,
|
||||
useAttrs,
|
||||
ref,
|
||||
watch,
|
||||
onBeforeUnmount,
|
||||
} from "vue";
|
||||
import {
|
||||
tableContextKey,
|
||||
tableColumnContextKey,
|
||||
tableColumnWidthKey,
|
||||
type TableSortDirection,
|
||||
} from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "TableColumn",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
as: { type: String, default: "th" },
|
||||
allowsSorting: { type: Boolean, default: false },
|
||||
isRowHeader: { type: Boolean, default: false },
|
||||
id: { type: String, default: undefined },
|
||||
width: { type: [String, Number], default: undefined },
|
||||
maxWidth: { type: Number, default: undefined },
|
||||
minWidth: { type: Number, default: undefined },
|
||||
defaultWidth: { type: [String, Number], default: undefined },
|
||||
},
|
||||
emits: { "update:width": (_width: number) => true },
|
||||
setup(props, { slots, emit }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(tableContextKey, null);
|
||||
const token = Symbol();
|
||||
const element = ref<HTMLElement | null>(null);
|
||||
watch(
|
||||
[element, () => props.id, () => props.isRowHeader],
|
||||
([node, id, isRowHeader]) => {
|
||||
if (!ctx?.columns) return;
|
||||
const next = new Map(ctx.columns.value);
|
||||
if (node) next.set(token, { node, id, isRowHeader });
|
||||
else next.delete(token);
|
||||
ctx.columns.value = next;
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
onBeforeUnmount(() => {
|
||||
if (ctx?.columns) {
|
||||
const next = new Map(ctx.columns.value);
|
||||
next.delete(token);
|
||||
ctx.columns.value = next;
|
||||
}
|
||||
});
|
||||
const internalWidth = ref(props.defaultWidth);
|
||||
const width = computed(() =>
|
||||
props.width !== undefined ? props.width : internalWidth.value,
|
||||
);
|
||||
const setWidth = (next: number) => {
|
||||
if (props.width === undefined) internalWidth.value = next;
|
||||
emit("update:width", next);
|
||||
};
|
||||
provide(tableColumnWidthKey, {
|
||||
minWidth: computed(() => props.minWidth),
|
||||
maxWidth: computed(() => props.maxWidth),
|
||||
width,
|
||||
setWidth,
|
||||
});
|
||||
const cssWidth = (value: string | number | undefined) =>
|
||||
typeof value === "number" ? `${value}px` : value;
|
||||
const sortDirection = computed<TableSortDirection | undefined>(() => {
|
||||
if (!props.allowsSorting || !props.id) return undefined;
|
||||
const d = ctx?.sortDescriptor.value;
|
||||
if (!d || d.column !== props.id) return undefined;
|
||||
return d.direction === "none" ? undefined : d.direction;
|
||||
});
|
||||
provide(tableColumnContextKey, {
|
||||
key: computed(() => props.id),
|
||||
allowsSorting: computed(() => props.allowsSorting),
|
||||
sortDirection: computed(() => sortDirection.value ?? "none"),
|
||||
});
|
||||
return () => {
|
||||
// Vue DOM merge: user onClick first, then the sort proposal.
|
||||
return h(
|
||||
props.as,
|
||||
mergeProps(attrs as Record<string, unknown>, {
|
||||
ref: element,
|
||||
class: [ctx?.slots.value.column(), attrs.class],
|
||||
style: {
|
||||
width: cssWidth(width.value),
|
||||
minWidth: cssWidth(
|
||||
typeof width.value === "number"
|
||||
? Math.max(props.minWidth ?? 0, width.value)
|
||||
: props.minWidth,
|
||||
),
|
||||
maxWidth: cssWidth(props.maxWidth),
|
||||
},
|
||||
"data-column-id": props.id,
|
||||
"data-row-header": props.isRowHeader || undefined,
|
||||
"data-slot": "table-column",
|
||||
"data-allows-sorting": props.allowsSorting || undefined,
|
||||
"aria-sort": props.allowsSorting
|
||||
? (sortDirection.value ?? "none")
|
||||
: undefined,
|
||||
// Row-header flag is metadata for the row cells; the header
|
||||
// th keeps column scope.
|
||||
scope: "col",
|
||||
onClick: (event: MouseEvent) => {
|
||||
if (event.defaultPrevented) return;
|
||||
if (props.allowsSorting && props.id && ctx) {
|
||||
ctx.sort(props.id);
|
||||
}
|
||||
},
|
||||
}),
|
||||
slots.default?.(),
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user