react-timezone-select 1.2.2 → 1.2.3-experimental.4

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