@isoftdata/svelte-table 2.6.7 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Table.svelte CHANGED
@@ -20,7 +20,15 @@
20
20
 
21
21
  import type { i18n } from 'i18next'
22
22
  import type { Get } from 'type-fest'
23
- import type { Column as GenericColumn, SortDirection, UuidRowProps, RowProperties, FooterReducerValue } from './'
23
+ import {
24
+ type Column as GenericColumn,
25
+ type SortDirection,
26
+ type UuidRowProps,
27
+ type RowProperties,
28
+ type FooterReducerValue,
29
+ ARBITRARY_PROPERTY_PREFIX,
30
+ type ArbitraryProperty,
31
+ } from './'
24
32
 
25
33
  import { writable, type Writable } from 'svelte/store'
26
34
 
@@ -40,11 +48,13 @@
40
48
 
41
49
  type K = keyof R
42
50
  type KeyPath = RowProperties<R>
51
+ type ColumnProperty = KeyPath | ArbitraryProperty
52
+
43
53
  type IndexedRow = R & { originalIndex: number; uuid: string }
44
54
  type GetRowProperty = Get<R, KeyPath>
45
55
  type GetIndexedRowProperty = Get<IndexedRow, RowProperties<IndexedRow>>
46
56
  type FooterValue = {
47
- property: KeyPath
57
+ property: ColumnProperty
48
58
  value: FooterReducerValue<R>
49
59
  }
50
60
  // Type hack so passing an array of columns without the type argument doesn't reduce the type of R to any
@@ -234,6 +244,11 @@
234
244
 
235
245
  // #endregion
236
246
  // #region Functions
