-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathtemplate.js
More file actions
153 lines (145 loc) · 4.39 KB
/
template.js
File metadata and controls
153 lines (145 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Template as Calendar } from "@spectrum-css/calendar/stories/template.js";
import { Template as PickerButton } from "@spectrum-css/pickerbutton/stories/template.js";
import { Template as Popover } from "@spectrum-css/popover/stories/template.js";
import { Container, getRandomId } from "@spectrum-css/preview/decorators";
import { Template as TextField } from "@spectrum-css/textfield/stories/template.js";
import { html } from "lit";
import { when } from "lit-html/directives/when.js";
import { classMap } from "lit/directives/class-map.js";
import { ifDefined } from "lit/directives/if-defined.js";
import "../index.css";
export const DatePicker = ({
rootClass = "spectrum-DatePicker",
id = getRandomId("datepicker"),
customClasses = [],
isOpen = true,
isInvalid = false,
isValid = false,
isQuiet = false,
isRange = false,
isDateTimeRange = false,
isDisabled = false,
isRequired = false,
isReadOnly = false,
selectedDay,
lastDay,
} = {}, context = {}) => {
const { globals = {}, updateArgs } = context;
const lang = globals.lang ?? "en-US";
const triggerId = getRandomId("datepicker-trigger");
return html`
<div
class=${classMap({
[rootClass]: true,
"is-open": !isDisabled && isOpen,
[`${rootClass}--quiet`]: isQuiet,
[`${rootClass}--range`]: isRange,
[`${rootClass}--datetimeRange`]: isDateTimeRange,
"is-invalid": !isDisabled && isInvalid,
"is-valid": !isDisabled && isValid,
"is-disabled": isDisabled,
...customClasses.reduce((a, c) => ({ ...a, [c]: true }), {}),
})}
id=${ifDefined(id)}
aria-disabled=${isDisabled ? "true" : "false"}
aria-invalid=${ifDefined(isInvalid && !isDisabled ? "false" : undefined)}
aria-readonly=${ifDefined(isReadOnly ? "true" : "false")}
aria-required=${ifDefined(isRequired ? "true" : "false")}
aria-haspopup="dialog"
>
${TextField({
size: "m",
isQuiet,
isDisabled,
isReadOnly,
isInvalid: !isRange ? isInvalid : undefined,
customClasses: [`${rootClass}-textfield`],
customInputClasses: isRange ? [`${rootClass}-input`, `${rootClass}-startField`] : [`${rootClass}-input`],
placeholder: "Choose a date",
name: "field",
id: triggerId,
value: selectedDay ? new Date(selectedDay).toLocaleDateString(lang) : undefined,
onclick: function () {
if (!isOpen) updateArgs({ isOpen: true });
},
}, context)}
${when(isRange, () => html`<div class=${rootClass}-rangeDash></div>`)}
${when(isRange, () => TextField({
size: "m",
isQuiet,
isDisabled,
isReadOnly,
isInvalid,
customClasses: [`${rootClass}-textfield`],
customInputClasses: [`${rootClass}-input`, `${rootClass}-endField`],
placeholder: "Choose a date",
name: "field",
value: lastDay ? new Date(lastDay).toLocaleDateString(lang) : undefined,
onclick: function () {
if (!isOpen) updateArgs({ isOpen: true });
},
}, context))}
${PickerButton({
customClasses: [`${rootClass}-button`, "spectrum-PickerButton--workflowicon"],
size: "m",
iconName: "Calendar",
iconSet: "workflow",
isQuiet,
customStyles: isReadOnly ? { "display": "none" } : {},
// @todo this is not added to the button on the website; need to make sure it's not a bug
// isOpen,
isInvalid,
isDisabled,
position: "right",
onclick: function () {
updateArgs({ isOpen: !isOpen });
},
}, context)}
</div>
`;
};
export const Template = ({
isOpen = true,
isQuiet = false,
isDisabled = false,
isReadOnly = false,
...args
} = {}, context = {}) => {
return html`
${Popover({
isOpen: isOpen && !isDisabled && !isReadOnly,
withTip: false,
position: "bottom-start",
isQuiet,
trigger: (passthroughs) => DatePicker({
...passthroughs,
isOpen,
isQuiet,
isDisabled,
isReadOnly,
...args,
}, context),
content: [
(passthroughs) => Calendar(passthroughs, context)
],
// @todo this implementation of calendar does not currently display range selections or selected date on first load
}, context)}
`;
};
/* Displays open and closed date pickers. */
export const OpenClosedTemplate = (args, context) => Container({
withBorder: false,
wrapperStyles: { "column-gap": "56px", },
content: html`
${Container({
withBorder: false,
heading: "Open",
content: Template(args, context),
})}
${Container({
withBorder: false,
heading: "Closed",
content: Template({...args, isOpen: false}, context),
})}
`
});