feat: migrate Toast and Table with reviewed state focus and SSR contracts
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
mergeProps,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import {
|
||||
tableColumnContextKey,
|
||||
tableContextKey,
|
||||
type TableSortDirection,
|
||||
} from "./context";
|
||||
|
||||
/**
|
||||
* Sortable column label + indicator, rendered inside a sortable Table.Column.
|
||||
* The column (th) owns the click target; this is a visual span.
|
||||
*/
|
||||
export default defineComponent({
|
||||
name: "TableSortableColumnHeader",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
/** Column id; alias kept for click-to-sort on the trigger button. */
|
||||
columnKey: { type: String, default: undefined },
|
||||
/** Current sort direction from the column's sort state. */
|
||||
sortDirection: {
|
||||
type: String as PropType<TableSortDirection | undefined>,
|
||||
default: undefined,
|
||||
},
|
||||
showIndicator: { type: Boolean, default: true },
|
||||
indicator: {
|
||||
type: [Object, String] as PropType<unknown>,
|
||||
default: undefined,
|
||||
},
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
as: { type: String, default: "span" },
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(tableContextKey, null);
|
||||
const column = inject(tableColumnContextKey, null);
|
||||
// Explicit prop wins; otherwise read the current descriptor for this column.
|
||||
const effectiveDirection = computed(() => {
|
||||
if (props.sortDirection !== undefined) return props.sortDirection;
|
||||
const d = ctx?.sortDescriptor.value;
|
||||
return d &&
|
||||
d.column === (props.columnKey ?? column?.key.value) &&
|
||||
d.direction !== "none"
|
||||
? d.direction
|
||||
: undefined;
|
||||
});
|
||||
const shouldRenderIndicator = computed(
|
||||
() => props.showIndicator && Boolean(effectiveDirection.value),
|
||||
);
|
||||
|
||||
function renderIndicator() {
|
||||
if (!shouldRenderIndicator.value) return null;
|
||||
const base = {
|
||||
class: ctx?.slots.value.sortableColumnIndicator(),
|
||||
"data-direction": effectiveDirection.value,
|
||||
"data-slot": "table-sortable-column-indicator",
|
||||
};
|
||||
if (props.indicator === undefined) {
|
||||
// Default chevron (baseline IconChevronUp), flips via data-direction.
|
||||
return h(
|
||||
"svg",
|
||||
{
|
||||
...base,
|
||||
"aria-hidden": "true",
|
||||
fill: "none",
|
||||
height: "12",
|
||||
viewBox: "0 0 12 12",
|
||||
width: "12",
|
||||
xmlns: "http://www.w3.org/2000/svg",
|
||||
},
|
||||
[
|
||||
h("path", {
|
||||
d: "M2.5 7.5L6 4l3.5 3.5",
|
||||
stroke: "currentColor",
|
||||
"stroke-linecap": "round",
|
||||
"stroke-linejoin": "round",
|
||||
"stroke-width": "1.5",
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
return props.indicator;
|
||||
}
|
||||
|
||||
return () => {
|
||||
// Strip onClick from the wrapper; it runs from the trigger button.
|
||||
const { onClick: _userOnClick, ...rest } = attrs as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...rest,
|
||||
class: [ctx?.slots.value.sortableColumnHeader(), attrs.class],
|
||||
"data-direction": effectiveDirection.value,
|
||||
"data-slot": "table-sortable-column-header",
|
||||
},
|
||||
[
|
||||
h(
|
||||
"button",
|
||||
mergeProps(
|
||||
{ onClick: attrs.onClick as unknown },
|
||||
{
|
||||
type: "button",
|
||||
disabled: props.isDisabled,
|
||||
class: "hv-table__sortable-column-trigger",
|
||||
onClick: (event: MouseEvent) => {
|
||||
if (event.defaultPrevented) return;
|
||||
if (props.isDisabled) {
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
if (!column?.allowsSorting.value && props.columnKey && ctx)
|
||||
ctx.sort(props.columnKey);
|
||||
},
|
||||
},
|
||||
),
|
||||
[slots.default?.(), renderIndicator()],
|
||||
),
|
||||
],
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user