react-timezone-select 1.2.3-experimental.1 → 1.2.3-experimental.5
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/dist/index.cjs +3 -3
- package/dist/index.js +108 -0
- package/dist/index.mjs +10068 -12
- package/dist/timezone-list.js +82 -0
- package/dist/types/globals.js +1 -0
- package/dist/types/timezone.js +1 -0
- package/package.json +6 -13
package/dist/index.cjs
CHANGED
|
@@ -289,7 +289,7 @@ __export(src_exports, {
|
|
|
289
289
|
allTimezones: () => timezone_list_default,
|
|
290
290
|
default: () => src_default
|
|
291
291
|
});
|
|
292
|
-
var
|
|
292
|
+
var React3 = __toESM(require("react"), 1);
|
|
293
293
|
|
|
294
294
|
// node_modules/@babel/runtime/helpers/esm/extends.js
|
|
295
295
|
function _extends() {
|
|
@@ -9994,7 +9994,7 @@ var TimezoneSelect = ({
|
|
|
9994
9994
|
}) => {
|
|
9995
9995
|
if (!timezones)
|
|
9996
9996
|
timezones = timezone_list_default;
|
|
9997
|
-
const getOptions =
|
|
9997
|
+
const getOptions = React3.useMemo(() => {
|
|
9998
9998
|
return Object.entries(timezones).reduce((selectOptions, zone) => {
|
|
9999
9999
|
const now = Bt.now(zone[0]);
|
|
10000
10000
|
const tz = now.timezone();
|
|
@@ -10066,7 +10066,7 @@ var TimezoneSelect = ({
|
|
|
10066
10066
|
return getOptions.find((tz) => tz.value === zone.value);
|
|
10067
10067
|
}
|
|
10068
10068
|
};
|
|
10069
|
-
return /* @__PURE__ */
|
|
10069
|
+
return /* @__PURE__ */ React3.createElement(react_select_esm_default, {
|
|
10070
10070
|
value: parseTimezone(value),
|
|
10071
10071
|
onChange: handleChange,
|
|
10072
10072
|
options: getOptions,
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import Select from 'react-select';
|
|
3
|
+
import spacetime from 'spacetime';
|
|
4
|
+
import soft from 'timezone-soft';
|
|
5
|
+
import allTimezones from './timezone-list.js';
|
|
6
|
+
export { allTimezones };
|
|
7
|
+
const TimezoneSelect = ({ value, onBlur, onChange, labelStyle = 'original', timezones, ...props }) => {
|
|
8
|
+
if (!timezones)
|
|
9
|
+
timezones = allTimezones;
|
|
10
|
+
const getOptions = React.useMemo(() => {
|
|
11
|
+
return Object.entries(timezones)
|
|
12
|
+
.reduce((selectOptions, zone) => {
|
|
13
|
+
const now = spacetime.now(zone[0]);
|
|
14
|
+
const tz = now.timezone();
|
|
15
|
+
const tzStrings = soft(zone[0]);
|
|
16
|
+
let label = '';
|
|
17
|
+
let abbr = now.isDST()
|
|
18
|
+
? // @ts-expect-error
|
|
19
|
+
tzStrings[0].daylight?.abbr
|
|
20
|
+
: // @ts-expect-error
|
|
21
|
+
tzStrings[0].standard?.abbr;
|
|
22
|
+
let altName = now.isDST()
|
|
23
|
+
? tzStrings[0].daylight?.name
|
|
24
|
+
: tzStrings[0].standard?.name;
|
|
25
|
+
const min = tz.current.offset * 60;
|
|
26
|
+
const hr = `${(min / 60) ^ 0}:` + (min % 60 === 0 ? '00' : Math.abs(min % 60));
|
|
27
|
+
const prefix = `(GMT${hr.includes('-') ? hr : `+${hr}`}) ${zone[1]}`;
|
|
28
|
+
switch (labelStyle) {
|
|
29
|
+
case 'original':
|
|
30
|
+
label = prefix;
|
|
31
|
+
break;
|
|
32
|
+
case 'altName':
|
|
33
|
+
label = `${prefix} ${altName?.length ? `(${altName})` : ''}`;
|
|
34
|
+
break;
|
|
35
|
+
case 'abbrev':
|
|
36
|
+
label = `${prefix} ${abbr?.length < 5 ? `(${abbr})` : ''}`;
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
label = `${prefix}`;
|
|
40
|
+
}
|
|
41
|
+
selectOptions.push({
|
|
42
|
+
value: tz.name,
|
|
43
|
+
label: label,
|
|
44
|
+
offset: tz.current.offset,
|
|
45
|
+
abbrev: abbr,
|
|
46
|
+
altName: altName,
|
|
47
|
+
});
|
|
48
|
+
return selectOptions;
|
|
49
|
+
}, [])
|
|
50
|
+
.sort((a, b) => a.offset - b.offset);
|
|
51
|
+
}, [labelStyle, timezones]);
|
|
52
|
+
const handleChange = (tz) => {
|
|
53
|
+
onChange && onChange(tz);
|
|
54
|
+
};
|
|
55
|
+
const findFuzzyTz = (zone) => {
|
|
56
|
+
let currentTime = spacetime.now('GMT');
|
|
57
|
+
try {
|
|
58
|
+
currentTime = spacetime.now(zone);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
return getOptions
|
|
64
|
+
.filter((tz) => tz.offset === currentTime.timezone().current.offset)
|
|
65
|
+
.map((tz) => {
|
|
66
|
+
let score = 0;
|
|
67
|
+
if (currentTime.timezones[tz.value.toLowerCase()] &&
|
|
68
|
+
!!currentTime.timezones[tz.value.toLowerCase()].dst ===
|
|
69
|
+
currentTime.timezone().hasDst) {
|
|
70
|
+
if (tz.value
|
|
71
|
+
.toLowerCase()
|
|
72
|
+
.indexOf(currentTime.tz.substring(currentTime.tz.indexOf('/') + 1)) !== -1) {
|
|
73
|
+
score += 8;
|
|
74
|
+
}
|
|
75
|
+
if (tz.label
|
|
76
|
+
.toLowerCase()
|
|
77
|
+
.indexOf(currentTime.tz.substring(currentTime.tz.indexOf('/') + 1)) !== -1) {
|
|
78
|
+
score += 4;
|
|
79
|
+
}
|
|
80
|
+
if (tz.value
|
|
81
|
+
.toLowerCase()
|
|
82
|
+
.indexOf(currentTime.tz.substring(0, currentTime.tz.indexOf('/')))) {
|
|
83
|
+
score += 2;
|
|
84
|
+
}
|
|
85
|
+
score += 1;
|
|
86
|
+
}
|
|
87
|
+
else if (tz.value === 'GMT') {
|
|
88
|
+
score += 1;
|
|
89
|
+
}
|
|
90
|
+
return { tz, score };
|
|
91
|
+
})
|
|
92
|
+
.sort((a, b) => b.score - a.score)
|
|
93
|
+
.map(({ tz }) => tz)[0];
|
|
94
|
+
};
|
|
95
|
+
const parseTimezone = (zone) => {
|
|
96
|
+
if (typeof zone === 'object' && zone.value && zone.label)
|
|
97
|
+
return zone;
|
|
98
|
+
if (typeof zone === 'string') {
|
|
99
|
+
return (getOptions.find(tz => tz.value === zone) ||
|
|
100
|
+
(zone.indexOf('/') !== -1 && findFuzzyTz(zone)));
|
|
101
|
+
}
|
|
102
|
+
else if (zone.value && !zone.label) {
|
|
103
|
+
return getOptions.find(tz => tz.value === zone.value);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return (React.createElement(Select, { value: parseTimezone(value), onChange: handleChange, options: getOptions, onBlur: onBlur, ...props }));
|
|
107
|
+
};
|
|
108
|
+
export default TimezoneSelect;
|