react-timezone-select 1.5.4 → 1.5.6

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/README.md CHANGED
@@ -16,7 +16,7 @@ While looking around for a good option, I had trouble finding a timezone select
16
16
 
17
17
  #### Demo: [ndom91.github.io/react-timezone-select](https://ndom91.github.io/react-timezone-select/)
18
18
 
19
- > This demo is also available in the `./examples` directory. Simply run `npm run dev` in the root of the repository after installing everything in the examples subdirectory and snowpack dev will begin, where you can find the demo at [`localhost:8080`](http://localhost:8080).
19
+ > This demo is also available in the `./examples` directory. Simply run `pnpm dev` in the root of the repository and the vite dev server will start, where you can then find the example app at [`localhost:3001`](http://localhost:3001).
20
20
 
21
21
  We also have some **more examples** available on Codesandbox using this component with the datetime library `spacetime` ([example](https://codesandbox.io/s/react-timezone-select-usage-z37hf)) as well as with `moment` ([example](https://codesandbox.io/s/react-timezone-select-usage-moment-5n6vn)), as well as in Typescript using the new `Intl` browser API ([example](https://codesandbox.io/s/react-timezone-select-typescript-8lsv3?file=/src/App.tsx)) showing how one might use this component in a real application.
22
22
 
@@ -34,7 +34,9 @@ import ReactDOM from 'react-dom'
34
34
  import TimezoneSelect from 'react-timezone-select'
35
35
 
36
36
  const App = () => {
37
- const [selectedTimezone, setSelectedTimezone] = useState({})
37
+ const [selectedTimezone, setSelectedTimezone] =useState(
38
+ Intl.DateTimeFormat().resolvedOptions().timeZone
39
+ )
38
40
 
39
41
  return (
40
42
  <div className="App">
@@ -86,22 +88,7 @@ const [timezone, setTimezone] = useState(
86
88
  )
87
89
  ```
88
90
 
89
- Thanks [@ndrwksr](https://github.com/ndom91/react-timezone-select/issues/25)!
90
-
91
- ### ⚠ Next.js Users
92
-
93
- **Update**: Starting with `v1.3.0` you DO NOT need `next-transpile-modules` to use this library with Next.js anymore!
94
-
95
- For now, Next.js isn't great about handling ESM packages. Until this gets fixed, there is a workaround for using this and other ESM packages via [`next-transpile-modules`](https://www.npmjs.com/package/next-transpile-modules).
96
-
97
- ```js
98
- // next.config.js
99
- const withTM = require('next-transpile-modules')(['react-timezone-select'])
100
-
101
- module.exports = withTM({
102
- // ...further Next.js config
103
- })
104
- ```
91
+ Thanks [@ndrwksr](https://github.com/ndom91/react-timezone-select/issues/25)
105
92
 
106
93
  ### 🕒 Custom Timezones
107
94
 
@@ -111,7 +98,7 @@ The `timezones` prop takes a dictionary of timezones. Don't worry, we'll prepend
111
98
 
112
99
  ```jsx
113
100
  import TimezoneSelect, { allTimezones } from 'react-timezone-select'
114
- ;<TimezoneSelect
101
+ <TimezoneSelect
115
102
  value={selectedTimezone}
116
103
  onChange={setSelectedTimezone}
117
104
  timezones={{
@@ -126,26 +113,34 @@ The above example will generate two additional choices in the select options, on
126
113
 
127
114
  ## đŸ•šī¸ Props
128
115
 
129
- - `value` - Initial Timezone `string`, i.e. `'Europe/Amsterdam'` or the full object from the onChange function: `{ value: string, label: string, abbrev: string, altName: string }`
116
+ - `value` - `string | Object` - Initial/current Timezone.
117
+ ```
118
+ 'America/Juneau' | {
119
+ value: 'America/Juneau'
120
+ label: '(GMT-8:00) Alaska,
121
+ abbrev: 'AHST',
122
+ offset: -8,
123
+ altName: 'Alaskan Standard Time'
124
+ }
125
+ ```
130
126
  - `onBlur` - `() => void`
131
127
  - `onChange` - `(timezone) => void`
132
- - Example `timezone` parameter:
133
- ```
134
- {
135
- value: 'America/Juneau'
136
- label: '(GMT-8:00) Alaska,
137
- abbrev: 'AHST',
138
- offset: -8,
139
- altName: 'Alaskan Standard Time'
140
- }
141
- ```
142
128
  - `labelStyle` - `'original' | 'altName' | 'abbrev'`
143
- - `timezones` - Custom Timezone Object
129
+ - `timezones` - `Record<string,string>`
130
+ ```
131
+ timezones={{
132
+ ...allTimezones,
133
+ 'America/Lima': 'Pittsburgh',
134
+ 'Europe/Berlin': 'Frankfurt',
135
+ }}
136
+ ```
137
+
138
+ - `maxAbbrLength` - `number` - Truncate `abbrev` labelStyles string to length
144
139
  - Any other [`react-select`](https://github.com/jedwatson/react-select#props) props
145
140
 
146
141
  ## 🚧 Contributing
147
142
 
148
- Pull requests are always welcome! Please stick to the `prettier` settings, and if adding new features, please consider adding test(s) and documentation where appropriate!
143
+ Pull requests are always welcome! Please stick to repo settings (prettier, eslint, etc.), and if adding new features, please consider adding test(s) and documentation where appropriate!
149
144
 
150
145
  ## 🙏 Thanks
151
146
 
package/dist/index.cjs CHANGED
@@ -129,48 +129,47 @@ var TimezoneSelect = ({
129
129
  onChange,
130
130
  labelStyle = "original",
131
131
  timezones,
132
+ maxAbbrLength = 4,
132
133
  ...props
133
134
  }) => {
134
135
  if (!timezones)
135
136
  timezones = timezone_list_default;
136
137
  const getOptions = React.useMemo(() => {
137
138
  return Object.entries(timezones).reduce((selectOptions, zone) => {
138
- const now = import_spacetime.default.now(zone[0]);
139
- const tz = now.timezone();
140
- const tzStrings = (0, import_timezone_soft.default)(zone[0]);
141
- let label = "";
142
- let abbr = now.isDST() ? (
143
- // @ts-expect-error
144
- tzStrings[0].daylight?.abbr
145
- ) : (
146
- // @ts-expect-error
147
- tzStrings[0].standard?.abbr
148
- );
149
- let altName = now.isDST() ? tzStrings[0].daylight?.name : tzStrings[0].standard?.name;
150
- const min = tz.current.offset * 60;
151
- const hr = `${min / 60 ^ 0}:` + (min % 60 === 0 ? "00" : Math.abs(min % 60));
152
- const prefix = `(GMT${hr.includes("-") ? hr : `+${hr}`}) ${zone[1]}`;
153
- switch (labelStyle) {
154
- case "original":
155
- label = prefix;
156
- break;
157
- case "altName":
158
- label = `${prefix} ${altName?.length ? `(${altName})` : ""}`;
159
- break;
160
- case "abbrev":
161
- label = `${prefix} ${abbr?.length < 5 ? `(${abbr})` : ""}`;
162
- break;
163
- default:
164
- label = `${prefix}`;
139
+ try {
140
+ const now = import_spacetime.default.now(zone[0]);
141
+ const tz = now.timezone();
142
+ const tzStrings = (0, import_timezone_soft.default)(zone[0]);
143
+ let label = "";
144
+ let abbr = now.isDST() ? tzStrings[0].daylight.abbr : tzStrings[0].standard.abbr;
145
+ let altName = now.isDST() ? tzStrings[0].daylight.name : tzStrings[0].standard.name;
146
+ const min = tz.current.offset * 60;
147
+ const hr = `${min / 60 ^ 0}:` + (min % 60 === 0 ? "00" : Math.abs(min % 60));
148
+ const prefix = `(GMT${hr.includes("-") ? hr : `+${hr}`}) ${zone[1]}`;
149
+ switch (labelStyle) {
150
+ case "original":
151
+ label = prefix;
152
+ break;
153
+ case "altName":
154
+ label = `${prefix} ${altName?.length ? `(${altName})` : ""}`;
155
+ break;
156
+ case "abbrev":
157
+ label = `${prefix} (${abbr.substring(0, maxAbbrLength)})`;
158
+ break;
159
+ default:
160
+ label = `${prefix}`;
161
+ }
162
+ selectOptions.push({
163
+ value: tz.name,
164
+ label,
165
+ offset: tz.current.offset,
166
+ abbrev: abbr,
167
+ altName
168
+ });
169
+ return selectOptions;
170
+ } catch {
171
+ return selectOptions;
165
172
  }
166
- selectOptions.push({
167
- value: tz.name,
168
- label,
169
- offset: tz.current.offset,
170
- abbrev: abbr,
171
- altName
172
- });
173
- return selectOptions;
174
173
  }, []).sort((a, b) => a.offset - b.offset);
175
174
  }, [labelStyle, timezones]);
176
175
  const handleChange = (tz) => {
package/dist/index.d.ts CHANGED
@@ -18,10 +18,19 @@ interface Props
18
18
  labelStyle?: ILabelStyle
19
19
  onChange?: (timezone: ITimezoneOption) => void
20
20
  timezones?: ICustomTimezone
21
+ maxAbbrLength?: number
21
22
  }
22
23
 
23
24
  declare const allTimezones: ICustomTimezone;
24
25
 
25
- declare const TimezoneSelect: ({ value, onBlur, onChange, labelStyle, timezones, ...props }: Props) => JSX.Element;
26
+ declare const TimezoneSelect: ({
27
+ value,
28
+ onBlur,
29
+ onChange,
30
+ labelStyle,
31
+ timezones,
32
+ maxAbbrLength,
33
+ ...props
34
+ }: Props) => JSX.Element
26
35
 
27
36
  export { ILabelStyle, ITimezone, ITimezoneOption, Props, allTimezones, TimezoneSelect as default };
package/dist/index.js CHANGED
@@ -95,48 +95,47 @@ var TimezoneSelect = ({
95
95
  onChange,
96
96
  labelStyle = "original",
97
97
  timezones,
98
+ maxAbbrLength = 4,
98
99
  ...props
99
100
  }) => {
100
101
  if (!timezones)
101
102
  timezones = timezone_list_default;
102
103
  const getOptions = React.useMemo(() => {
103
104
  return Object.entries(timezones).reduce((selectOptions, zone) => {
104
- const now = spacetime.now(zone[0]);
105
- const tz = now.timezone();
106
- const tzStrings = soft(zone[0]);
107
- let label = "";
108
- let abbr = now.isDST() ? (
109
- // @ts-expect-error
110
- tzStrings[0].daylight?.abbr
111
- ) : (
112
- // @ts-expect-error
113
- tzStrings[0].standard?.abbr
114
- );
115
- let altName = now.isDST() ? tzStrings[0].daylight?.name : tzStrings[0].standard?.name;
116
- const min = tz.current.offset * 60;
117
- const hr = `${min / 60 ^ 0}:` + (min % 60 === 0 ? "00" : Math.abs(min % 60));
118
- const prefix = `(GMT${hr.includes("-") ? hr : `+${hr}`}) ${zone[1]}`;
119
- switch (labelStyle) {
120
- case "original":
121
- label = prefix;
122
- break;
123
- case "altName":
124
- label = `${prefix} ${altName?.length ? `(${altName})` : ""}`;
125
- break;
126
- case "abbrev":
127
- label = `${prefix} ${abbr?.length < 5 ? `(${abbr})` : ""}`;
128
- break;
129
- default:
130
- label = `${prefix}`;
105
+ try {
106
+ const now = spacetime.now(zone[0]);
107
+ const tz = now.timezone();
108
+ const tzStrings = soft(zone[0]);
109
+ let label = "";
110
+ let abbr = now.isDST() ? tzStrings[0].daylight.abbr : tzStrings[0].standard.abbr;
111
+ let altName = now.isDST() ? tzStrings[0].daylight.name : tzStrings[0].standard.name;
112
+ const min = tz.current.offset * 60;
113
+ const hr = `${min / 60 ^ 0}:` + (min % 60 === 0 ? "00" : Math.abs(min % 60));
114
+ const prefix = `(GMT${hr.includes("-") ? hr : `+${hr}`}) ${zone[1]}`;
115
+ switch (labelStyle) {
116
+ case "original":
117
+ label = prefix;
118
+ break;
119
+ case "altName":
120
+ label = `${prefix} ${altName?.length ? `(${altName})` : ""}`;
121
+ break;
122
+ case "abbrev":
123
+ label = `${prefix} (${abbr.substring(0, maxAbbrLength)})`;
124
+ break;
125
+ default:
126
+ label = `${prefix}`;
127
+ }
128
+ selectOptions.push({
129
+ value: tz.name,
130
+ label,
131
+ offset: tz.current.offset,
132
+ abbrev: abbr,
133
+ altName
134
+ });
135
+ return selectOptions;
136
+ } catch {
137
+ return selectOptions;
131
138
  }
132
- selectOptions.push({
133
- value: tz.name,
134
- label,
135
- offset: tz.current.offset,
136
- abbrev: abbr,
137
- altName
138
- });
139
- return selectOptions;
140
139
  }, []).sort((a, b) => a.offset - b.offset);
141
140
  }, [labelStyle, timezones]);
142
141
  const handleChange = (tz) => {
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "react-timezone-select",
3
- "version": "1.5.4",
3
+ "version": "1.5.6",
4
4
  "description": "Usable, dynamic React Timezone Select",
5
5
  "scripts": {
6
- "dev": "concurrently \"rollup -cw\" \"cd example && npm run dev\"",
7
- "prepublish": "npm run build",
8
- "postpublish": "npm run build:example && npm run deploy",
6
+ "dev": "concurrently \"tsup src/index.tsx --format esm --watch\" \"cd example && pnpm dev\"",
7
+ "prepublishOnly": "pnpm run build",
8
+ "postpublish": "pnpm run build:example && npm run deploy",
9
9
  "build": "tsup src/index.tsx --format cjs,esm --dts",
10
- "build:example": "cd example && npm run build",
10
+ "build:example": "cd example && pnpm run build",
11
11
  "deploy": "gh-pages -d example/dist",
12
+ "pretest": "pnpm run build",
12
13
  "test": "jest",
13
14
  "test:watch": "jest --watch",
14
- "test:ci": "jest --ci ",
15
+ "test:ci": "pnpm run build && jest --ci ",
15
16
  "tsc": "tsc"
16
17
  },
17
18
  "author": "Nico Domino <yo@ndo.dev>",
@@ -51,8 +52,8 @@
51
52
  },
52
53
  "dependencies": {
53
54
  "react-select": "^5.7.0",
54
- "spacetime": "^7.1.4",
55
- "timezone-soft": "^1.3.1"
55
+ "spacetime": "^7.4.1",
56
+ "timezone-soft": "^1.4.1"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@babel/core": "^7.20.7",