Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
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:
This demo is also available in the
./examples
directory. Simply runnpm start
in the root of the repository after installing everything in the examples subdirectory and snowpack dev will begin, where you can find the demo atlocalhost:8080
.
We also have some more examples available on Codesandbox using this component with the datetime library spacetime
(example) as well as with moment
(example), showing how one might use this component in a real application.
npm install react-timezone-select
import React, { useState } from "react"
import ReactDOM from "react-dom"
import TimezoneSelect from "react-timezone-select"
const App = () => {
const [selectedTimezone, setSelectedTimezone] = useState({})
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)
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
)
Thanks @ndrwksr!
For now, Next.js isn't great about handling ESM packages. Until this gets fixed, there is a workaround for using this and other ESM packages via next-transpile-modules
.
// next.config.js
const withTM = require("next-transpile-modules")(["react-timezone-select"])
module.exports = withTM({
// ...further Next.js config
})
Update: Next.js 11.1.0 now supports ESM packages natively! Until 12.0.0 you need to include esmExternals: true
in your next.js.config
though.
You can append custom choices of your own, or fully replace the listed timezone options.
The timezones
prop takes a dictionary of timezones. Don't worry, we'll prepend the (GMT...)
part, you just have to pass the city(s) or region(s) you want in your label.
import TimezoneSelect, { i18nTimezones } from "react-timezone-select"
;<TimezoneSelect
value={selectedTimezone}
onChange={setSelectedTimezone}
timezones={{
...i18nTimezones,
"America/Lima": "Pittsburgh",
"Europe/Berlin": "Frankfurt",
}}
/>
The above example will generate two additional choices in the select options, one with the label '(GMT-5:00) Pittsburgh'
and another with '(GMT+1:00) Frankfurt'
. You can omit spreading in the i18nTimezones
object and then only your custom timezone options get rendered in the select component.
value
- Initial Timezone string
, i.e. 'Europe/Amsterdam'
or the full object from the onChange function: { value: string, label: string, abbrev: string, altName: string }
onBlur
- () => void
onChange
- (timezone) => void
timezone
parameter: {
value: 'America/Juneau'
label: '(GMT-8:00) Alaska,
abbrev: 'AHST',
offset: -8,
altName: 'Alaskan Standard Time'
}
labelStyle
- 'original' | 'altName' | 'abbrev'
timezones
- Custom Timezone Objectreact-select
propsPull requests are always welcome! Please stick to the prettier
settings, and if adding new features, please consider adding test(s) and documentation where appropriate!
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.
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.