282 lines
10 KiB
JavaScript
282 lines
10 KiB
JavaScript
"use client"
|
|
var __defProp = Object.defineProperty;
|
|
var __defProps = Object.defineProperties;
|
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
var __spreadValues = (a, b) => {
|
|
for (var prop in b || (b = {}))
|
|
if (__hasOwnProp.call(b, prop))
|
|
__defNormalProp(a, prop, b[prop]);
|
|
if (__getOwnPropSymbols)
|
|
for (var prop of __getOwnPropSymbols(b)) {
|
|
if (__propIsEnum.call(b, prop))
|
|
__defNormalProp(a, prop, b[prop]);
|
|
}
|
|
return a;
|
|
};
|
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
var __objRest = (source, exclude) => {
|
|
var target = {};
|
|
for (var prop in source)
|
|
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
target[prop] = source[prop];
|
|
if (source != null && __getOwnPropSymbols)
|
|
for (var prop of __getOwnPropSymbols(source)) {
|
|
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
target[prop] = source[prop];
|
|
}
|
|
return target;
|
|
};
|
|
|
|
// src/index.tsx
|
|
import { useMemo } from "react";
|
|
import Select from "react-select";
|
|
import spacetime from "spacetime";
|
|
import soft from "timezone-soft";
|
|
|
|
// src/timezone-list.ts
|
|
var allTimezones = {
|
|
"Pacific/Midway": "Midway Island, Samoa",
|
|
"Pacific/Honolulu": "Hawaii",
|
|
"America/Juneau": "Alaska",
|
|
"America/Boise": "Mountain Time",
|
|
"America/Dawson": "Dawson, Yukon",
|
|
"America/Chihuahua": "Chihuahua, La Paz, Mazatlan",
|
|
"America/Phoenix": "Arizona",
|
|
"America/Los_Angeles": "Pacific Time",
|
|
"America/Chicago": "Central Time",
|
|
"America/Regina": "Saskatchewan",
|
|
"America/Mexico_City": "Guadalajara, Mexico City, Monterrey",
|
|
"America/Belize": "Central America",
|
|
"America/Detroit": "Eastern Time",
|
|
"America/Bogota": "Bogota, Lima, Quito",
|
|
"America/Caracas": "Caracas, La Paz",
|
|
"America/Santiago": "Santiago",
|
|
"America/St_Johns": "Newfoundland and Labrador",
|
|
"America/Sao_Paulo": "Brasilia",
|
|
"America/Tijuana": "Tijuana",
|
|
"America/Montevideo": "Montevideo",
|
|
"America/Argentina/Buenos_Aires": "Buenos Aires, Georgetown",
|
|
"America/Godthab": "Greenland",
|
|
"Atlantic/Azores": "Azores",
|
|
"Atlantic/Cape_Verde": "Cape Verde Islands",
|
|
GMT: "UTC",
|
|
"Europe/London": "Edinburgh, London",
|
|
"Europe/Dublin": "Dublin",
|
|
"Europe/Lisbon": "Lisbon",
|
|
"Africa/Casablanca": "Casablanca, Monrovia",
|
|
"Atlantic/Canary": "Canary Islands",
|
|
"Europe/Belgrade": "Belgrade, Bratislava, Budapest, Ljubljana, Prague",
|
|
"Europe/Sarajevo": "Sarajevo, Skopje, Warsaw, Zagreb",
|
|
"Europe/Brussels": "Brussels, Copenhagen, Madrid, Paris",
|
|
"Europe/Amsterdam": "Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",
|
|
"Africa/Algiers": "West Central Africa",
|
|
"Europe/Bucharest": "Bucharest",
|
|
"Africa/Cairo": "Cairo",
|
|
"Europe/Helsinki": "Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius",
|
|
"Europe/Athens": "Athens",
|
|
"Asia/Jerusalem": "Jerusalem",
|
|
"Africa/Harare": "Harare, Pretoria",
|
|
"Europe/Moscow": "Istanbul, Minsk, Moscow, St. Petersburg, Volgograd",
|
|
"Asia/Kuwait": "Kuwait, Riyadh",
|
|
"Africa/Nairobi": "Nairobi",
|
|
"Asia/Baghdad": "Baghdad",
|
|
"Asia/Tehran": "Tehran",
|
|
"Asia/Dubai": "Abu Dhabi, Muscat",
|
|
"Asia/Baku": "Baku, Tbilisi, Yerevan",
|
|
"Asia/Kabul": "Kabul",
|
|
"Asia/Yekaterinburg": "Ekaterinburg",
|
|
"Asia/Karachi": "Islamabad, Karachi, Tashkent",
|
|
"Asia/Kolkata": "Chennai, Kolkata, Mumbai, New Delhi",
|
|
"Asia/Kathmandu": "Kathmandu",
|
|
"Asia/Dhaka": "Astana, Dhaka",
|
|
"Asia/Colombo": "Sri Jayawardenepura",
|
|
"Asia/Almaty": "Almaty, Novosibirsk",
|
|
"Asia/Rangoon": "Yangon Rangoon",
|
|
"Asia/Bangkok": "Bangkok, Hanoi, Jakarta",
|
|
"Asia/Krasnoyarsk": "Krasnoyarsk",
|
|
"Asia/Shanghai": "Beijing, Chongqing, Hong Kong SAR, Urumqi",
|
|
"Asia/Kuala_Lumpur": "Kuala Lumpur, Singapore",
|
|
"Asia/Taipei": "Taipei",
|
|
"Australia/Perth": "Perth",
|
|
"Asia/Irkutsk": "Irkutsk, Ulaanbaatar",
|
|
"Asia/Seoul": "Seoul",
|
|
"Asia/Tokyo": "Osaka, Sapporo, Tokyo",
|
|
"Asia/Yakutsk": "Yakutsk",
|
|
"Australia/Darwin": "Darwin",
|
|
"Australia/Adelaide": "Adelaide",
|
|
"Australia/Sydney": "Canberra, Melbourne, Sydney",
|
|
"Australia/Brisbane": "Brisbane",
|
|
"Australia/Hobart": "Hobart",
|
|
"Asia/Vladivostok": "Vladivostok",
|
|
"Pacific/Guam": "Guam, Port Moresby",
|
|
"Asia/Magadan": "Magadan, Solomon Islands, New Caledonia",
|
|
"Asia/Kamchatka": "Kamchatka, Marshall Islands",
|
|
"Pacific/Fiji": "Fiji Islands",
|
|
"Pacific/Auckland": "Auckland, Wellington",
|
|
"Pacific/Tongatapu": "Nuku'alofa"
|
|
};
|
|
var timezone_list_default = allTimezones;
|
|
|
|
// src/index.tsx
|
|
import { jsx } from "react/jsx-runtime";
|
|
function useTimezoneSelect({
|
|
timezones = timezone_list_default,
|
|
labelStyle = "original",
|
|
displayValue = "GMT",
|
|
currentDatetime
|
|
}) {
|
|
const allOptions = useMemo(() => {
|
|
return Object.entries(timezones).map((zone) => {
|
|
var _a, _b, _c, _d;
|
|
try {
|
|
const now = (currentDatetime ? spacetime(currentDatetime) : spacetime.now()).goto(zone[0]);
|
|
const isDstString = now.isDST() ? "daylight" : "standard";
|
|
const tz = now.timezone();
|
|
const tzStrings = soft(zone[0]);
|
|
const abbr = (_b = (_a = tzStrings == null ? void 0 : tzStrings[0]) == null ? void 0 : _a[isDstString]) == null ? void 0 : _b.abbr;
|
|
const altName = (_d = (_c = tzStrings == null ? void 0 : tzStrings[0]) == null ? void 0 : _c[isDstString]) == null ? void 0 : _d.name;
|
|
const min = tz.current.offset * 60;
|
|
const hr = `${min / 60 ^ 0}:${min % 60 === 0 ? "00" : Math.abs(min % 60)}`;
|
|
const prefix = `(${displayValue}${hr.includes("-") ? hr : `+${hr}`}) ${zone[1]}`;
|
|
let label = "";
|
|
switch (labelStyle) {
|
|
case "original":
|
|
label = prefix;
|
|
break;
|
|
case "altName":
|
|
label = `${prefix} ${altName ? `(${altName})` : ""}`;
|
|
break;
|
|
case "abbrev":
|
|
label = `${prefix} ${abbr ? `(${abbr})` : ""}`;
|
|
break;
|
|
case "offsetHidden":
|
|
label = `${prefix.replace(/^\(.*?\)\s*/, "")}`;
|
|
break;
|
|
default:
|
|
label = `${prefix}`;
|
|
}
|
|
return {
|
|
value: tz.name,
|
|
label,
|
|
offset: tz.current.offset,
|
|
abbrev: abbr,
|
|
altName,
|
|
hasDst: tz.hasDst
|
|
};
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}).filter(Boolean).sort((a, b) => a.offset - b.offset);
|
|
}, [labelStyle, timezones, currentDatetime]);
|
|
const customTimezoneKeys = useMemo(() => {
|
|
const defaultKeys = new Set(Object.keys(timezone_list_default));
|
|
return new Set(Object.keys(timezones).filter((key) => !defaultKeys.has(key)));
|
|
}, [timezones]);
|
|
const options = useMemo(() => {
|
|
return allOptions.filter((item, idx, arr) => {
|
|
if (customTimezoneKeys.has(item.value)) return true;
|
|
return arr.findIndex((t) => t.offset === item.offset && t.hasDst === item.hasDst) === idx;
|
|
}).map((_a) => {
|
|
var _b = _a, { hasDst: _ } = _b, item = __objRest(_b, ["hasDst"]);
|
|
return __spreadProps(__spreadValues({}, item), {
|
|
searchTerms: allOptions.filter((t) => t.offset === item.offset && t.hasDst === _).map((t) => t.label).join(" ")
|
|
});
|
|
});
|
|
}, [allOptions, customTimezoneKeys]);
|
|
const filterOption = (option, inputValue) => {
|
|
var _a, _b, _c;
|
|
const data = option.data;
|
|
const term = inputValue.toLowerCase();
|
|
return ((_a = data.label) == null ? void 0 : _a.toLowerCase().includes(term)) || ((_c = (_b = data.searchTerms) == null ? void 0 : _b.toLowerCase().includes(term)) != null ? _c : false);
|
|
};
|
|
const findFuzzyTz = (zone) => {
|
|
var _a, _b;
|
|
let currentTime;
|
|
try {
|
|
currentTime = (currentDatetime ? spacetime(currentDatetime) : spacetime.now()).goto(zone);
|
|
} catch (err) {
|
|
currentTime = (currentDatetime ? spacetime(currentDatetime) : spacetime.now()).goto("GMT");
|
|
}
|
|
return (_b = (_a = allOptions.filter((tz) => tz.offset === currentTime.timezone().current.offset).map((tz) => {
|
|
let score = 0;
|
|
if (currentTime.timezones[tz.value.toLowerCase()] && !!currentTime.timezones[tz.value.toLowerCase()].dst === currentTime.timezone().hasDst) {
|
|
if (tz.value.toLowerCase().indexOf(currentTime.tz.substring(currentTime.tz.indexOf("/") + 1)) !== -1) {
|
|
score += 8;
|
|
}
|
|
if (tz.label.toLowerCase().indexOf(currentTime.tz.substring(currentTime.tz.indexOf("/") + 1)) !== -1) {
|
|
score += 4;
|
|
}
|
|
if (tz.value.toLowerCase().indexOf(currentTime.tz.substring(0, currentTime.tz.indexOf("/"))) !== -1) {
|
|
score += 2;
|
|
}
|
|
score += 1;
|
|
} else if (tz.value === "GMT") {
|
|
score += 1;
|
|
}
|
|
return { tz, score };
|
|
}).sort((a, b) => b.score - a.score)) == null ? void 0 : _a[0]) == null ? void 0 : _b.tz;
|
|
};
|
|
function isObject(item) {
|
|
return typeof item === "object" && !Array.isArray(item) && item !== null;
|
|
}
|
|
const parseTimezone = (zone) => {
|
|
if (typeof zone === "string") {
|
|
return allOptions.find((tz) => tz.value === zone) || zone.indexOf("/") !== -1 && findFuzzyTz(zone);
|
|
} else if (isObject(zone) && !zone.label) {
|
|
return allOptions.find((tz) => tz.value === zone.value);
|
|
} else {
|
|
return zone;
|
|
}
|
|
};
|
|
return { options, parseTimezone, filterOption };
|
|
}
|
|
var TimezoneSelect = (_a) => {
|
|
var _b = _a, {
|
|
value,
|
|
onBlur,
|
|
onChange,
|
|
labelStyle,
|
|
displayValue,
|
|
timezones,
|
|
currentDatetime
|
|
} = _b, props = __objRest(_b, [
|
|
"value",
|
|
"onBlur",
|
|
"onChange",
|
|
"labelStyle",
|
|
"displayValue",
|
|
"timezones",
|
|
"currentDatetime"
|
|
]);
|
|
const { options, parseTimezone, filterOption } = useTimezoneSelect({
|
|
timezones,
|
|
labelStyle,
|
|
displayValue,
|
|
currentDatetime
|
|
});
|
|
const handleChange = (tz) => {
|
|
if (!onChange) return;
|
|
const _a2 = tz, { searchTerms: _ } = _a2, rest = __objRest(_a2, ["searchTerms"]);
|
|
onChange(rest);
|
|
};
|
|
return /* @__PURE__ */ jsx(
|
|
Select,
|
|
__spreadValues({
|
|
value: parseTimezone(value),
|
|
onChange: handleChange,
|
|
options,
|
|
filterOption,
|
|
onBlur
|
|
}, props)
|
|
);
|
|
};
|
|
export {
|
|
timezone_list_default as allTimezones,
|
|
TimezoneSelect as default,
|
|
useTimezoneSelect
|
|
};
|