react-timezone-select 1.2.3-experimental.5 → 1.2.3-experimental.7

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.js CHANGED
@@ -103,6 +103,6 @@ const TimezoneSelect = ({ value, onBlur, onChange, labelStyle = 'original', time
103
103
  return getOptions.find(tz => tz.value === zone.value);
104
104
  }
105
105
  };
106
- return (React.createElement(Select, { value: parseTimezone(value), onChange: handleChange, options: getOptions, onBlur: onBlur, ...props }));
106
+ return (<Select value={parseTimezone(value)} onChange={handleChange} options={getOptions} onBlur={onBlur} {...props}/>);
107
107
  };
108
108
  export default TimezoneSelect;
package/dist/index.jsx 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 (<Select value={parseTimezone(value)} onChange={handleChange} options={getOptions} onBlur={onBlur} {...props}/>);
107
+ };
108
+ export default TimezoneSelect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-timezone-select",
3
- "version": "1.2.3-experimental.5",
3
+ "version": "1.2.3-experimental.7",
4
4
  "description": "Usable, dynamic React Timezone Select",
5
5
  "scripts": {
6
6
  "start": "concurrently npm:start:example npm:build",