@onsvisual/svelte-components 1.0.12 → 1.0.13
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.
|
@@ -11,9 +11,20 @@
|
|
|
11
11
|
{ country: "Northern Ireland", mountain: "Slieve Donard", height: 850 }
|
|
12
12
|
];
|
|
13
13
|
const columns = [
|
|
14
|
-
{ key: "country", label: "Country"
|
|
14
|
+
{ key: "country", label: "Country" },
|
|
15
15
|
{ key: "mountain", label: "Highest mountain" },
|
|
16
|
-
{ key: "height", label: "Height in metres",
|
|
16
|
+
{ key: "height", label: "Height in metres", numeric: true }
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const dataAlt = [
|
|
20
|
+
{ measure: "First measure", value: 1 },
|
|
21
|
+
{ measure: "Second measure", value: 2 },
|
|
22
|
+
{ measure: "Third measure", value: null },
|
|
23
|
+
{ measure: "Fourth measure", value: 3 }
|
|
24
|
+
];
|
|
25
|
+
const columnsAlt = [
|
|
26
|
+
{ key: "measure", label: "Measure" },
|
|
27
|
+
{ key: "value", label: "Value", numeric: true }
|
|
17
28
|
];
|
|
18
29
|
|
|
19
30
|
const { Story } = defineMeta({
|
|
@@ -29,4 +40,9 @@
|
|
|
29
40
|
|
|
30
41
|
<Story name="Fixed height with sticky header" args={{ data, columns, height: 200 }} />
|
|
31
42
|
|
|
32
|
-
<Story name="Compact mode" args={{ data, columns, compact: true }} />
|
|
43
|
+
<Story name="Compact mode, sortable" args={{ data, columns, sortable: true, compact: true }} />
|
|
44
|
+
|
|
45
|
+
<Story
|
|
46
|
+
name="Sortable with null values"
|
|
47
|
+
args={{ data: dataAlt, columns: columnsAlt, sortable: true }}
|
|
48
|
+
/>
|
|
@@ -7,10 +7,15 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export let title = "";
|
|
9
9
|
/**
|
|
10
|
-
* Make the
|
|
10
|
+
* Make the table smaller/more compact
|
|
11
11
|
* @type {boolean}
|
|
12
12
|
*/
|
|
13
13
|
export let compact = false;
|
|
14
|
+
/**
|
|
15
|
+
* Make the table sortable
|
|
16
|
+
* @type {boolean}
|
|
17
|
+
*/
|
|
18
|
+
export let sortable = false;
|
|
14
19
|
/**
|
|
15
20
|
* Make the chart responsive (changes layout on narrow screens)
|
|
16
21
|
* @type {boolean}
|
|
@@ -32,21 +37,20 @@
|
|
|
32
37
|
*/
|
|
33
38
|
export let data = [];
|
|
34
39
|
/**
|
|
35
|
-
* Optional metadata for formatting columns. Array of {key, label,
|
|
40
|
+
* Optional metadata for formatting columns. Array of {key, label, numeric?, dp?, formatter?} objects
|
|
36
41
|
* @type {object[]}
|
|
37
42
|
*/
|
|
38
43
|
export let columns =
|
|
39
44
|
Array.isArray(data) && data[0] ? Object.keys(data[0]).map((key) => ({ key, label: key })) : [];
|
|
40
45
|
let _data = [...data];
|
|
41
|
-
let sort = columns.map((
|
|
46
|
+
let sort = columns.map(() => "none");
|
|
42
47
|
/**
|
|
43
48
|
* Optional: Set an additional CSS class for the component
|
|
44
49
|
* @type {string}
|
|
45
50
|
*/
|
|
46
51
|
export let cls = "";
|
|
47
52
|
|
|
48
|
-
$:
|
|
49
|
-
$: formatters = columns.map((d) => formatter(d.dp));
|
|
53
|
+
$: formatters = columns.map((d) => d.formatter || formatter(d.dp));
|
|
50
54
|
</script>
|
|
51
55
|
|
|
52
56
|
<div
|
|
@@ -70,7 +74,7 @@
|
|
|
70
74
|
<thead class="ons-table__head">
|
|
71
75
|
<tr class="ons-table__row">
|
|
72
76
|
{#each columns as col, i}
|
|
73
|
-
{#if
|
|
77
|
+
{#if sortable}
|
|
74
78
|
<th
|
|
75
79
|
scope="col"
|
|
76
80
|
class="ons-table__header"
|
|
@@ -7,6 +7,7 @@ export default class Table extends SvelteComponentTyped<{
|
|
|
7
7
|
data?: object[] | undefined;
|
|
8
8
|
title?: string | undefined;
|
|
9
9
|
compact?: boolean | undefined;
|
|
10
|
+
sortable?: boolean | undefined;
|
|
10
11
|
responsive?: boolean | undefined;
|
|
11
12
|
rowHover?: boolean | undefined;
|
|
12
13
|
columns?: object[] | undefined;
|
|
@@ -25,6 +26,7 @@ declare const __propDef: {
|
|
|
25
26
|
data?: object[] | undefined;
|
|
26
27
|
title?: string | undefined;
|
|
27
28
|
compact?: boolean | undefined;
|
|
29
|
+
sortable?: boolean | undefined;
|
|
28
30
|
responsive?: boolean | undefined;
|
|
29
31
|
rowHover?: boolean | undefined;
|
|
30
32
|
columns?: object[] | undefined;
|
|
@@ -12,7 +12,7 @@ A table component with various customisation options.
|
|
|
12
12
|
];
|
|
13
13
|
const columns = [
|
|
14
14
|
{ key: "col1", label: "Text column" },
|
|
15
|
-
{ key: "col2", label: "Numeric column", numeric: true
|
|
15
|
+
{ key: "col2", label: "Numeric column", numeric: true},
|
|
16
16
|
];
|
|
17
17
|
</script>
|
|
18
18
|
|
package/dist/js/utils.js
CHANGED
|
@@ -56,10 +56,30 @@ export const formatter = (dp = null) => {
|
|
|
56
56
|
};
|
|
57
57
|
|
|
58
58
|
export const ascending = (a, b) =>
|
|
59
|
-
a == null
|
|
59
|
+
a == null && b != null
|
|
60
|
+
? 1
|
|
61
|
+
: b == null && a != null
|
|
62
|
+
? -1
|
|
63
|
+
: a < b
|
|
64
|
+
? -1
|
|
65
|
+
: a > b
|
|
66
|
+
? 1
|
|
67
|
+
: a >= b
|
|
68
|
+
? 0
|
|
69
|
+
: NaN;
|
|
60
70
|
|
|
61
71
|
export const descending = (a, b) =>
|
|
62
|
-
a == null
|
|
72
|
+
a == null && b != null
|
|
73
|
+
? 1
|
|
74
|
+
: b == null && a != null
|
|
75
|
+
? -1
|
|
76
|
+
: b < a
|
|
77
|
+
? -1
|
|
78
|
+
: b > a
|
|
79
|
+
? 1
|
|
80
|
+
: b >= a
|
|
81
|
+
? 0
|
|
82
|
+
: NaN;
|
|
63
83
|
|
|
64
84
|
export const sleep = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
65
85
|
|