compote-ui 0.42.0 → 0.42.2

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.
@@ -1,24 +1,30 @@
1
- function urlDefaults(options) {
2
- if (options.type !== 'url')
1
+ const TYPE_DEFAULTS = {
2
+ number: { align: 'right' },
3
+ currency: { align: 'right' },
4
+ percent: { align: 'right' },
5
+ date: { align: 'center', size: 110 },
6
+ time: { align: 'center', size: 80 },
7
+ 'date-time': { align: 'center', size: 160 },
8
+ boolean: { align: 'center', size: 90 },
9
+ url: { align: 'center', size: 60, enableSorting: false }
10
+ };
11
+ function applyTypeDefaults(options) {
12
+ const defaults = options.type ? TYPE_DEFAULTS[options.type] : undefined;
13
+ if (!defaults)
3
14
  return options;
4
- return {
5
- align: 'center',
6
- enableSorting: false,
7
- size: 60,
8
- ...options
9
- };
15
+ return { ...defaults, ...options };
10
16
  }
11
17
  export function createDataTableColumnHelper() {
12
18
  return {
13
19
  accessor(accessorKey, options) {
14
20
  return {
15
- ...urlDefaults(options),
21
+ ...applyTypeDefaults(options),
16
22
  accessorKey
17
23
  };
18
24
  },
19
25
  accessorFn(accessorFn, options) {
20
26
  return {
21
- ...urlDefaults(options),
27
+ ...applyTypeDefaults(options),
22
28
  accessorFn
23
29
  };
24
30
  },
@@ -1,4 +1,5 @@
1
1
  import { columnVisibilityFeature, columnResizingFeature, columnSizingFeature, columnFilteringFeature, columnFacetingFeature, columnPinningFeature, columnOrderingFeature, createSortedRowModel, createFilteredRowModel, createFacetedRowModel, createFacetedMinMaxValues, createFacetedUniqueValues, createTable as createTanStackTable, filterFns, renderComponent, renderSnippet, rowSelectionFeature, rowSortingFeature, sortFns, tableFeatures } from '@tanstack/svelte-table';
2
+ import { createAtom } from '@tanstack/svelte-store';
2
3
  import { useLocaleContext } from '@ark-ui/svelte/locale';
3
4
  const dataTableFeatures = tableFeatures({
4
5
  columnVisibilityFeature,
@@ -17,6 +18,13 @@ function oneOfFilterFn(row, columnId, filterValue) {
17
18
  oneOfFilterFn.autoRemove = (val) => !Array.isArray(val) || val.length === 0;
18
19
  export function createTable(options) {
19
20
  const localeCtx = useLocaleContext();
21
+ const initialColumnVisibility = {
22
+ ...createColumnVisibility(options.columns),
23
+ ...options.initialState?.columnVisibility
24
+ };
25
+ const columnVisibilityAtom = options.onColumnVisibilityChange
26
+ ? createAtom(initialColumnVisibility)
27
+ : undefined;
20
28
  const table = createTanStackTable({
21
29
  _features: dataTableFeatures,
22
30
  _rowModels: {
@@ -38,12 +46,10 @@ export function createTable(options) {
38
46
  get columns() {
39
47
  return createColumns(options.columns, localeCtx);
40
48
  },
49
+ ...(columnVisibilityAtom ? { atoms: { columnVisibility: columnVisibilityAtom } } : {}),
41
50
  initialState: {
42
51
  ...options.initialState,
43
- columnVisibility: {
44
- ...createColumnVisibility(options.columns),
45
- ...options.initialState?.columnVisibility
46
- },
52
+ columnVisibility: initialColumnVisibility,
47
53
  columnSizing: {
48
54
  ...createColumnSizing(options.columns),
49
55
  ...options.initialState?.columnSizing
@@ -61,13 +67,16 @@ export function createTable(options) {
61
67
  sorting: state.sorting,
62
68
  columnFilters: state.columnFilters
63
69
  }));
64
- if (options.onColumnVisibilityChange) {
70
+ if (columnVisibilityAtom && options.onColumnVisibilityChange) {
65
71
  const { onColumnVisibilityChange } = options;
66
- const orig = table.setColumnVisibility.bind(table);
67
- table.setColumnVisibility = (updater) => {
68
- orig(updater);
69
- onColumnVisibilityChange(table.store.state.columnVisibility);
70
- };
72
+ let initialized = false;
73
+ columnVisibilityAtom.subscribe((value) => {
74
+ if (!initialized) {
75
+ initialized = true;
76
+ return;
77
+ }
78
+ onColumnVisibilityChange(value);
79
+ });
71
80
  }
72
81
  return table;
73
82
  }
@@ -168,6 +177,17 @@ const TYPE_FORMAT_DEFAULTS = {
168
177
  percent: { style: 'percent' },
169
178
  number: {}
170
179
  };
180
+ const TYPE_DATE_FORMAT_DEFAULTS = {
181
+ date: { day: '2-digit', month: '2-digit', year: 'numeric' },
182
+ time: { hour: '2-digit', minute: '2-digit' },
183
+ 'date-time': {
184
+ day: '2-digit',
185
+ month: '2-digit',
186
+ year: 'numeric',
187
+ hour: '2-digit',
188
+ minute: '2-digit'
189
+ }
190
+ };
171
191
  function getFilterFnForType(type) {
172
192
  switch (type) {
173
193
  case 'number':
@@ -185,14 +205,24 @@ function getFilterFnForType(type) {
185
205
  function applyTypeFormat(column, value, localeCtx) {
186
206
  if (value === null || value === undefined || value === '')
187
207
  return undefined;
188
- const defaults = column.type ? TYPE_FORMAT_DEFAULTS[column.type] : undefined;
189
- if (defaults !== undefined) {
208
+ const numDefaults = column.type ? TYPE_FORMAT_DEFAULTS[column.type] : undefined;
209
+ if (numDefaults !== undefined) {
190
210
  const locale = column.formatLocale ?? localeCtx().locale;
191
211
  return new Intl.NumberFormat(locale, {
192
- ...defaults,
212
+ ...numDefaults,
193
213
  ...column.formatOptions
194
214
  }).format(Number(value));
195
215
  }
216
+ if (column.type === 'date' || column.type === 'time' || column.type === 'date-time') {
217
+ const dateValue = value instanceof Date ? value : new Date(value);
218
+ if (isNaN(dateValue.getTime()))
219
+ return undefined;
220
+ const locale = column.formatLocale ?? localeCtx().locale;
221
+ return new Intl.DateTimeFormat(locale, {
222
+ ...TYPE_DATE_FORMAT_DEFAULTS[column.type],
223
+ ...column.formatOptions
224
+ }).format(dateValue);
225
+ }
196
226
  if (column.type === 'boolean')
197
227
  return value ? 'Yes' : 'No';
198
228
  return value;
@@ -1,7 +1,7 @@
1
1
  import type { CellData, ColumnDef, RowData, TableFeatures } from '@tanstack/svelte-table';
2
2
  import type { Snippet } from 'svelte';
3
3
  export type DataTableAlign = 'left' | 'center' | 'right';
4
- export type DataTableColumnType = 'text' | 'number' | 'currency' | 'percent' | 'boolean' | 'select' | 'url';
4
+ export type DataTableColumnType = 'text' | 'number' | 'currency' | 'percent' | 'boolean' | 'select' | 'url' | 'date' | 'time' | 'date-time';
5
5
  export type DataTableCellRenderer<T extends RowData> = (value: unknown, row: T) => string | number | boolean | null | undefined;
6
6
  export type DataTableCellRenderProps<T extends RowData> = {
7
7
  value: unknown;
@@ -19,7 +19,7 @@ export type DataTableLeafColumnBase<T extends RowData> = DataTableColumnOptions<
19
19
  cellProps?: DataTableCellPropsResolver<T>;
20
20
  cellSnippet?: Snippet<[DataTableCellRenderProps<T>]>;
21
21
  type?: DataTableColumnType;
22
- formatOptions?: Intl.NumberFormatOptions;
22
+ formatOptions?: Intl.NumberFormatOptions | Intl.DateTimeFormatOptions;
23
23
  formatLocale?: string;
24
24
  pinned?: 'left' | 'right';
25
25
  grow?: boolean;
@@ -53,7 +53,7 @@ export type DataTableColumn<T extends RowData> = DataTableLeafColumn<T> | DataTa
53
53
  export type DataTableColumnMeta = {
54
54
  align?: DataTableAlign;
55
55
  type?: DataTableColumnType;
56
- formatOptions?: Intl.NumberFormatOptions;
56
+ formatOptions?: Intl.NumberFormatOptions | Intl.DateTimeFormatOptions;
57
57
  formatLocale?: string;
58
58
  grow?: boolean;
59
59
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compote-ui",
3
- "version": "0.42.0",
3
+ "version": "0.42.2",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
6
  "dev": "vite dev --open",