compote-ui 0.42.1 → 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
  },
@@ -177,6 +177,17 @@ const TYPE_FORMAT_DEFAULTS = {
177
177
  percent: { style: 'percent' },
178
178
  number: {}
179
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
+ };
180
191
  function getFilterFnForType(type) {
181
192
  switch (type) {
182
193
  case 'number':
@@ -194,14 +205,24 @@ function getFilterFnForType(type) {
194
205
  function applyTypeFormat(column, value, localeCtx) {
195
206
  if (value === null || value === undefined || value === '')
196
207
  return undefined;
197
- const defaults = column.type ? TYPE_FORMAT_DEFAULTS[column.type] : undefined;
198
- if (defaults !== undefined) {
208
+ const numDefaults = column.type ? TYPE_FORMAT_DEFAULTS[column.type] : undefined;
209
+ if (numDefaults !== undefined) {
199
210
  const locale = column.formatLocale ?? localeCtx().locale;
200
211
  return new Intl.NumberFormat(locale, {
201
- ...defaults,
212
+ ...numDefaults,
202
213
  ...column.formatOptions
203
214
  }).format(Number(value));
204
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
+ }
205
226
  if (column.type === 'boolean')
206
227
  return value ? 'Yes' : 'No';
207
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.1",
3
+ "version": "0.42.2",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
6
  "dev": "vite dev --open",