Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to pass initial value and fixed button type #134

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/timezone-selector-plugin/selector/commons/Button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function Button({
value,
}}
className={`flex items-center gap-2 py-2 text-md ${customClass}`}
type="submit"
type="button"
>
<div className="min-w-0 text-left break-words grow flex space-x-1">
{hideCitiesTooltip ? (
Expand Down
7 changes: 5 additions & 2 deletions js/timezone-selector-plugin/selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import { useEffect, useState } from "preact/hooks";

import Button from "./Button";
import OptionsContainer from "./OptionsContainer";
import { DEFAULT_VALUE } from "./utils";
import { DEFAULT_VALUE, getTimezoneObject } from "./utils";
import { getFromLocalStorage } from "./commons/utils";

function Selector({
className = "",
elementId = "timezone-selector",
initialValue,
position = "bottom",
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChange = (selectedValue) => { },
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onHourFormatChange = (hourFormat) => { },
}) {
const [selectedValue, setSelectedValue] = useState(DEFAULT_VALUE);
const [selectedValue, setSelectedValue] = useState(
getTimezoneObject(initialValue) || DEFAULT_VALUE
);
const [isOverlayVisible, setIsOverlayVisible] = useState(false);
const [is24H, setIs24H] = useState(getFromLocalStorage("ntsp-24-hr-time-format") || false);

Expand Down
5 changes: 4 additions & 1 deletion js/timezone-selector-plugin/selector/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ export const allTimezones = groupedOptions.reduce(
[],
);

export const getTimezoneObject = (ianaTimezone) => ianaTimezone
&& allTimezones.find((timezone) => timezone.utc.includes(ianaTimezone));

const findBrowserTimezone = () => {
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

return allTimezones.find((timezone) => timezone.utc.includes(browserTimezone));
return getTimezoneObject(browserTimezone);
};

export const DEFAULT_VALUE = findBrowserTimezone() || allTimezones[0];
Expand Down