Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-timezone-select
Advanced tools
Another react timezone select component, I know.. However this one has a few key benefits!
While looking around for a good option, I had trouble finding a timezone select components which:
[!IMPORTANT]
Demo: ndom91.github.io/react-timezone-select
This demo is also available in the
./examples
directory. Simply runpnpm dev
in the root of the repository and the vite dev server will start, where you can then find the example app atlocalhost:3001
.
npm install react-timezone-select react-select
[!CAUTION] The package
react-select
is optional. It is unnecessary if you're only using the hook.
import React, { useState } from "react"
import ReactDOM from "react-dom"
import TimezoneSelect, { type ITimezone } from "react-timezone-select"
const App = () => {
const [selectedTimezone, setSelectedTimezone] = useState<ITimezone>(
Intl.DateTimeFormat().resolvedOptions().timeZone,
)
return (
<div className="App">
<h2>react-timezone-select</h2>
<blockquote>Please make a selection</blockquote>
<div className="select-wrapper">
<TimezoneSelect value={selectedTimezone} onChange={setSelectedTimezone} />
</div>
<h3>Output:</h3>
<div
style={{
backgroundColor: "#ccc",
padding: "20px",
margin: "20px auto",
borderRadius: "5px",
maxWidth: "600px",
}}
>
<pre
style={{
margin: "0 20px",
fontWeight: 500,
fontFamily: "monospace",
}}
>
{JSON.stringify(selectedTimezone, null, 2)}
</pre>
</div>
</div>
)
}
const rootElement = document.getElementById("root")
ReactDOM.render(<App />, rootElement)
By default, react-timezone-select
uses 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.
import { useTimezoneSelect, allTimezones } from "react-timezone-select"
const labelStyle = "original"
const timezones = {
...allTimezones,
"Europe/Berlin": "Frankfurt",
}
const customSelect = () => {
const { options, parseTimezone } = useTimezoneSelect({ labelStyle, timezones })
return (
<select onChange={(e) => onChange(parseTimezone(e.currentTarget.value))}>
{options.map((option) => (
<option value={option.value}>{option.label}</option>
))}
</select>
)
}
Prop | Type | Default | Note |
---|---|---|---|
value | string | ITimezoneOption | null | Initial/current Timezone |
onBlur | () => void | null | |
onChange | (timezone: ITimezoneOption) => void | null | |
labelStyle | 'original' | 'altName' | 'abbrev' | 'offsetHidden' | 'original' | |
displayValue | 'GMT' | 'UTC' | 'GMT' | Prefix for the label (i.e. "(GMT+2:00)" or "(UTC+2:00)" ) |
timezones | Record | allTimezones | |
currentDatetime | Date | string | null | Override datetime used to calculate timezone values (alternative to current datetime), useful for calculating different summer / winter times, etc. |
value
s:// string
value='America/Juneau'
// ITimezoneOption; i.e. `onChange` return value
value={{
value: 'America/Juneau'
label: '(GMT-8:00) Alaska,
abbrev: 'AHST',
offset: -8,
altName: 'Alaskan Standard Time'
}}
timezones
:timezones={{
...allTimezones,
'America/Lima': 'Pittsburgh',
'Europe/Berlin': 'Frankfurt',
}}
If you'd like the user's own timezone to be set as the initially selected option on render, we can make use of the new Intl
browser API by setting the default state value to Intl.DateTimeFormat().resolvedOptions().timeZone
.
const [timezone, setTimezone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone)
You can append custom choices of your own, or fully replace the listed timezone options.
The timezones
prop takes a dictionary of timezones in the format of "{ tzIdentifier: Label }
" (Timezone Identifiers).
import TimezoneSelect, { type ITimezone, allTimezones } from 'react-timezone-select'
const [selectedTimezone, setSelectedTimezone] = useState<ITimezone>('Europe/Berlin')
<TimezoneSelect
value={selectedTimezone}
onChange={setSelectedTimezone}
timezones={{
...allTimezones,
'America/Lima': 'Pittsburgh',
'Europe/Berlin': 'Frankfurt',
}}
/>
The example above will include all original timezones and generate two additional choices:
'(GMT-5:00) Pittsburgh'
'(GMT+1:00) Frankfurt'
We'll prepend the correct (GMT...)
part to the generated label, you just have to provide the string you want in your label. Also, you can omit spreading in the allTimezones
object for a select dropdown consisting of only your custom choices.
Pull requests are always welcome! Please stick to repo formatting/linting settings, and if adding new features, please consider adding test(s) and documentation where appropriate!
MIT
FAQs
Usable, dynamic React Timezone Select
The npm package react-timezone-select receives a total of 47,018 weekly downloads. As such, react-timezone-select popularity was classified as popular.
We found that react-timezone-select demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.