Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use generic column types in resize-handle and data-sorting plugins (#247) #248

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
8 changes: 5 additions & 3 deletions ember-headless-table/src/plugins/column-resizing/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ let waiter = buildWaiter('ColumnResizing#ResizeHandle');
*
*/

class ResizeHandle extends Modifier<{ Args: { Positional: [Column] } }> {
class ResizeHandle<DataType = unknown> extends Modifier<{
Args: { Positional: [Column<DataType>] };
}> {
declare dragHandle: HTMLElement;
declare column: Column;
declare column: Column<DataType>;
declare meta: ColumnMeta;

// Pointer
Expand All @@ -45,7 +47,7 @@ class ResizeHandle extends Modifier<{ Args: { Positional: [Column] } }> {
token?: unknown;

isSetup = false;
modify(element: Element, [column]: [Column]) {
modify(element: Element, [column]: [Column<DataType>]) {
this.column = column;
this.meta = meta.forColumn(column, ColumnResizing);
this.dragHandle = element as HTMLElement;
Expand Down
8 changes: 5 additions & 3 deletions ember-headless-table/src/plugins/column-resizing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import type { Column } from '[public-types]';
* be marked as isResizing, because this is a user-scoped question:
* "Is the user directly resizing this column?"
*/
export const isResizing = (column: Column) => meta.forColumn(column, ColumnResizing).isResizing;
export const isResizing = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, ColumnResizing).isResizing;

/**
* Does the column have room to shrink?
*/
export const canShrink = (column: Column) => meta.forColumn(column, ColumnResizing).canShrink;
export const canShrink = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, ColumnResizing).canShrink;

/**
* Does the column have a resize handle?
Expand All @@ -29,7 +31,7 @@ export const canShrink = (column: Column) => meta.forColumn(column, ColumnResizi
* - if resizing is enabled for the whole table
* - or if we're asking about the first column (resize handles may only be "between" columns)
*/
export const hasResizeHandle = (column: Column) =>
export const hasResizeHandle = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, ColumnResizing).hasResizeHandle;

/**
Expand Down
15 changes: 10 additions & 5 deletions ember-headless-table/src/plugins/data-sorting/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ import type { Column } from '[public-types]';
/**
* Query a specific column's current sort direction
*/
export const sortDirection = (column: Column) => meta.forColumn(column, Sorting).sortDirection;
export const sortDirection = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, Sorting).sortDirection;

/**
* Ask if a column is sortable
*/
export const isSortable = (column: Column) => meta.forColumn(column, Sorting).isSortable;
export const isSortable = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, Sorting).isSortable;

/**
* Ask if a column is ascending
*/
export const isAscending = (column: Column) => meta.forColumn(column, Sorting).isAscending;
export const isAscending = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, Sorting).isAscending;

/**
* Ask if a column is sorted descending
*/
export const isDescending = (column: Column) => meta.forColumn(column, Sorting).isDescending;
export const isDescending = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, Sorting).isDescending;

/**
* Ask if a column is not sorted
*/
export const isUnsorted = (column: Column) => meta.forColumn(column, Sorting).isUnsorted;
export const isUnsorted = <DataType = unknown>(column: Column<DataType>) =>
meta.forColumn(column, Sorting).isUnsorted;

/**
* Sort the specified column's data using a tri-toggle.
Expand Down
16 changes: 8 additions & 8 deletions ember-headless-table/src/plugins/data-sorting/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ declare module 'ember-headless-table/plugins' {

export interface Signature<DataType = unknown> {
Meta: {
Column: ColumnMeta;
Table: TableMeta;
Column: ColumnMeta<DataType>;
Table: TableMeta<DataType>;
};
Options: {
Plugin: Options<DataType>;
Expand Down Expand Up @@ -92,8 +92,8 @@ export class Sorting<DataType = unknown> extends BasePlugin<Signature<DataType>>
};
}

export class ColumnMeta {
constructor(private column: Column) {}
export class ColumnMeta<DataType = unknown> {
constructor(private column: Column<DataType>) {}

@cached
get options() {
Expand Down Expand Up @@ -131,8 +131,8 @@ export class ColumnMeta {
}
}

export class TableMeta {
constructor(private table: Table) {}
export class TableMeta<DataType = unknown> {
constructor(private table: Table<DataType>) {}

@cached
get options() {
Expand All @@ -152,7 +152,7 @@ export class TableMeta {
}

@action
handleSort(column: Column) {
handleSort(column: Column<DataType>) {
let columnMeta = meta.forColumn(column, Sorting);

if (!columnMeta.sortProperty) {
Expand All @@ -169,7 +169,7 @@ export class TableMeta {
}

@action
toggleAscending(column: Column) {
toggleAscending(column: Column<DataType>) {
let columnMeta = meta.forColumn(column, Sorting);

if (!columnMeta.sortProperty) {
Expand Down
4 changes: 2 additions & 2 deletions ember-headless-table/src/plugins/metadata/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Metadata } from './plugin';

import type { Column, Table } from '[public-types]';

export const forColumn = (column: Column<any>, key: string) => {
export const forColumn = <DataType = unknown>(column: Column<DataType>, key: string) => {
return options.forColumn(column, Metadata)[key];
};

export const forTable = (table: Table<any>, key: string) => {
export const forTable = <DataType = unknown>(table: Table<DataType>, key: string) => {
return options.forTable(table, Metadata)[key];
};
Loading