-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathtemplate.js
167 lines (162 loc) · 3.87 KB
/
template.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Template as FieldLabel } from "@spectrum-css/fieldlabel/stories/template.js";
import { Template as HelpText } from "@spectrum-css/helptext/stories/template.js";
import { Template as Icon } from "@spectrum-css/icon/stories/template.js";
import { Template as Popover } from "@spectrum-css/popover/stories/template.js";
import { getRandomId } from "@spectrum-css/preview/decorators";
import { Template as ProgressCircle } from "@spectrum-css/progresscircle/stories/template.js";
import { Template as Switch } from "@spectrum-css/switch/stories/template.js";
import { html } from "lit";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import { when } from "lit/directives/when.js";
import "../index.css";
export const Picker = ({
rootClass = "spectrum-Picker",
size = "m",
labelPosition,
placeholder,
isQuiet = false,
isKeyboardFocused = false,
isOpen = false,
isInvalid = false,
isLoading = false,
isDisabled = false,
customClasses = [],
customStyles = {},
onclick,
menuHeight = false,
} = {}, context = {}) => {
return html`
<button
class=${classMap({
[rootClass]: true,
[`${rootClass}--size${size?.toUpperCase()}`]:
typeof size !== "undefined",
[`${rootClass}--quiet`]: isQuiet,
[`${rootClass}--sideLabel`]: labelPosition != "top",
["is-invalid"]: isInvalid,
["is-open"]: isOpen,
["is-loading"]: isLoading,
["is-keyboardFocused"]: isKeyboardFocused,
[`${rootClass}--menu-height-fixed`]: menuHeight,
...customClasses.reduce((a, c) => ({ ...a, [c]: true }), {}),
})}
?disabled=${isDisabled}
aria-haspopup="listbox"
style=${styleMap(customStyles)}
type="button"
@click=${onclick}
>
<span class="${rootClass}-label is-placeholder">${placeholder}</span>
${when(isLoading, () =>
ProgressCircle({
size: "s",
isIndeterminate: true,
}, context)
)}
${when(isInvalid && !isLoading, () =>
Icon({
size,
iconName: "Alert",
customClasses: [`${rootClass}-validationIcon`],
}, context)
)}
${Icon({
size,
setName: "ui",
iconName: "ChevronDown",
customClasses: [`${rootClass}-menuIcon`],
}, context)}
</button>
`;
};
export const Template = ({
rootClass = "spectrum-Picker",
size = "m",
label,
labelPosition = "top",
placeholder,
helpText,
isQuiet = false,
isKeyboardFocused = false,
isOpen = false,
isInvalid = false,
isLoading = false,
isDisabled = false,
isReadOnly = false,
withSwitch = false,
fieldLabelStyle = {},
customClasses = [],
content = [],
menuHeight = false,
id = getRandomId("picker"),
} = {}, context = {}) => {
const { updateArgs } = context;
let iconName = "ChevronDown200";
switch (size) {
case "s":
iconName = "ChevronDown75";
break;
case "m":
iconName = "ChevronDown100";
break;
case "xl":
iconName = "ChevronDown300";
break;
default:
iconName = "ChevronDown200";
}
return html`
${when(label, () =>
FieldLabel({
size,
label,
isDisabled,
customStyles: fieldLabelStyle,
alignment: labelPosition,
}, context)
)}
${Popover({
isOpen: isOpen && !isDisabled,
withTip: false,
position: "bottom-start",
trigger: (passthroughs, context) => Picker({
...passthroughs,
rootClass,
size,
placeholder,
isQuiet,
isKeyboardFocused,
isOpen,
isInvalid,
isLoading,
isDisabled,
isReadOnly,
customClasses,
content,
iconName,
labelPosition,
menuHeight,
id,
onclick: function() {
updateArgs({ isOpen: !isOpen });
},
}, context),
content,
}, context)}
${when(helpText, () =>
HelpText({
text: helpText,
variant: isInvalid ? "negative" : "neutral",
hideIcon: true,
}, context)
)}
${when(withSwitch, () => Switch({
size,
label: "Toggle switch",
customStyles: {
"padding-inline-start": "15px"
}
}, context))}
`;
};