247
+
248
+ function isArbitraryProperty(property: string | undefined): property is ArbitraryProperty {
249
+ return !!property?.startsWith(ARBITRARY_PROPERTY_PREFIX)
250
+ }
251
+
237
252
  export async function defaultColumnClicked(clickedColumn: Column, sortDirection: SortDirection) {
238
253
  if (currentPageNumber !== lastPageNumber) {
239
254
  paginationComponent?.setPageNumber(1)
@@ -248,7 +263,9 @@
248
263
  }
249
264
 
250
265
  function defaultFilter(filter: string, rows: R[], columns: Column[]): IndexedRow[] {
251
- const columnProps = columns?.map(({ property }) => property)
266
+ const columnProps = columns
267
+ ?.map(({ property }) => property)
268
+ .filter((property): property is KeyPath => !isArbitraryProperty(property))
252
269
 
253
270
  if (filterEnabled) {
254
271
  const props = filterProps || columnProps
@@ -318,7 +335,7 @@
318
335
  }
319
336
 
320
337
  async function columnClickHandler(clickedColumn: Column) {
321
- if (clickedColumn.sortType === false || columnResizingIsActive) {
338
+ if (clickedColumn.sortType === false || columnResizingIsActive || isArbitraryProperty(clickedColumn.property)) {
322
339
  return
323
340
  }
324
341
  sortDirection = sortDirection === 'ASC' && clickedColumn.property === sortColumn?.property ? 'DESC' : 'ASC'
@@ -448,10 +465,15 @@
448
465
  const hasPreviousSort = previousSortColumn && previousSortOrder.length > 0
449
466
 
450
467
  let sortProp: KeyPath
451
-
452
- if (keepSameSortOrder && hasPreviousSort && previousSortColumn && idProp) {
468
+ if (
469
+ keepSameSortOrder &&
470
+ hasPreviousSort &&
471
+ previousSortColumn &&
472
+ idProp &&
473
+ !isArbitraryProperty(previousSortColumn.property)
474
+ ) {
453
475
  sortColumn = previousSortColumn
454
- sortProp = sortColumn.property
476
+ sortProp = previousSortColumn.property
455
477
  sortDirection = 'ASC' //force ASC for keeping the same order because we defined the order in previousSortOrder
456
478
 
457
479
  rows = rows.map(row => {
@@ -461,7 +483,7 @@
461
483
 
462
484
  return { ...row, order: foundItem ? foundItem.order : 0 }
463
485
  })
464
- } else if (sortColumn?.property) {
486
+ } else if (sortColumn?.property && !isArbitraryProperty(sortColumn.property)) {
465
487
  sortProp = sortColumn.property
466
488
  } else {
467
489
  // no sort property, can't sort
@@ -540,7 +562,7 @@
540
562
  }
541
563
  }
542
564
 
543
- export function setColumnVisibility(columnProperties: Array<KeyPath> | KeyPath, visible: boolean) {
565
+ export function setColumnVisibility(columnProperties: Array<ColumnProperty> | ColumnProperty, visible: boolean) {
544
566
  if (!Array.isArray(columnProperties)) {
545
567
  columnProperties = [columnProperties]
546
568
  }
@@ -548,7 +570,7 @@
548
570
  try {
549
571
  untrack(() => {
550
572
  columnInfo.updateColumns(
551
- columnProperties.map((property): { property: RowProperties<R>; visible: boolean } => ({
573
+ columnProperties.map((property): { property: ColumnProperty; visible: boolean } => ({
552
574
  property,
553
575
  visible,
554
576
  })),
@@ -560,7 +582,10 @@
560
582
  }
561
583
 
562
584
  /** When called in `onMount` or an effect, will set the visibility of the specified columns whenever the value of `getVisible()` changes*/
563
- export function setColumnVisibilityWatch(columnProperties: Array<KeyPath> | KeyPath, getVisible: () => boolean) {
585
+ export function setColumnVisibilityWatch(
586
+ columnProperties: Array<ColumnProperty> | ColumnProperty,
587
+ getVisible: () => boolean,
588
+ ) {
564
589
  $effect(() => {
565
590
  const visible = getVisible()
566
591
  setColumnVisibility(columnProperties, visible)
@@ -643,7 +668,7 @@
643
668
  await tick()
644
669
  if (columnResizingEnabled) {
645
670
  columnInfo.updateColumns(
646
- columns.map((column): { property: RowProperties<R>; userWidth: string } => {
671
+ columns.map((column): { property: ColumnProperty; userWidth: string } => {
647
672
  const thisColumnInfo = columnInfo.current[column.property]
648
673
  const prevUserWidth = thisColumnInfo?.userWidth
649
674
  // Minimum width will be inherited from the layout on reset, but this won't, so re-set it here if needed
@@ -698,7 +723,9 @@
698
723
  const mathFunctions = ['AVG', 'SUM']
699
724
 
700
725
  let columnValues: Array<number | GetRowProperty> = rows.map(row => {
701
- const value = getNestedProperty(row, property)
726
+ const value = isArbitraryProperty(property)
727
+ ? ('' as Get<R, RowProperties<R>>)
728
+ : getNestedProperty(row, property)
702
729
  if (
703
730
  column.footer?.fn &&
704
731
  typeof column.footer.fn === 'string' &&
@@ -821,7 +848,7 @@
821
848
  <th
822
849
  id="col-{tableId}-{column.property}"
823
850
  title={column.title ?? column.name}
824
- style:cursor={column.sortType !== false ? 'pointer' : ''}
851
+ style:cursor={column.sortType !== false && !isArbitraryProperty(column.property) ? 'pointer' : ''}
825
852
  style:width={column.userWidth ?? column.width}
826
853
  style:min-width={column.minWidth}
827
854
  data-sort-specified={isSortColumn ? sortDirection : ''}
@@ -889,9 +916,11 @@
889
916
  align={column.align}
890
917
  property={column.property}
891
918
  >
892
- {column.formatter
893
- ? column.formatter(getNestedProperty(row, column.property, ''))
894
- : getNestedProperty(row, column.property, '')}
919
+ {isArbitraryProperty(column.property)
920
+ ? ''
921
+ : column.formatter
922
+ ? column.formatter(getNestedProperty(row, column.property, ''))
923
+ : getNestedProperty(row, column.property, '')}
895
924
  </Td>
896
925
  {/each}
897
926
  </tr>
@@ -908,7 +937,10 @@
908
937
  {@render footerRow({ footers })}
909
938
  {:else}
910
939
  {#each footers as { property, value } (property)}
911
- <Td {property}>{value ?? ''}</Td>
940
+ <Td
941
+ {property}
942
+ tagName="TH">{value ?? ''}</Td
943
+ >
912
944
  {/each}
913
945
  {/if}
914
946
  </tr>
@@ -1,6 +1,6 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  import { ColumnInfoRunicStore } from './column-info-store.svelte';
3
- import type { Column as GenericColumn, SortDirection, UuidRowProps, RowProperties, FooterReducerValue } from './';
3
+ import { type Column as GenericColumn, type SortDirection, type UuidRowProps, type RowProperties, type FooterReducerValue } from './';
4
4
  import type { HTMLTableAttributes, ClassValue } from 'svelte/elements';
5
5
  declare class __sveltets_Render<R extends UuidRowProps> {
6
6
  props(): Omit<HTMLTableAttributes, "children" | `bind:${string}` | `on:${string}` | `aria-${string}`> & {
@@ -15,7 +15,7 @@ declare class __sveltets_Render<R extends UuidRowProps> {
15
15
  currentPageNumber?: number;
16
16
  showFooter?: boolean;
17
17
  footers?: {
18
- property: RowProperties<R>;
18
+ property: `_${string}` | RowProperties<R>;
19
19
  value: FooterReducerValue<R>;
20
20
  }[] | undefined;
21
21
  lazySort?: boolean;
@@ -126,7 +126,7 @@ declare class __sveltets_Render<R extends UuidRowProps> {
126
126
  }]> | undefined;
127
127
  footerRow?: Snippet<[{
128
128
  footers: {
129
- property: RowProperties<R>;
129
+ property: `_${string}` | RowProperties<R>;
130
130
  value: FooterReducerValue<R>;
131
131
  }[];
132
132
  }]> | undefined;
@@ -203,8 +203,8 @@ declare class __sveltets_Render<R extends UuidRowProps> {
203
203
  sortDirection: SortDirection;
204
204
  sameSortOrder: boolean;
205
205
  }) => void;
206
- setColumnVisibility: (columnProperties: RowProperties<R> | RowProperties<R>[], visible: boolean) => void;
207
- setColumnVisibilityWatch: (columnProperties: RowProperties<R> | RowProperties<R>[], getVisible: () => boolean) => void;
206
+ setColumnVisibility: (columnProperties: (`_${string}` | RowProperties<R>) | (`_${string}` | RowProperties<R>)[], visible: boolean) => void;
207
+ setColumnVisibilityWatch: (columnProperties: (`_${string}` | RowProperties<R>) | (`_${string}` | RowProperties<R>)[], getVisible: () => boolean) => void;
208
208
  };
209
209
  }
210
210
  interface $$IsomorphicComponent {
package/dist/Td.svelte CHANGED
@@ -27,6 +27,7 @@
27
27
  enterGoesDown?: boolean | ((ctx: { event: KeyboardEvent; tr: HTMLTableRowElement }) => void)
28
28
  /** If enabled, keypress (enter only) and click events on the Td's contents will have `stopPropagation`/`preventDefault` called on them. */
29
29
  stopPropagation?: boolean
30
+ tagName?: 'TD' | 'TH'
30
31
  children?: Snippet
31
32
  }
32
33
 
@@ -37,6 +38,7 @@
37
38
  align = $bindable(columnInfo.current[property]?.align),
38
39
  enterGoesDown = false,
39
40
  stopPropagation = false,
41
+ tagName = 'TD',
40
42
  children,
41
43
  ...rest
42
44
  }: Props = $props()
@@ -104,7 +106,8 @@
104
106
  </script>
105
107
 
106
108
  {#if visible}
107
- <td
109
+ <svelte:element
110
+ this={tagName}
108
111
  class={[classNames, alignClass]}
109
112
  {...rest}
110
113
  style:font-variant-numeric={thisColumnInfo?.numeric ? 'tabular-nums' : undefined}
@@ -126,7 +129,7 @@
126
129
  {:else}
127
130
  {@render children?.()}
128
131
  {/if}
129
- </td>
132
+ </svelte:element>
130
133
  {/if}
131
134
 
132
135
  <style>
@@ -20,6 +20,7 @@ interface Props extends Omit<HTMLTdAttributes, 'align'> {
20
20
  }) => void);
21
21
  /** If enabled, keypress (enter only) and click events on the Td's contents will have `stopPropagation`/`preventDefault` called on them. */
22
22
  stopPropagation?: boolean;
23
+ tagName?: 'TD' | 'TH';
23
24
  children?: Snippet;
24
25
  }
25
26
  declare const Td: import("svelte").Component<Props, {}, "align">;
@@ -1,4 +1,4 @@
1
- import type { ColumnInfoObject, Column, UuidRowProps, ColumnInfo as GenericColumnInfo, RowProperties } from './types';
1
+ import type { ColumnInfoObject, Column, UuidRowProps, ColumnInfo as GenericColumnInfo, RowProperties, ArbitraryProperty } from './types';
2
2
  type StoreOptions = {
3
3
  /** The key to use in localstorage */
4
4
  key?: string;
@@ -44,9 +44,9 @@ export declare class ColumnInfoRunicStore<R extends UuidRowProps> {
44
44
  iconLeft?: boolean | undefined;
45
45
  iconPrefix?: import("@fortawesome/fontawesome-common-types").IconPrefix | undefined;
46
46
  minWidth?: string | undefined;
47
- name: string;
47
+ name?: string | undefined;
48
48
  numeric?: boolean | undefined;
49
- property: RowProperties<R>;
49
+ property: `_${string}` | RowProperties<R>;
50
50
  sortType?: "ALPHA_NUM" | "STANDARD" | false | undefined;
51
51
  title?: string | undefined;
52
52
  unhidable?: boolean | undefined;
@@ -56,9 +56,9 @@ export declare class ColumnInfoRunicStore<R extends UuidRowProps> {
56
56
  visible: boolean;
57
57
  }[];
58
58
  get current(): ColumnInfoObject<R>;
59
- updateColumn(property: RowProperties<R>, value: Partial<GenericColumnInfo<R>>): void;
59
+ updateColumn(property: RowProperties<R> | ArbitraryProperty, value: Partial<GenericColumnInfo<R>>): void;
60
60
  updateColumns(newColumnInfos: Array<Partial<GenericColumnInfo<R>> & {
61
- property: RowProperties<R>;
61
+ property: RowProperties<R> | ArbitraryProperty;
62
62
  }>): void;
63
63
  private getStoreValFromColumns;
64
64
  /** Takes values from the given column, removes non-serializable values, and fills in with default serializable values */
@@ -61,7 +61,7 @@ export class ColumnInfoRunicStore {
61
61
  console.error(`Column ${col.property} not found in column info store`);
62
62
  return;
63
63
  }
64
- // This gets run for every column in the table on render, so use Object.assign as it's faster
64
+ // This gets run for every column in the table on render, so use Object.assign as it's faster than spreading
65
65
  Object.assign(this.#value[col.property], col);
66
66
  });
67
67
  this.#lastValue = this.#value;
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { default as TreeRow } from './TreeRow.svelte';
6
6
  export { default as DraggableRows } from './DraggableRows.svelte';
7
7
  export * from './column-info-store.svelte';
8
8
  export * from './tree-utility.js';
9
+ export * from './types.js';
9
10
  export type * from './Pagination.svelte';
10
11
  export type * from './types';
11
12
  export type * from './bracket-paths';
package/dist/index.js CHANGED
@@ -6,3 +6,4 @@ export { default as TreeRow } from './TreeRow.svelte';
6
6
  export { default as DraggableRows } from './DraggableRows.svelte';
7
7
  export * from './column-info-store.svelte';
8
8
  export * from './tree-utility.js';
9
+ export * from './types.js';
package/dist/types.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type { IconName, IconPrefix } from '@fortawesome/fontawesome-common-types';
2
2
  import type { BracketPaths } from './bracket-paths';
3
3
  import type { Get, Simplify } from 'type-fest';
4
+ export declare const ARBITRARY_PROPERTY_PREFIX = "_";
5
+ export type ArbitraryProperty = `${typeof ARBITRARY_PROPERTY_PREFIX}${string}`;
4
6
  export type SortDirection = 'ASC' | 'DESC';
5
7
  /** String untion of all properties of the row object, or just `string` as a fallback.
6
8
  *
@@ -49,9 +51,9 @@ export type Column<R extends UuidRowProps = any> = {
49
51
  iconLeft?: boolean;
50
52
  iconPrefix?: IconPrefix;
51
53
  minWidth?: string;
52
- name: string;
54
+ name?: string;
53
55
  numeric?: boolean;
54
- property: RowProperties<R>;
56
+ property: RowProperties<R> | ArbitraryProperty;
55
57
  sortType?: 'ALPHA_NUM' | 'STANDARD' | false;
56
58
  title?: string;
57
59
  unhidable?: boolean;
@@ -60,7 +62,7 @@ export type Column<R extends UuidRowProps = any> = {
60
62
  width?: string;
61
63
  wrap?: boolean;
62
64
  };
63
- export type FooterReducerValue<R extends UuidRowProps> = string | number | Get<R, RowProperties<R>>;
65
+ export type FooterReducerValue<R extends UuidRowProps> = string | number | Get<R, RowProperties<R> | ArbitraryProperty>;
64
66
  export type ColumnInfo<R extends UuidRowProps> = Simplify<Column<R> & {
65
67
  visible: boolean;
66
68
  }>;
package/dist/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export const ARBITRARY_PROPERTY_PREFIX = '_';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoftdata/svelte-table",
3
- "version": "2.6.7",
3
+ "version": "2.8.0",
4
4
  "exports": {
5
5
  ".": {
6
6
  "types": "./dist/index.d.ts",