Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plain-plugins-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/table-core': patch
---

Publish `Table_All` and give `TableFeature` hooks that table type, so custom features can read their own options and state without casting `table.options`
8 changes: 2 additions & 6 deletions docs/framework/alpine/guide/custom-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ export const densityPlugin: TableFeature = {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -299,9 +297,7 @@ export const densityPlugin: TableFeature = {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg'
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
8 changes: 2 additions & 6 deletions docs/framework/ember/guide/custom-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ export const densityPlugin: TableFeature = {
const safeUpdater: Updater<DensityState> = (old) => {
return functionalUpdate(updater, old)
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -306,9 +304,7 @@ export const densityPlugin: TableFeature = {
// cycle through the 3 options
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg'
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
8 changes: 2 additions & 6 deletions examples/alpine/custom-plugin/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ export const densityPlugin: TableFeature = {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -114,9 +112,7 @@ export const densityPlugin: TableFeature = {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg' // cycle through the 3 options
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ export const densityPlugin: TableFeature = {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -95,9 +93,7 @@ export const densityPlugin: TableFeature = {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg' // cycle through the 3 options
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
8 changes: 2 additions & 6 deletions examples/ember/custom-plugin/app/templates/application.gts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ export const densityPlugin: TableFeature = {
const safeUpdater: Updater<DensityState> = (old) => {
return functionalUpdate(updater, old)
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -119,9 +117,7 @@ export const densityPlugin: TableFeature = {
// cycle through the 3 options
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg'
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions examples/octane/custom-plugin/src/main.tsrx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const densityPlugin: TableFeature = {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
return table.options.onDensityChange?.(
safeUpdater,
)
},
Expand All @@ -115,7 +115,7 @@ export const densityPlugin: TableFeature = {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg' // cycle through the 3 options
}
return (table.options as TableOptions_Density).onDensityChange?.(
return table.options.onDensityChange?.(
safeUpdater,
)
},
Expand Down
8 changes: 2 additions & 6 deletions examples/preact/custom-plugin/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ export const densityPlugin: TableFeature = {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -115,9 +113,7 @@ export const densityPlugin: TableFeature = {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg' // cycle through the 3 options
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
8 changes: 2 additions & 6 deletions examples/react/custom-plugin/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ export const densityPlugin: TableFeature = {
const newState = functionalUpdate(updater, old)
return newState
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
table_toggleDensity: {
Expand All @@ -116,9 +114,7 @@ export const densityPlugin: TableFeature = {
if (value) return value
return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg' // cycle through the 3 options
}
return (table.options as TableOptions_Density).onDensityChange?.(
safeUpdater,
)
return table.options.onDensityChange?.(safeUpdater)
},
},
})
Expand Down
26 changes: 26 additions & 0 deletions packages/table-core/src/types/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ export type Table<
> = Table_Core<TFeatures, TData> &
ExtractFeatureMapTypes<TFeatures, Table_FeatureMap<TFeatures, TData>>

/**
* `Table` with the options, state, and row models widened to their
* all-features types.
*
* `TableFeature` hooks receive this so a feature can read the options and state
* it contributes itself, which `Table` alone cannot describe because its
* members are narrowed to `TFeatures`.
*/
export type Table_All<
TFeatures extends TableFeatures,
TData extends RowData = any,
> = Table<TFeatures, TData> & {
_rowModels: CachedRowModel_All<TFeatures, TData>
_rowModelFns: RowModelFns_All<TFeatures, TData>
options: DebugOptions<TableFeatures> &
TableOptions_All<TFeatures, TData> & {
state?: TableState_All
initialState?: TableState_All
atoms?: ExternalAtoms_All
}
initialState: TableState<TFeatures> & TableState_All
baseAtoms: BaseAtoms<TFeatures> & BaseAtoms_All
atoms: Atoms<TFeatures> & Atoms_All
store: ReadonlyStore<TableState<TFeatures>> & ReadonlyStore<TableState_All>
}

/**
* `Table_Table` members that `Table_Internal` re-declares with broadened
* (all-features) types so internal code can read any feature's slots and
Expand Down
44 changes: 35 additions & 9 deletions scripts/rewrite-table-core-dts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ function removeNamedSpecifiers(source, names) {
)
}

function rewriteInternalImportSpecifiers(source) {
function rewriteInternalImportSpecifiers(source, tableReplacement) {
const replacements = new Map([
['Table_Internal', 'Table'],
['Table_Internal', tableReplacement],
['Column_Internal', 'Column'],
])

Expand Down Expand Up @@ -251,14 +251,33 @@ function ensureNamedImport(source, name, importPath) {
return `import { ${name} } from "${importPath}";\n${source}`
}

function ensurePublicTypeImports(source, file) {
function isDeclarationPath(file, suffix) {
return file.replaceAll('\\', '/').endsWith(suffix)
}

function ensurePublicTypeImports(source, file, tableReplacement) {
let next = source

if (!file.endsWith('/types/Table.d.ts') && /\bTable</.test(next)) {
if (!isDeclarationPath(file, '/types/Table.d.ts') && /\bTable</.test(next)) {
next = ensureNamedImport(next, 'Table', getImportPath(file, 'Table'))
}

if (!file.endsWith('/types/Column.d.ts') && /\bColumn</.test(next)) {
if (
tableReplacement !== 'Table' &&
!isDeclarationPath(file, '/types/Table.d.ts') &&
new RegExp(String.raw`\b${tableReplacement}<`).test(next)
) {
next = ensureNamedImport(
next,
tableReplacement,
getImportPath(file, 'Table'),
)
}

if (
!isDeclarationPath(file, '/types/Column.d.ts') &&
/\bColumn</.test(next)
) {
next = ensureNamedImport(next, 'Column', getImportPath(file, 'Column'))
}

Expand All @@ -268,20 +287,27 @@ function ensurePublicTypeImports(source, file) {
function rewriteDeclaration(source, file) {
let next = source

// `TableFeature` hooks are the plugin-authoring surface, so they keep the
// all-features table. Everywhere else narrows to `Table` so ordinary
// consumers are not handed optional state slices.
const tableReplacement = isDeclarationPath(file, '/types/TableFeatures.d.ts')
? 'Table_All'
: 'Table'

for (const typeName of forbiddenTypeNames) {
next = removeExportedInterface(next, typeName)
}

next = removeTypeAlias(next, 'Table_InternalBroadenedKeys')
next = rewriteInternalImportSpecifiers(next)
next = rewriteInternalImportSpecifiers(next, tableReplacement)
next = removeNamedSpecifiers(next, forbiddenTypeNames)

next = next.replaceAll('Table_Internal<', 'Table<')
next = next.replaceAll('Table_Internal<', `${tableReplacement}<`)
next = next.replaceAll('Column_Internal<', 'Column<')
next = next.replaceAll('Table_Internal', 'Table')
next = next.replaceAll('Table_Internal', tableReplacement)
next = next.replaceAll('Column_Internal', 'Column')

next = ensurePublicTypeImports(next, file)
next = ensurePublicTypeImports(next, file, tableReplacement)

return next
}
Expand Down