154 lines
5.0 KiB
Vue
154 lines
5.0 KiB
Vue
<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,
|
|
widthSpec,
|
|
applyWidth: (px: number | undefined) => {
|
|
resolvedWidth.value = px;
|
|
},
|
|
});
|
|
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,
|
|
);
|
|
/** Pixel width resolved by the managed (fr) layout; unset stays native. */
|
|
const resolvedWidth = ref<number | undefined>(undefined);
|
|
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 widthSpec = () => ({
|
|
width: width.value,
|
|
minWidth: props.minWidth,
|
|
maxWidth: props.maxWidth,
|
|
});
|
|
const cssWidth = (value: string | number | undefined) => {
|
|
if (value === undefined) return undefined;
|
|
if (typeof value === "number") return `${value}px`;
|
|
// fr is resolved by TableContent after measurement. Leave native layout
|
|
// untouched until a managed pixel width is available.
|
|
if (/^\d+(\.\d+)?fr$/.test(value)) return undefined;
|
|
return 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:
|
|
resolvedWidth.value !== undefined
|
|
? `${resolvedWidth.value}px`
|
|
: cssWidth(width.value),
|
|
minWidth: cssWidth(
|
|
resolvedWidth.value === undefined &&
|
|
typeof width.value === "number"
|
|
? Math.max(props.minWidth ?? 0, width.value)
|
|
: props.minWidth,
|
|
),
|
|
maxWidth: cssWidth(props.maxWidth),
|
|
},
|
|
"data-column-id": props.id,
|
|
"data-default-width":
|
|
typeof (props.width ?? width.value) === "string"
|
|
? String(props.width ?? width.value)
|
|
: undefined,
|
|
"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>
|