-
Notifications
You must be signed in to change notification settings - Fork 75
/
input-time-zone.tsx
335 lines (285 loc) · 9.6 KB
/
input-time-zone.tsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import {
Component,
Element,
Event,
EventEmitter,
h,
Host,
Method,
Prop,
State,
VNode,
Watch,
} from "@stencil/core";
import { connectLabel, disconnectLabel, LabelableComponent } from "../../utils/label";
import { InteractiveComponent, updateHostInteraction } from "../../utils/interactive";
import {
connectLocalized,
disconnectLocalized,
LocalizedComponent,
SupportedLocale,
} from "../../utils/locale";
import { BasicTimeZoneGroup } from "./interfaces";
import { Scale } from "../interfaces";
import {
connectMessages,
disconnectMessages,
setUpMessages,
T9nComponent,
updateMessages,
} from "../../utils/t9n";
import { InputTimeZoneMessages } from "./assets/input-time-zone/t9n";
import { generateTimeZoneGroups, getUserTimeZoneOffset } from "./utils";
import { OverlayPositioning } from "../../utils/floating-ui";
import {
componentFocusable,
LoadableComponent,
setComponentLoaded,
setUpLoadableComponent,
} from "../../utils/loadable";
import {
afterConnectDefaultValueSet,
connectForm,
disconnectForm,
FormComponent,
HiddenFormInputSlot,
} from "../../utils/form";
@Component({
tag: "calcite-input-time-zone",
styleUrl: "input-time-zone.scss",
assetsDirs: ["assets"],
shadow: {
delegatesFocus: true,
},
})
export class InputTimeZone
implements
FormComponent,
InteractiveComponent,
LabelableComponent,
LoadableComponent,
LocalizedComponent,
T9nComponent
{
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* When `true`, interaction is prevented and the component is displayed with lower opacity.
*/
@Prop({ reflect: true }) disabled = false;
/**
* The ID of the form that will be associated with the component.
*
* When not set, the component will be associated with its ancestor form element, if any.
*/
@Prop({ reflect: true }) form: string;
/**
* Made into a prop for testing purposes only
*
* @internal
*/
// eslint-disable-next-line @stencil-community/strict-mutable -- updated by t9n module
@Prop({ mutable: true }) messages: InputTimeZoneMessages;
/**
* Use this property to override individual strings used by the component.
*/
// eslint-disable-next-line @stencil-community/strict-mutable -- updated by t9n module
@Prop({ mutable: true }) messageOverrides: Partial<InputTimeZoneMessages>;
@Watch("messageOverrides")
onMessagesChange(): void {
/* wired up by t9n util */
}
/**
* Specifies the name of the component.
*
* Required to pass the component's `value` on form submission.
*/
@Prop({ reflect: true }) name: string;
/** When `true`, displays and positions the component. */
@Prop({ mutable: true, reflect: true }) open = false;
/**
* Determines the type of positioning to use for the overlaid content.
*
* Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
*
* `"fixed"` should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
*
*/
@Prop({ reflect: true }) overlayPositioning: OverlayPositioning = "absolute";
/**
* When `true`, the component must have a value in order for the form to submit.
*
* @internal
*/
@Prop({ reflect: true }) required = false;
/** Specifies the size of the component. */
@Prop({ reflect: true }) scale: Scale = "m";
/**
* The component's value, where the value is the time zone offset or the difference, in minutes, between the selected time zone and UTC.
*
* If no value is provided, the user's time zone offset will be selected by default.
*
* @see https://www.w3.org/International/core/2005/09/timezone.html#:~:text=What%20is%20a%20%22zone%20offset,or%20%22%2D%22%20from%20UTC.
*/
@Prop({ mutable: true }) value: string;
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
@Method()
async setFocus(): Promise<void> {
await componentFocusable(this);
await this.comboboxEl.setFocus();
}
//--------------------------------------------------------------------------
//
// Events
//
//--------------------------------------------------------------------------
@Event({ cancelable: false }) calciteInputTimeZoneBeforeClose: EventEmitter<void>;
@Event({ cancelable: false }) calciteInputTimeZoneBeforeOpen: EventEmitter<void>;
@Event({ cancelable: false }) calciteInputTimeZoneChange: EventEmitter<void>;
@Event({ cancelable: false }) calciteInputTimeZoneClose: EventEmitter<void>;
@Event({ cancelable: false }) calciteInputTimeZoneOpen: EventEmitter<void>;
//--------------------------------------------------------------------------
//
// Private Properties
//
//--------------------------------------------------------------------------
@Element() el: HTMLCalciteInputTimeZoneElement;
private comboboxEl: HTMLCalciteComboboxElement;
@State() defaultMessages: InputTimeZoneMessages;
defaultValue: InputTimeZone["value"];
@State() effectiveLocale: SupportedLocale = "";
@Watch("effectiveLocale")
effectiveLocaleWatcher(): void {
updateMessages(this, this.effectiveLocale);
}
formEl: HTMLFormElement;
labelEl: HTMLCalciteLabelElement;
private selectedTimeZoneGroup: BasicTimeZoneGroup;
private timeZoneGroups: BasicTimeZoneGroup[];
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
onLabelClick(): void {
this.setFocus();
}
private setComboboxRef = (el: HTMLCalciteComboboxElement): void => {
this.comboboxEl = el;
};
private onComboboxBeforeClose = (event: CustomEvent): void => {
event.stopPropagation();
this.calciteInputTimeZoneBeforeClose.emit();
};
private onComboboxBeforeOpen = (event: CustomEvent): void => {
event.stopPropagation();
this.calciteInputTimeZoneBeforeOpen.emit();
};
private onComboboxChange = (event: CustomEvent): void => {
event.stopPropagation();
const combobox = event.target as HTMLCalciteComboboxElement;
const selected = this.timeZoneGroups.find(
({ offsetValue }) => combobox.value === `${offsetValue}`
);
const selectedValue = `${selected.offsetValue}`;
if (this.value === selectedValue) {
return;
}
this.value = selectedValue;
this.selectedTimeZoneGroup = selected;
this.calciteInputTimeZoneChange.emit();
};
private onComboboxClose = (event: CustomEvent): void => {
event.stopPropagation();
this.open = false;
this.calciteInputTimeZoneClose.emit();
};
private onComboboxOpen = (event: CustomEvent): void => {
this.open = true;
event.stopPropagation();
this.calciteInputTimeZoneOpen.emit();
};
// --------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback(): void {
connectForm(this);
connectLabel(this);
connectLocalized(this);
connectMessages(this);
}
disconnectedCallback(): void {
disconnectForm(this);
disconnectLabel(this);
disconnectLocalized(this);
disconnectMessages(this);
}
async componentWillLoad(): Promise<void> {
setUpLoadableComponent(this);
await setUpMessages(this);
const timeZoneGroups = await generateTimeZoneGroups();
this.timeZoneGroups = timeZoneGroups;
const offsetToMatch = this.value ?? getUserTimeZoneOffset();
this.selectedTimeZoneGroup = timeZoneGroups.find(
({ offsetValue }) =>
// intentional == to match string to number
offsetValue == offsetToMatch
);
const selectedValue = `${this.selectedTimeZoneGroup.offsetValue}`;
afterConnectDefaultValueSet(this, selectedValue);
this.value = selectedValue;
}
componentDidLoad(): void {
setComponentLoaded(this);
}
componentDidRender(): void {
updateHostInteraction(this);
}
render(): VNode {
return (
<Host>
<calcite-combobox
clearDisabled={true}
disabled={this.disabled}
label={this.messages.chooseTimeZone}
lang={this.effectiveLocale}
onCalciteComboboxBeforeClose={this.onComboboxBeforeClose}
onCalciteComboboxBeforeOpen={this.onComboboxBeforeOpen}
onCalciteComboboxChange={this.onComboboxChange}
onCalciteComboboxClose={this.onComboboxClose}
onCalciteComboboxOpen={this.onComboboxOpen}
open={this.open}
overlayPositioning={this.overlayPositioning}
scale={this.scale}
selectionMode="single-persist"
// eslint-disable-next-line react/jsx-sort-props -- ref should be last so node attrs/props are in sync (see https://github.com/Esri/calcite-design-system/pull/6530)
ref={this.setComboboxRef}
>
{this.timeZoneGroups.map((group) => {
const selected = this.selectedTimeZoneGroup === group;
const label = group.offsetLabel;
const value = group.offsetValue;
return (
<calcite-combobox-item
key={label}
selected={selected}
textLabel={label}
value={`${value}`}
/>
);
})}
</calcite-combobox>
<HiddenFormInputSlot component={this} />
</Host>
);
}
}