UNPKG

@grafana/ui

Version:
141 lines (138 loc) 4.3 kB
import { jsx } from 'react/jsx-runtime'; import { useCallback, useMemo } from 'react'; import { getTimeZoneGroups, getTimeZoneInfo, InternalTimeZones } from '@grafana/data'; import { t } from '@grafana/i18n'; import { Select } from '../Select/Select.mjs'; import { TimeZoneGroup } from './TimeZonePicker/TimeZoneGroup.mjs'; import { formatUtcOffset } from './TimeZonePicker/TimeZoneOffset.mjs'; import { CompactTimeZoneOption, WideTimeZoneOption } from './TimeZonePicker/TimeZoneOption.mjs'; import { getTimeZoneTitle } from './TimeZonePicker/TimeZoneTitle.mjs'; "use strict"; const TimeZonePicker = (props) => { const { onChange, width, autoFocus = false, onBlur, value, includeInternal = false, disabled = false, inputId, menuShouldPortal = true, openMenuOnFocus = false } = props; const groupedTimeZones = useTimeZones(includeInternal); const selected = useSelectedTimeZone(groupedTimeZones, value); const filterBySearchIndex = useFilterBySearchIndex(); const TimeZoneOption = width && width <= 45 ? CompactTimeZoneOption : WideTimeZoneOption; const onChangeTz = useCallback( (selectable) => { if (!selectable || typeof selectable.value !== "string") { return onChange(value); } onChange(selectable.value); }, [onChange, value] ); return /* @__PURE__ */ jsx( Select, { inputId, value: selected, placeholder: t("time-picker.zone.select-search-input", "Type to search (country, city, abbreviation)"), autoFocus, menuShouldPortal, openMenuOnFocus, width, filterOption: filterBySearchIndex, options: groupedTimeZones, onChange: onChangeTz, onBlur, components: { Option: TimeZoneOption, Group: TimeZoneGroup }, disabled, "aria-label": t("time-picker.zone.select-aria-label", "Time zone picker") } ); }; const useTimeZones = (includeInternal) => { const now = Date.now(); const timeZoneGroups = useMemo(() => { return getTimeZoneGroups(includeInternal).map((group) => { const options = group.zones.reduce((options2, zone) => { const info = getTimeZoneInfo(zone, now); if (!info) { return options2; } const name = getTimeZoneTitle(info); options2.push({ label: name, value: info.zone, searchIndex: getSearchIndex(name, info, now) }); return options2; }, []); return { label: group.name, options }; }); }, [includeInternal, now]); return timeZoneGroups; }; const useSelectedTimeZone = (groups, timeZone) => { return useMemo(() => { var _a; if (timeZone === void 0) { return void 0; } const tz = (_a = timeZone == null ? void 0 : timeZone.toLowerCase()) != null ? _a : ""; const group = groups.find((group2) => { if (!group2.label) { return isInternal(tz); } return tz.startsWith(group2.label.toLowerCase()); }); return group == null ? void 0 : group.options.find((option) => { var _a2; if (tz === "") { return option.value === InternalTimeZones.default; } return ((_a2 = option.value) == null ? void 0 : _a2.toLowerCase()) === tz; }); }, [groups, timeZone]); }; const isInternal = (timeZone) => { switch (timeZone) { case InternalTimeZones.default: case InternalTimeZones.localBrowserTime: case InternalTimeZones.utc: return true; default: return false; } }; const useFilterBySearchIndex = () => { return useCallback((option, searchQuery) => { if (!searchQuery || !option.data || !option.data.searchIndex) { return true; } return option.data.searchIndex.indexOf(searchQuery.toLowerCase()) > -1; }, []); }; const getSearchIndex = (label, info, timestamp) => { const parts = [ info.zone.toLowerCase(), info.abbreviation.toLowerCase(), formatUtcOffset(timestamp, info.zone).toLowerCase() ]; if (label !== info.zone) { parts.push(label.toLowerCase()); } for (const country of info.countries) { parts.push(country.name.toLowerCase()); parts.push(country.code.toLowerCase()); } return parts.join("|"); }; export { TimeZonePicker }; //# sourceMappingURL=TimeZonePicker.mjs.map