@canonical/svelte-ds-app-launchpad 0.18.1 → 0.20.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/LICENSE +166 -0
- package/dist/components/DateTime/DateTime.ssr.test.d.ts +1 -0
- package/dist/components/DateTime/DateTime.ssr.test.js +94 -0
- package/dist/components/DateTime/DateTime.stories.svelte +34 -0
- package/dist/components/DateTime/DateTime.stories.svelte.d.ts +19 -0
- package/dist/components/DateTime/DateTime.svelte +27 -0
- package/dist/components/DateTime/DateTime.svelte.d.ts +12 -0
- package/dist/components/DateTime/DateTime.svelte.test.d.ts +1 -0
- package/dist/components/DateTime/DateTime.svelte.test.js +81 -0
- package/dist/components/DateTime/index.d.ts +2 -0
- package/dist/components/DateTime/index.js +2 -0
- package/dist/components/DateTime/types.d.ts +16 -0
- package/dist/components/DateTime/types.js +2 -0
- package/dist/components/DateTime/utils/formatters.d.ts +1 -0
- package/dist/components/DateTime/utils/formatters.js +5 -0
- package/dist/components/DescriptionList/DescriptionList.stories.svelte +2 -1
- package/dist/components/RelativeDateTime/RelativeDateTime.ssr.test.d.ts +1 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.ssr.test.js +130 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.stories.svelte +41 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.stories.svelte.d.ts +19 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte +86 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte.d.ts +13 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte.test.d.ts +1 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte.test.js +152 -0
- package/dist/components/RelativeDateTime/index.d.ts +2 -0
- package/dist/components/RelativeDateTime/index.js +2 -0
- package/dist/components/RelativeDateTime/types.d.ts +26 -0
- package/dist/components/RelativeDateTime/types.js +2 -0
- package/dist/components/RelativeDateTime/utils/constants.d.ts +1 -0
- package/dist/components/RelativeDateTime/utils/constants.js +9 -0
- package/dist/components/RelativeDateTime/utils/formatters.d.ts +2 -0
- package/dist/components/RelativeDateTime/utils/formatters.js +9 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.d.ts +15 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.js +21 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.test.d.ts +1 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.test.js +65 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +2 -0
- package/package.json +9 -8
- package/dist/components/Breadcrumbs/__screenshots__/Breadcrumbs.svelte.test.ts/Breadcrumbs-component-accessibility-focus-management-allows-focusing-links-with-tab-key-1.png +0 -0
- package/dist/components/Breadcrumbs/__screenshots__/Breadcrumbs.svelte.test.ts/Breadcrumbs-component-accessibility-focus-management-allows-focusing-links-with-tab-key-when-segments-are-collapsed-1.png +0 -0
- package/dist/components/Checkbox/__screenshots__/Checkbox.svelte.test.ts/Checkbox-component-Checked-state-toggles-on-click-1.png +0 -0
- package/dist/components/Checkbox/__screenshots__/Checkbox.svelte.test.ts/Checkbox-component-Events-emits-change-event-on-click-1.png +0 -0
- package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-apply-custom-class-and-modifiers-1.png +0 -0
- package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-not-render-a-button-when-dismiss-is-provided-1.png +0 -0
- package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-not-render-a-button-when-readonly-is-provided-1.png +0 -0
- package/dist/components/Radio/__screenshots__/Radio.svelte.test.ts/Radio-component-Checked-state-can-be-checked-by-on-click-1.png +0 -0
- package/dist/components/Radio/__screenshots__/Radio.svelte.test.ts/Radio-component-Events-emits-change-event-on-click-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Accessibility-can-be-toggled-with-space-key-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Events-emits-change-event-on-click-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Switch-state-can-be-disabled-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Switch-state-toggles-checked-state-on-click-1.png +0 -0
- package/dist/components/Textarea/utils/__screenshots__/calculateDynamicRows.svelte.test.ts/Textarea---calculateDynamicRows-Swapped-min-max-swaps-min-and-max-when-max-is-smaller-than-min-1.png +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
|
166
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/* @canonical/generator-ds 0.17.1 */
|
|
2
|
+
import { render } from "@canonical/svelte-ssr-test";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import Component from "./DateTime.svelte";
|
|
5
|
+
vi.mock("./utils/formatters.js", () => {
|
|
6
|
+
return {
|
|
7
|
+
defaultDateTimeFormatter: new Intl.DateTimeFormat("en-US", {
|
|
8
|
+
dateStyle: "short",
|
|
9
|
+
timeStyle: "short",
|
|
10
|
+
timeZone: "UTC",
|
|
11
|
+
}),
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
const date = new Date("2024-01-01T12:00:00Z");
|
|
15
|
+
const timestamp = date.getTime();
|
|
16
|
+
const dateString = date.toISOString();
|
|
17
|
+
describe("DateTime SSR", () => {
|
|
18
|
+
const baseProps = {
|
|
19
|
+
date,
|
|
20
|
+
};
|
|
21
|
+
describe("basics", () => {
|
|
22
|
+
it("doesn't throw", () => {
|
|
23
|
+
expect(() => {
|
|
24
|
+
render(Component, { props: { ...baseProps } });
|
|
25
|
+
}).not.toThrow();
|
|
26
|
+
});
|
|
27
|
+
it("renders", () => {
|
|
28
|
+
const page = render(Component, { props: { ...baseProps } });
|
|
29
|
+
expect(componentLocator(page)).toBeInstanceOf(page.window.HTMLTimeElement);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe("Basic attributes", () => {
|
|
33
|
+
it.each([
|
|
34
|
+
["id", "test-id"],
|
|
35
|
+
["aria-label", "test-aria-label"],
|
|
36
|
+
])("applies %s", (attribute, value) => {
|
|
37
|
+
const page = render(Component, {
|
|
38
|
+
props: { ...baseProps, [attribute]: value },
|
|
39
|
+
});
|
|
40
|
+
const element = componentLocator(page);
|
|
41
|
+
expect(element.getAttribute(attribute)).toBe(value);
|
|
42
|
+
});
|
|
43
|
+
it("applies style", () => {
|
|
44
|
+
const page = render(Component, {
|
|
45
|
+
props: { ...baseProps, style: "color: orange;" },
|
|
46
|
+
});
|
|
47
|
+
const element = componentLocator(page);
|
|
48
|
+
expect(element.getAttribute("style")).toContain("color: orange;");
|
|
49
|
+
});
|
|
50
|
+
it("applies class", () => {
|
|
51
|
+
const page = render(Component, {
|
|
52
|
+
props: { ...baseProps, class: "test-class" },
|
|
53
|
+
});
|
|
54
|
+
const element = componentLocator(page);
|
|
55
|
+
expect(element.classList.contains("test-class")).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe("datetime attribute", () => {
|
|
59
|
+
it("applies correct datetime attribute for Date input", () => {
|
|
60
|
+
const page = render(Component, { props: { ...baseProps } });
|
|
61
|
+
const element = componentLocator(page);
|
|
62
|
+
expect(element.getAttribute("datetime")).toBe(date.toISOString());
|
|
63
|
+
});
|
|
64
|
+
it("applies correct datetime attribute for timestamp input", () => {
|
|
65
|
+
const page = render(Component, {
|
|
66
|
+
props: { ...baseProps, date: timestamp },
|
|
67
|
+
});
|
|
68
|
+
const element = componentLocator(page);
|
|
69
|
+
expect(element.getAttribute("datetime")).toBe(date.toISOString());
|
|
70
|
+
});
|
|
71
|
+
it("applies correct datetime attribute for date string input", () => {
|
|
72
|
+
const page = render(Component, {
|
|
73
|
+
props: { ...baseProps, date: dateString },
|
|
74
|
+
});
|
|
75
|
+
const element = componentLocator(page);
|
|
76
|
+
expect(element.getAttribute("datetime")).toBe(date.toISOString());
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
it("accepts custom formatter", () => {
|
|
80
|
+
const page = render(Component, {
|
|
81
|
+
props: {
|
|
82
|
+
...baseProps,
|
|
83
|
+
formatter: {
|
|
84
|
+
format: (date) => `Formatted: ${date.toISOString()}`,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
const element = componentLocator(page);
|
|
89
|
+
expect(element.textContent).toBe(`Formatted: ${date.toISOString()}`);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
function componentLocator(page) {
|
|
93
|
+
return page.getByRole("time");
|
|
94
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import { defineMeta } from "@storybook/addon-svelte-csf";
|
|
3
|
+
import DateTime from "./DateTime.svelte";
|
|
4
|
+
|
|
5
|
+
const { Story } = defineMeta({
|
|
6
|
+
title: "Components/DateTime",
|
|
7
|
+
tags: ["autodocs"],
|
|
8
|
+
component: DateTime,
|
|
9
|
+
args: {
|
|
10
|
+
date: "2023-10-01T12:00:00Z",
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<Story name="Default" />
|
|
16
|
+
|
|
17
|
+
<Story
|
|
18
|
+
name="Custom Intl formatter"
|
|
19
|
+
args={{
|
|
20
|
+
formatter: new Intl.DateTimeFormat("en-GB", {
|
|
21
|
+
dateStyle: "long",
|
|
22
|
+
timeStyle: "long",
|
|
23
|
+
}),
|
|
24
|
+
}}
|
|
25
|
+
/>
|
|
26
|
+
|
|
27
|
+
<Story
|
|
28
|
+
name="Custom formatter function"
|
|
29
|
+
args={{
|
|
30
|
+
formatter: {
|
|
31
|
+
format: (date: Date) => `${date.getTime()} since epoch`,
|
|
32
|
+
},
|
|
33
|
+
}}
|
|
34
|
+
/>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import DateTime from "./DateTime.svelte";
|
|
2
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
3
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
4
|
+
$$bindings?: Bindings;
|
|
5
|
+
} & Exports;
|
|
6
|
+
(internal: unknown, props: {
|
|
7
|
+
$$events?: Events;
|
|
8
|
+
$$slots?: Slots;
|
|
9
|
+
}): Exports & {
|
|
10
|
+
$set?: any;
|
|
11
|
+
$on?: any;
|
|
12
|
+
};
|
|
13
|
+
z_$$bindings?: Bindings;
|
|
14
|
+
}
|
|
15
|
+
declare const DateTime: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
|
16
|
+
[evt: string]: CustomEvent<any>;
|
|
17
|
+
}, {}, {}, string>;
|
|
18
|
+
type DateTime = InstanceType<typeof DateTime>;
|
|
19
|
+
export default DateTime;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<!-- @canonical/generator-ds 0.17.1 -->
|
|
2
|
+
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
import type { DateTimeProps } from "./types.js";
|
|
5
|
+
import { defaultDateTimeFormatter } from "./utils/formatters.js";
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
date: dateProp,
|
|
9
|
+
formatter = defaultDateTimeFormatter,
|
|
10
|
+
...rest
|
|
11
|
+
}: DateTimeProps = $props();
|
|
12
|
+
|
|
13
|
+
const date = $derived(new Date(dateProp));
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<time datetime={date.toISOString()} {...rest}>
|
|
17
|
+
{formatter.format(date)}
|
|
18
|
+
</time>
|
|
19
|
+
|
|
20
|
+
<!-- @component
|
|
21
|
+
`DateTime` component formats and displays dates and times.
|
|
22
|
+
|
|
23
|
+
```svelte
|
|
24
|
+
<DateTime date={new Date()} />
|
|
25
|
+
<DateTime date="2023-10-01T12:00:00Z" />
|
|
26
|
+
```
|
|
27
|
+
-->
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DateTimeProps } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* `DateTime` component formats and displays dates and times.
|
|
4
|
+
*
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <DateTime date={new Date()} />
|
|
7
|
+
* <DateTime date="2023-10-01T12:00:00Z" />
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
declare const DateTime: import("svelte").Component<DateTimeProps, {}, "">;
|
|
11
|
+
type DateTime = ReturnType<typeof DateTime>;
|
|
12
|
+
export default DateTime;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* @canonical/generator-ds 0.17.1 */
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { render } from "vitest-browser-svelte";
|
|
4
|
+
import Component from "./DateTime.svelte";
|
|
5
|
+
vi.mock("./utils/formatters.js", () => {
|
|
6
|
+
return {
|
|
7
|
+
defaultDateTimeFormatter: new Intl.DateTimeFormat("en-US", {
|
|
8
|
+
dateStyle: "short",
|
|
9
|
+
timeStyle: "short",
|
|
10
|
+
timeZone: "UTC",
|
|
11
|
+
}),
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
const date = new Date("2024-01-01T12:00:00Z");
|
|
15
|
+
const timestamp = date.getTime();
|
|
16
|
+
const dateString = date.toISOString();
|
|
17
|
+
describe("DateTime component", () => {
|
|
18
|
+
const baseProps = {
|
|
19
|
+
date,
|
|
20
|
+
};
|
|
21
|
+
it("renders", async () => {
|
|
22
|
+
const page = render(Component, baseProps);
|
|
23
|
+
await expect.element(componentLocator(page)).toBeInTheDocument();
|
|
24
|
+
});
|
|
25
|
+
describe("basic attributes", () => {
|
|
26
|
+
it.each([
|
|
27
|
+
["id", "test-id"],
|
|
28
|
+
["aria-label", "test-aria-label"],
|
|
29
|
+
])("applies %s", async (attribute, value) => {
|
|
30
|
+
const page = render(Component, { ...baseProps, [attribute]: value });
|
|
31
|
+
await expect
|
|
32
|
+
.element(componentLocator(page))
|
|
33
|
+
.toHaveAttribute(attribute, value);
|
|
34
|
+
});
|
|
35
|
+
it("applies style", async () => {
|
|
36
|
+
const page = render(Component, { ...baseProps, style: "color: orange;" });
|
|
37
|
+
await expect
|
|
38
|
+
.element(componentLocator(page))
|
|
39
|
+
.toHaveStyle("color: orange;");
|
|
40
|
+
});
|
|
41
|
+
it("applies class", async () => {
|
|
42
|
+
const page = render(Component, { ...baseProps, class: "test-class" });
|
|
43
|
+
const element = componentLocator(page);
|
|
44
|
+
await expect.element(element).toHaveClass("test-class");
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
describe("datetime attribute", () => {
|
|
48
|
+
it("applies correct datetime attribute for Date input", async () => {
|
|
49
|
+
const page = render(Component, baseProps);
|
|
50
|
+
await expect
|
|
51
|
+
.element(componentLocator(page))
|
|
52
|
+
.toHaveAttribute("datetime", date.toISOString());
|
|
53
|
+
});
|
|
54
|
+
it("applies correct datetime attribute for timestamp input", async () => {
|
|
55
|
+
const page = render(Component, { ...baseProps, date: timestamp });
|
|
56
|
+
await expect
|
|
57
|
+
.element(componentLocator(page))
|
|
58
|
+
.toHaveAttribute("datetime", date.toISOString());
|
|
59
|
+
});
|
|
60
|
+
it("applies correct datetime attribute for date string input", async () => {
|
|
61
|
+
const page = render(Component, { ...baseProps, date: dateString });
|
|
62
|
+
await expect
|
|
63
|
+
.element(componentLocator(page))
|
|
64
|
+
.toHaveAttribute("datetime", date.toISOString());
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
it("accepts custom formatter", async () => {
|
|
68
|
+
const page = render(Component, {
|
|
69
|
+
...baseProps,
|
|
70
|
+
formatter: {
|
|
71
|
+
format: (date) => `Formatted: ${date.toISOString()}`,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
await expect
|
|
75
|
+
.element(componentLocator(page))
|
|
76
|
+
.toHaveTextContent(`Formatted: ${date.toISOString()}`);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
function componentLocator(page) {
|
|
80
|
+
return page.getByRole("time");
|
|
81
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SvelteHTMLElements } from "svelte/elements";
|
|
2
|
+
type BaseProps = Omit<SvelteHTMLElements["time"], "children">;
|
|
3
|
+
export interface DateTimeProps extends BaseProps {
|
|
4
|
+
/**
|
|
5
|
+
* The moment in time to display.
|
|
6
|
+
* This can be a Date object, a timestamp, or a date string.
|
|
7
|
+
*/
|
|
8
|
+
date: Date | string | number;
|
|
9
|
+
/**
|
|
10
|
+
* Custom formatter to use.
|
|
11
|
+
*/
|
|
12
|
+
formatter?: {
|
|
13
|
+
format: (date: Date) => string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const defaultDateTimeFormatter: Intl.DateTimeFormat;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { defineMeta } from "@storybook/addon-svelte-csf";
|
|
3
3
|
import type { ComponentProps } from "svelte";
|
|
4
4
|
import { Chip } from "../Chip/index.js";
|
|
5
|
+
import { DateTime } from "../DateTime/index.js";
|
|
5
6
|
import { Link } from "../Link/index.js";
|
|
6
7
|
import { DescriptionList } from "./index.js";
|
|
7
8
|
|
|
@@ -17,7 +18,7 @@
|
|
|
17
18
|
<DescriptionList.Item name="ID">134</DescriptionList.Item>
|
|
18
19
|
<DescriptionList.Item name="Requested by">Jane Doe</DescriptionList.Item>
|
|
19
20
|
<DescriptionList.Item name="Created">
|
|
20
|
-
2023-10-01T12:00:00Z
|
|
21
|
+
<DateTime date="2023-10-01T12:00:00Z" />
|
|
21
22
|
</DescriptionList.Item>
|
|
22
23
|
<DescriptionList.Item name="A very long term name to test layout behavior">
|
|
23
24
|
This is a description for a term with a very long name to see how the
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/* @canonical/generator-ds 0.17.1 */
|
|
2
|
+
import { render } from "@canonical/svelte-ssr-test";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import Component from "./RelativeDateTime.svelte";
|
|
5
|
+
vi.mock("./utils/formatters.js", () => {
|
|
6
|
+
return {
|
|
7
|
+
dateTimeFormatter: new Intl.DateTimeFormat("en-US", {
|
|
8
|
+
dateStyle: "short",
|
|
9
|
+
timeStyle: "short",
|
|
10
|
+
timeZone: "UTC",
|
|
11
|
+
}),
|
|
12
|
+
relativeTimeFormatter: new Intl.RelativeTimeFormat("en-US", {
|
|
13
|
+
numeric: "auto",
|
|
14
|
+
style: "long",
|
|
15
|
+
}),
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
const date = new Date("2024-01-01T12:00:00Z");
|
|
19
|
+
const timestamp = date.getTime();
|
|
20
|
+
const dateString = date.toISOString();
|
|
21
|
+
describe("RelativeDateTime SSR", () => {
|
|
22
|
+
const baseProps = {
|
|
23
|
+
date,
|
|
24
|
+
};
|
|
25
|
+
describe("basics", () => {
|
|
26
|
+
it("doesn't throw", () => {
|
|
27
|
+
expect(() => {
|
|
28
|
+
render(Component, { props: { ...baseProps } });
|
|
29
|
+
}).not.toThrow();
|
|
30
|
+
});
|
|
31
|
+
it("renders", () => {
|
|
32
|
+
const page = render(Component, { props: { ...baseProps } });
|
|
33
|
+
expect(componentLocator(page)).toBeInstanceOf(page.window.HTMLTimeElement);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
describe("Basic attributes", () => {
|
|
37
|
+
it.each([
|
|
38
|
+
["id", "test-id"],
|
|
39
|
+
["aria-label", "test-aria-label"],
|
|
40
|
+
])("applies %s", (attribute, value) => {
|
|
41
|
+
const page = render(Component, {
|
|
42
|
+
props: { ...baseProps, [attribute]: value },
|
|
43
|
+
});
|
|
44
|
+
const element = componentLocator(page);
|
|
45
|
+
expect(element.getAttribute(attribute)).toBe(value);
|
|
46
|
+
});
|
|
47
|
+
it("applies style", () => {
|
|
48
|
+
const page = render(Component, {
|
|
49
|
+
props: { ...baseProps, style: "color: orange;" },
|
|
50
|
+
});
|
|
51
|
+
const element = componentLocator(page);
|
|
52
|
+
expect(element.getAttribute("style")).toContain("color: orange;");
|
|
53
|
+
});
|
|
54
|
+
it("applies class", () => {
|
|
55
|
+
const page = render(Component, {
|
|
56
|
+
props: { ...baseProps, class: "test-class" },
|
|
57
|
+
});
|
|
58
|
+
const element = componentLocator(page);
|
|
59
|
+
expect(element.classList.contains("test-class")).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("datetime attribute", () => {
|
|
63
|
+
it("applies correct datetime attribute for Date input", () => {
|
|
64
|
+
const page = render(Component, { props: { ...baseProps } });
|
|
65
|
+
const element = componentLocator(page);
|
|
66
|
+
expect(element.getAttribute("datetime")).toBe(date.toISOString());
|
|
67
|
+
});
|
|
68
|
+
it("applies correct datetime attribute for timestamp input", () => {
|
|
69
|
+
const page = render(Component, {
|
|
70
|
+
props: { ...baseProps, date: timestamp },
|
|
71
|
+
});
|
|
72
|
+
const element = componentLocator(page);
|
|
73
|
+
expect(element.getAttribute("datetime")).toBe(date.toISOString());
|
|
74
|
+
});
|
|
75
|
+
it("applies correct datetime attribute for date string input", () => {
|
|
76
|
+
const page = render(Component, {
|
|
77
|
+
props: { ...baseProps, date: dateString },
|
|
78
|
+
});
|
|
79
|
+
const element = componentLocator(page);
|
|
80
|
+
expect(element.getAttribute("datetime")).toBe(date.toISOString());
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
describe("Content", () => {
|
|
84
|
+
describe("now label", () => {
|
|
85
|
+
it("renders nowLabel when within nowThreshold", () => {
|
|
86
|
+
const now = Date.now();
|
|
87
|
+
const page = render(Component, {
|
|
88
|
+
props: { ...baseProps, date: now, nowThreshold: 999999 },
|
|
89
|
+
});
|
|
90
|
+
expect(componentLocator(page).textContent).toContain("now");
|
|
91
|
+
});
|
|
92
|
+
it("does not render nowLabel when outside nowThreshold", () => {
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
const page = render(Component, {
|
|
95
|
+
props: { ...baseProps, date: now - 1000, nowThreshold: 10 },
|
|
96
|
+
});
|
|
97
|
+
expect(componentLocator(page).textContent).not.toContain("now");
|
|
98
|
+
});
|
|
99
|
+
it("renders custom nowLabel when within nowThreshold", () => {
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
const page = render(Component, {
|
|
102
|
+
props: {
|
|
103
|
+
...baseProps,
|
|
104
|
+
date: now,
|
|
105
|
+
nowThreshold: 999999,
|
|
106
|
+
nowLabel: "just now",
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
expect(componentLocator(page).textContent).toContain("just now");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
it("renders relative time when outside nowThreshold", () => {
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
const pastDate = new Date(now - 1000 * 60 * 60 * 3); // 3 hours ago
|
|
115
|
+
const page = render(Component, {
|
|
116
|
+
props: { ...baseProps, date: pastDate, nowThreshold: 0 },
|
|
117
|
+
});
|
|
118
|
+
expect(componentLocator(page).textContent).toContain("3 hours ago");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
it("applies formatted date as title", () => {
|
|
122
|
+
const page = render(Component, {
|
|
123
|
+
props: { ...baseProps },
|
|
124
|
+
});
|
|
125
|
+
expect(componentLocator(page).getAttribute("title")).toBe("1/1/24, 12:00 PM");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
function componentLocator(page) {
|
|
129
|
+
return page.getByRole("time");
|
|
130
|
+
}
|