react-timezone-select 2.0.1 → 2.1.0
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 +26 -0
- package/dist/index.cjs +38 -26
- package/dist/index.d.ts +14 -8
- package/dist/index.js +36 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -138,6 +138,32 @@ timezones={{
|
|
|
138
138
|
- `maxAbbrLength` - `number` - Truncate `abbrev` labelStyles string to length
|
|
139
139
|
- Any other [`react-select`](https://github.com/jedwatson/react-select#props) props
|
|
140
140
|
|
|
141
|
+
## 🎨 Custom Select component
|
|
142
|
+
|
|
143
|
+
By default, `react-timezone-select` uses [`react-select`](https://github.com/jedwatson/react-select) as underlying select component. If you'd like to bring your own select component, you can use the `useTimezoneSelect` hook instead of the `TimezoneSelect` component to render the timezones using your self-provided select component.
|
|
144
|
+
|
|
145
|
+
```jsx
|
|
146
|
+
import { useTimezoneSelect, allTimezones } from 'react-timezone-select'
|
|
147
|
+
|
|
148
|
+
const labelStyle = 'original'
|
|
149
|
+
const timezones = {
|
|
150
|
+
...allTimezones,
|
|
151
|
+
'Europe/Berlin': 'Frankfurt'
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const customSelect = () => {
|
|
155
|
+
const { options, parseTimezone } = useTimezoneSelect({ labelStyle, timezones })
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<select onChange={e => onChange(parseTimezone(e.currentTarget.value))}>
|
|
159
|
+
{options.map(option => (
|
|
160
|
+
<option value={option.value}>{option.label}</option>
|
|
161
|
+
))}
|
|
162
|
+
</select>
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
141
167
|
## 🚧 Contributing
|
|
142
168
|
|
|
143
169
|
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!
|
package/dist/index.cjs
CHANGED
|
@@ -30,7 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
var src_exports = {};
|
|
31
31
|
__export(src_exports, {
|
|
32
32
|
allTimezones: () => timezone_list_default,
|
|
33
|
-
default: () => TimezoneSelect
|
|
33
|
+
default: () => TimezoneSelect,
|
|
34
|
+
useTimezoneSelect: () => useTimezoneSelect
|
|
34
35
|
});
|
|
35
36
|
module.exports = __toCommonJS(src_exports);
|
|
36
37
|
var React = __toESM(require("react"), 1);
|
|
@@ -123,17 +124,13 @@ var allTimezones = {
|
|
|
123
124
|
var timezone_list_default = allTimezones;
|
|
124
125
|
|
|
125
126
|
// src/index.tsx
|
|
126
|
-
|
|
127
|
-
value,
|
|
128
|
-
onBlur,
|
|
129
|
-
onChange,
|
|
130
|
-
labelStyle = "original",
|
|
127
|
+
function useTimezoneSelect({
|
|
131
128
|
timezones = timezone_list_default,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
})
|
|
135
|
-
const
|
|
136
|
-
|
|
129
|
+
labelStyle = "original",
|
|
130
|
+
maxAbbrLength = 4
|
|
131
|
+
}) {
|
|
132
|
+
const options = React.useMemo(() => {
|
|
133
|
+
return Object.entries(timezones).map((zone) => {
|
|
137
134
|
try {
|
|
138
135
|
const now = import_spacetime.default.now(zone[0]);
|
|
139
136
|
const tz = now.timezone();
|
|
@@ -161,23 +158,18 @@ var TimezoneSelect = ({
|
|
|
161
158
|
default:
|
|
162
159
|
label = `${prefix}`;
|
|
163
160
|
}
|
|
164
|
-
|
|
161
|
+
return {
|
|
165
162
|
value: tz.name,
|
|
166
163
|
label,
|
|
167
164
|
offset: tz.current.offset,
|
|
168
165
|
abbrev: abbr,
|
|
169
166
|
altName
|
|
170
|
-
}
|
|
171
|
-
return selectOptions;
|
|
167
|
+
};
|
|
172
168
|
} catch {
|
|
173
|
-
return
|
|
169
|
+
return null;
|
|
174
170
|
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
);
|
|
178
|
-
const handleChange = (tz) => {
|
|
179
|
-
onChange && onChange(tz);
|
|
180
|
-
};
|
|
171
|
+
}).filter(Boolean).sort((a, b) => a.offset - b.offset);
|
|
172
|
+
}, [labelStyle, timezones]);
|
|
181
173
|
const findFuzzyTz = (zone) => {
|
|
182
174
|
let currentTime = import_spacetime.default.now("GMT");
|
|
183
175
|
try {
|
|
@@ -185,7 +177,7 @@ var TimezoneSelect = ({
|
|
|
185
177
|
} catch (err) {
|
|
186
178
|
return;
|
|
187
179
|
}
|
|
188
|
-
return
|
|
180
|
+
return options.filter(
|
|
189
181
|
(tz) => tz.offset === currentTime.timezone().current.offset
|
|
190
182
|
).map((tz) => {
|
|
191
183
|
let score = 0;
|
|
@@ -214,17 +206,36 @@ var TimezoneSelect = ({
|
|
|
214
206
|
if (typeof zone === "object" && zone.value && zone.label)
|
|
215
207
|
return zone;
|
|
216
208
|
if (typeof zone === "string") {
|
|
217
|
-
return
|
|
209
|
+
return options.find((tz) => tz.value === zone) || zone.indexOf("/") !== -1 && findFuzzyTz(zone);
|
|
218
210
|
} else if (zone.value && !zone.label) {
|
|
219
|
-
return
|
|
211
|
+
return options.find((tz) => tz.value === zone.value);
|
|
220
212
|
}
|
|
221
213
|
};
|
|
214
|
+
return { options, parseTimezone };
|
|
215
|
+
}
|
|
216
|
+
var TimezoneSelect = ({
|
|
217
|
+
value,
|
|
218
|
+
onBlur,
|
|
219
|
+
onChange,
|
|
220
|
+
labelStyle,
|
|
221
|
+
maxAbbrLength,
|
|
222
|
+
timezones,
|
|
223
|
+
...props
|
|
224
|
+
}) => {
|
|
225
|
+
const { options, parseTimezone } = useTimezoneSelect({
|
|
226
|
+
timezones,
|
|
227
|
+
labelStyle,
|
|
228
|
+
maxAbbrLength
|
|
229
|
+
});
|
|
230
|
+
const handleChange = (tz) => {
|
|
231
|
+
onChange && onChange(tz);
|
|
232
|
+
};
|
|
222
233
|
return /* @__PURE__ */ React.createElement(
|
|
223
234
|
import_react_select.default,
|
|
224
235
|
{
|
|
225
236
|
value: parseTimezone(value),
|
|
226
237
|
onChange: handleChange,
|
|
227
|
-
options
|
|
238
|
+
options,
|
|
228
239
|
onBlur,
|
|
229
240
|
...props
|
|
230
241
|
}
|
|
@@ -232,5 +243,6 @@ var TimezoneSelect = ({
|
|
|
232
243
|
};
|
|
233
244
|
// Annotate the CommonJS export names for ESM import in node:
|
|
234
245
|
0 && (module.exports = {
|
|
235
|
-
allTimezones
|
|
246
|
+
allTimezones,
|
|
247
|
+
useTimezoneSelect
|
|
236
248
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -4,24 +4,30 @@ declare type ICustomTimezone = {
|
|
|
4
4
|
[key: string]: string;
|
|
5
5
|
};
|
|
6
6
|
declare type ILabelStyle = 'original' | 'altName' | 'abbrev';
|
|
7
|
-
|
|
7
|
+
declare type ITimezoneOption = {
|
|
8
8
|
value: string;
|
|
9
9
|
label: string;
|
|
10
10
|
abbrev?: string;
|
|
11
11
|
altName?: string;
|
|
12
12
|
offset?: number;
|
|
13
|
-
}
|
|
13
|
+
};
|
|
14
14
|
declare type ITimezone = ITimezoneOption | string;
|
|
15
|
-
|
|
16
|
-
value: ITimezone;
|
|
15
|
+
declare type TimezoneSelectOptions = {
|
|
17
16
|
labelStyle?: ILabelStyle;
|
|
18
|
-
onChange?: (timezone: ITimezoneOption) => void;
|
|
19
17
|
timezones?: ICustomTimezone;
|
|
20
18
|
maxAbbrLength?: number;
|
|
21
|
-
}
|
|
19
|
+
};
|
|
20
|
+
declare type Props = Omit<Props$1<ITimezone, false>, 'onChange'> & TimezoneSelectOptions & {
|
|
21
|
+
value: ITimezone;
|
|
22
|
+
onChange?: (timezone: ITimezoneOption) => void;
|
|
23
|
+
};
|
|
22
24
|
|
|
23
25
|
declare const allTimezones: ICustomTimezone;
|
|
24
26
|
|
|
25
|
-
declare
|
|
27
|
+
declare function useTimezoneSelect({ timezones, labelStyle, maxAbbrLength, }: TimezoneSelectOptions): {
|
|
28
|
+
parseTimezone: (zone: ITimezone) => ITimezoneOption;
|
|
29
|
+
options: ITimezoneOption[];
|
|
30
|
+
};
|
|
31
|
+
declare const TimezoneSelect: ({ value, onBlur, onChange, labelStyle, maxAbbrLength, timezones, ...props }: Props) => JSX.Element;
|
|
26
32
|
|
|
27
|
-
export { ILabelStyle, ITimezone, ITimezoneOption, Props, allTimezones, TimezoneSelect as default };
|
|
33
|
+
export { ILabelStyle, ITimezone, ITimezoneOption, Props, TimezoneSelectOptions, allTimezones, TimezoneSelect as default, useTimezoneSelect };
|
package/dist/index.js
CHANGED
|
@@ -89,17 +89,13 @@ var allTimezones = {
|
|
|
89
89
|
var timezone_list_default = allTimezones;
|
|
90
90
|
|
|
91
91
|
// src/index.tsx
|
|
92
|
-
|
|
93
|
-
value,
|
|
94
|
-
onBlur,
|
|
95
|
-
onChange,
|
|
96
|
-
labelStyle = "original",
|
|
92
|
+
function useTimezoneSelect({
|
|
97
93
|
timezones = timezone_list_default,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
})
|
|
101
|
-
const
|
|
102
|
-
|
|
94
|
+
labelStyle = "original",
|
|
95
|
+
maxAbbrLength = 4
|
|
96
|
+
}) {
|
|
97
|
+
const options = React.useMemo(() => {
|
|
98
|
+
return Object.entries(timezones).map((zone) => {
|
|
103
99
|
try {
|
|
104
100
|
const now = spacetime.now(zone[0]);
|
|
105
101
|
const tz = now.timezone();
|
|
@@ -127,23 +123,18 @@ var TimezoneSelect = ({
|
|
|
127
123
|
default:
|
|
128
124
|
label = `${prefix}`;
|
|
129
125
|
}
|
|
130
|
-
|
|
126
|
+
return {
|
|
131
127
|
value: tz.name,
|
|
132
128
|
label,
|
|
133
129
|
offset: tz.current.offset,
|
|
134
130
|
abbrev: abbr,
|
|
135
131
|
altName
|
|
136
|
-
}
|
|
137
|
-
return selectOptions;
|
|
132
|
+
};
|
|
138
133
|
} catch {
|
|
139
|
-
return
|
|
134
|
+
return null;
|
|
140
135
|
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
);
|
|
144
|
-
const handleChange = (tz) => {
|
|
145
|
-
onChange && onChange(tz);
|
|
146
|
-
};
|
|
136
|
+
}).filter(Boolean).sort((a, b) => a.offset - b.offset);
|
|
137
|
+
}, [labelStyle, timezones]);
|
|
147
138
|
const findFuzzyTz = (zone) => {
|
|
148
139
|
let currentTime = spacetime.now("GMT");
|
|
149
140
|
try {
|
|
@@ -151,7 +142,7 @@ var TimezoneSelect = ({
|
|
|
151
142
|
} catch (err) {
|
|
152
143
|
return;
|
|
153
144
|
}
|
|
154
|
-
return
|
|
145
|
+
return options.filter(
|
|
155
146
|
(tz) => tz.offset === currentTime.timezone().current.offset
|
|
156
147
|
).map((tz) => {
|
|
157
148
|
let score = 0;
|
|
@@ -180,17 +171,36 @@ var TimezoneSelect = ({
|
|
|
180
171
|
if (typeof zone === "object" && zone.value && zone.label)
|
|
181
172
|
return zone;
|
|
182
173
|
if (typeof zone === "string") {
|
|
183
|
-
return
|
|
174
|
+
return options.find((tz) => tz.value === zone) || zone.indexOf("/") !== -1 && findFuzzyTz(zone);
|
|
184
175
|
} else if (zone.value && !zone.label) {
|
|
185
|
-
return
|
|
176
|
+
return options.find((tz) => tz.value === zone.value);
|
|
186
177
|
}
|
|
187
178
|
};
|
|
179
|
+
return { options, parseTimezone };
|
|
180
|
+
}
|
|
181
|
+
var TimezoneSelect = ({
|
|
182
|
+
value,
|
|
183
|
+
onBlur,
|
|
184
|
+
onChange,
|
|
185
|
+
labelStyle,
|
|
186
|
+
maxAbbrLength,
|
|
187
|
+
timezones,
|
|
188
|
+
...props
|
|
189
|
+
}) => {
|
|
190
|
+
const { options, parseTimezone } = useTimezoneSelect({
|
|
191
|
+
timezones,
|
|
192
|
+
labelStyle,
|
|
193
|
+
maxAbbrLength
|
|
194
|
+
});
|
|
195
|
+
const handleChange = (tz) => {
|
|
196
|
+
onChange && onChange(tz);
|
|
197
|
+
};
|
|
188
198
|
return /* @__PURE__ */ React.createElement(
|
|
189
199
|
Select,
|
|
190
200
|
{
|
|
191
201
|
value: parseTimezone(value),
|
|
192
202
|
onChange: handleChange,
|
|
193
|
-
options
|
|
203
|
+
options,
|
|
194
204
|
onBlur,
|
|
195
205
|
...props
|
|
196
206
|
}
|
|
@@ -198,5 +208,6 @@ var TimezoneSelect = ({
|
|
|
198
208
|
};
|
|
199
209
|
export {
|
|
200
210
|
timezone_list_default as allTimezones,
|
|
201
|
-
TimezoneSelect as default
|
|
211
|
+
TimezoneSelect as default,
|
|
212
|
+
useTimezoneSelect
|
|
202
213
|
};
|
package/package.json
CHANGED