@tanstack/ember-table
Version:
Headless UI for building powerful tables & datagrids for Ember.
153 lines (147 loc) • 4.81 kB
JavaScript
import Component from '@glimmer/component';
import { cached } from '@glimmer/tracking';
import { flexRender } from '@tanstack/table-core/flex-render';
import { precompileTemplate } from '@ember/template-compilation';
import { setComponentTemplate } from '@ember/component';
import { n } from 'decorator-transforms/runtime-esm';
class FlexRenderComponentConfig {
component;
options;
constructor(component, options) {
this.component = component;
this.options = options;
}
}
function flexRenderComponent(component, options) {
return new FlexRenderComponentConfig(component, options);
}
class FlexRenderCell extends Component {
get result() {
const cell = this.args.cell;
const definition = cell.column.columnDef;
const groupingCell = cell;
const groupingDefinition = definition;
if (groupingCell.getIsAggregated?.()) {
return flexRender(groupingDefinition.aggregatedCell ?? definition.cell, cell.getContext());
}
if (groupingCell.getIsPlaceholder?.()) {
return null;
}
return flexRender(definition.cell, cell.getContext());
}
static {
n(this.prototype, "result", [cached]);
}
get resolvedContext() {
return this.args.cell.getContext();
}
get isComponent() {
return this.result instanceof FlexRenderComponentConfig;
}
get componentToRender() {
const result = this.result;
if (result instanceof FlexRenderComponentConfig) {
return result.component;
}
return undefined;
}
get componentOptions() {
const result = this.result;
if (result instanceof FlexRenderComponentConfig) {
return result.options;
}
return undefined;
}
get content() {
return this.result;
}
static {
setComponentTemplate(precompileTemplate("{{#if this.isComponent}}\n <this.componentToRender @ctx={{this.resolvedContext}} @options={{this.componentOptions}} />\n{{else}}\n {{this.content}}\n{{/if}}", {
strictMode: true
}), this);
}
}
// --- FlexRenderHeader ---
class FlexRenderHeader extends Component {
get result() {
const header = this.args.header;
// Placeholder headers are not skipped here. Whether a placeholder renders
// is the template's decision: the usual pattern wraps this component in
// `{{#unless header.isPlaceholder}}`, but merging headers vertically with
// `header.rowSpan` requires the spanning placeholder to render its
// column's header content. This matches every other framework adapter.
return flexRender(header.column.columnDef.header, header.getContext());
}
static {
n(this.prototype, "result", [cached]);
}
get resolvedContext() {
return this.args.header.getContext();
}
get isComponent() {
return this.result instanceof FlexRenderComponentConfig;
}
get componentToRender() {
const result = this.result;
if (result instanceof FlexRenderComponentConfig) {
return result.component;
}
return undefined;
}
get componentOptions() {
const result = this.result;
if (result instanceof FlexRenderComponentConfig) {
return result.options;
}
return undefined;
}
get content() {
return this.result;
}
static {
setComponentTemplate(precompileTemplate("{{#if this.isComponent}}\n <this.componentToRender @ctx={{this.resolvedContext}} @options={{this.componentOptions}} />\n{{else}}\n {{this.content}}\n{{/if}}", {
strictMode: true
}), this);
}
}
// --- FlexRenderFooter ---
class FlexRenderFooter extends Component {
get result() {
const footer = this.args.footer;
// Placeholder footers are not skipped here; see `FlexRenderHeader`. The
// template decides, which matches every other framework adapter.
return flexRender(footer.column.columnDef.footer, footer.getContext());
}
static {
n(this.prototype, "result", [cached]);
}
get resolvedContext() {
return this.args.footer.getContext();
}
get isComponent() {
return this.result instanceof FlexRenderComponentConfig;
}
get componentToRender() {
const result = this.result;
if (result instanceof FlexRenderComponentConfig) {
return result.component;
}
return undefined;
}
get componentOptions() {
const result = this.result;
if (result instanceof FlexRenderComponentConfig) {
return result.options;
}
return undefined;
}
get content() {
return this.result;
}
static {
setComponentTemplate(precompileTemplate("{{#if this.isComponent}}\n <this.componentToRender @ctx={{this.resolvedContext}} @options={{this.componentOptions}} />\n{{else}}\n {{this.content}}\n{{/if}}", {
strictMode: true
}), this);
}
}
export { FlexRenderCell as F, FlexRenderComponentConfig as a, FlexRenderFooter as b, FlexRenderHeader as c, flexRenderComponent as f };