An open source design system that's fully customizable and agent ready
TypeScript
fix(Table): a swizzled Table honors the package's virtualization capability (#10798)
Cindy Zhangda3dbdc8bd86
diff --git a/.changeset/table-virtualization-swizzle-brand.md b/.changeset/table-virtualization-swizzle-brand.mdnew file mode 100644index 0000000000..cd2ca7b438--- /dev/null+++ b/.changeset/table-virtualization-swizzle-brand.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A swizzled `Table` now honors `virtualization` from the package's `useTableRowsVirtualization`, and the package `Table` honors a capability from a swizzled copy. Before, each copy recognized only its own capability, so a swizzled Table rendered every row and its code failed to type-check. The capability is read through a registered, versioned key, and its type carries one required brand member that is never present at runtime, so only the hook makes a capability. A `virtualization` value the hook did not make now gets its own development warning. No API changes.++@cixzhangdiff --git a/packages/core/api-report.md b/packages/core/api-report.mdindex 283319c53e..7fe253d6ab 100644--- a/packages/core/api-report.md+++ b/packages/core/api-report.md@@ -5554,7 +5554,7 @@ interface TableTreeRowMetalevel: numbertype TableVerticalAlign = 'middle' | 'top' | 'bottom'interface TableVirtualizationCapability- [capabilityBrand]: Readonly<UseTableRowsVirtualizationConfig>+ @astryxdesign/core/table-virtualization/v1: neverinterface TextAreaProps extends Omit<FieldInputBaseProps, 'onChange' | 'defaultValue'>, TextEntryTraitProps (props of TextArea)type TextAreaSize = 'sm' | 'md' | 'lg'interface TextAreaStatusdiff --git a/packages/core/src/Table/BaseTable.tsx b/packages/core/src/Table/BaseTable.tsxindex f46d451c5d..ce91c78bc5 100644--- a/packages/core/src/Table/BaseTable.tsx+++ b/packages/core/src/Table/BaseTable.tsx@@ -616,6 +616,9 @@ function BaseTableInner<T extends Record<string, unknown>>({const headerRowCount = mode === 'data' && resolvedColumns.length > 0 ? 1 : 0;const config = getTableVirtualizationConfig(virtualization);const windowFallbackReason = useMemo(() => {+ if (virtualization != null && config == null) {+ return 'virtualization is not a value from useTableRowsVirtualization';+ }if (mode !== 'data') {return 'windowing requires data mode';}@@ -659,6 +662,7 @@ function BaseTableInner<T extends Record<string, unknown>>({rowIndexStart,rowCount,resolvedColumns,+ virtualization,config,]);const windowAdmitted = windowFallbackReason === undefined;diff --git a/packages/core/src/Table/TableWindowFallback.test.tsx b/packages/core/src/Table/TableWindowFallback.test.tsxindex ff8a0cc532..4b562989f7 100644--- a/packages/core/src/Table/TableWindowFallback.test.tsx+++ b/packages/core/src/Table/TableWindowFallback.test.tsx@@ -78,6 +78,28 @@ describe('Table windowing admission diagnostics', () => {view.rerender(<StatusFixture />);expect(warning).toHaveBeenCalledTimes(1);});+ it('names a virtualization value the hook did not make', () => {+ const warning = vi.spyOn(console, 'warn').mockImplementation(() => {});+ const foreign = Object.freeze({rowHeight: 56, viewportHeight: 240});+ const view = render(+ <Table+ data={data}+ columns={columns}+ idKey="id"+ virtualization={+ foreign as unknown as BaseTableProps<Item>['virtualization']+ }+ />,+ );+ expect(view.container.querySelectorAll('tbody > tr')).toHaveLength(+ data.length,+ );+ expect(warning).toHaveBeenCalledTimes(1);+ expect(warning.mock.calls[0].join(' ')).toContain(+ 'not a value from useTableRowsVirtualization',+ );+ });+it('renders fully with one diagnostic when a caller replaces the owned scroll wrapper', () => {const CallerWrapper: NonNullable<BaseTableProps<Item>['scrollWrapper']> = ({children,diff --git a/packages/core/src/Table/plugins/virtualization/useTableRowsVirtualization.ts b/packages/core/src/Table/plugins/virtualization/useTableRowsVirtualization.tsindex 994e123251..a157f1fbf6 100644--- a/packages/core/src/Table/plugins/virtualization/useTableRowsVirtualization.ts+++ b/packages/core/src/Table/plugins/virtualization/useTableRowsVirtualization.ts@@ -9,8 +9,11 @@* @position Geometry-only opt-in; Table owns data, admission and rendering*/import {useMemo} from 'react';+import {sharedKey} from '../../../utils/sharedRealm';-const capabilityBrand: unique symbol = Symbol('TableVirtualizationCapability');+// A registered key, so a swizzled Table reads a capability the package's hook+// made, and the package reads a copy's.+const CAPABILITY = sharedKey('table/virtualization-capability', 1);export interface UseTableRowsVirtualizationConfig {/** Total body-row pitch, including padding and collapsed borders, in CSS pixels. */@@ -21,7 +24,11 @@ export interface UseTableRowsVirtualizationConfig {/** Geometry intent, not a data slice, renderer, scroll handle or plugin. */export interface TableVirtualizationCapability {- readonly [capabilityBrand]: Readonly<UseTableRowsVirtualizationConfig>;+ /**+ * A brand that is never present at runtime. It is required, so only the+ * hook makes a capability, and its name is the same in a swizzled copy.+ */+ readonly '@astryxdesign/core/table-virtualization/v1': never;}/** Private consumption seam. Parent validates geometry and all logical inputs. */@@ -31,11 +38,15 @@ export function getTableVirtualizationConfig(if (typeof capability !== 'object' ||capability === null ||- !(capabilityBrand in capability)+ !(CAPABILITY in capability)) {return undefined;}- return (capability as TableVirtualizationCapability)[capabilityBrand];+ return (+ capability as {+ readonly [CAPABILITY]: Readonly<UseTableRowsVirtualizationConfig>;+ }+ )[CAPABILITY];}/**@@ -55,8 +66,8 @@ export function useTableRowsVirtualization({return useMemo(() =>Object.freeze({- [capabilityBrand]: Object.freeze({rowHeight, viewportHeight}),- }),+ [CAPABILITY]: Object.freeze({rowHeight, viewportHeight}),+ }) as unknown as TableVirtualizationCapability,[rowHeight, viewportHeight],);}diff --git a/packages/core/src/utils/sharedBrands.test.ts b/packages/core/src/utils/sharedBrands.test.tsindex abed42267b..637cdc70da 100644--- a/packages/core/src/utils/sharedBrands.test.ts+++ b/packages/core/src/utils/sharedBrands.test.ts@@ -12,6 +12,7 @@ import * as os from 'node:os';import * as path from 'node:path';import {fileURLToPath} from 'node:url';import ts from 'typescript';+import {renderHook} from '@testing-library/react';import {describe, expect, it, vi} from 'vitest';const SRC = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');@@ -75,6 +76,29 @@ describe('brands a swizzled copy shares with the package', () => {).toBeDefined();});+ it("a copy of Table's virtualization module reads a package capability", async () => {+ const pkg =+ await import('../Table/plugins/virtualization/useTableRowsVirtualization');+ vi.resetModules();+ const copy =+ await import('../Table/plugins/virtualization/useTableRowsVirtualization');+ expect(copy).not.toBe(pkg);+ // A swizzled Table carries its own copy of this module and reads the+ // capability the package's useTableRowsVirtualization made.+ const {result} = renderHook(() =>+ pkg.useTableRowsVirtualization({rowHeight: 40, viewportHeight: 400}),+ );+ expect(copy.getTableVirtualizationConfig(result.current)).toEqual({+ rowHeight: 40,+ viewportHeight: 400,+ });+ // The brand member is required, so a plain object is not a capability:+ // only the hook makes one.+ // @ts-expect-error -- `{}` lacks the capability's brand.+ const notACapability: typeof result.current = {};+ expect(copy.getTableVirtualizationConfig(notACapability)).toBeUndefined();+ });+it('the package and a copy give the branded types one shape', () => {// Two paths to Core's source that TypeScript reads as two modules each,// as it reads a package module and the copy of it.@@ -89,6 +113,10 @@ describe('brands a swizzled copy shares with the package', () => {['SnapPaging/snapPagingValue', 'SnapPaging'],['Markdown/plugins/protocol', 'MarkdownPluginEntry'],['Markdown/plugins/protocol', 'MarkdownTransform'],+ [+ 'Table/plugins/virtualization/useTableRowsVirtualization',+ 'TableVirtualizationCapability',+ ],];fs.writeFileSync(check,