Giter Site home page Giter Site logo

thattimc / react-timezone-select Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ndom91/react-timezone-select

0.0 0.0 0.0 1.88 MB

An extremely usable and dynamic React timezone selector

Home Page: https://ndom91.github.io/react-timezone-select/

License: MIT License

JavaScript 14.43% HTML 14.30% TypeScript 68.28% CSS 3.00%

react-timezone-select's Introduction

๐ŸŒโŒš react-timezone-select

Bundlephobia NPM Downloads npm GitHub issues Test CI MIT

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:

1) Adjusted the choices automatically with Daylight Savings Time (DST)
2) Didn't have a huge list of choices to scroll through when technically only 24 (ish) are necessary

Update: v0.7+ now built with spacetime instead of moment.js, reducing bundle size by ~66%!
Update: v0.10+ now built with Typescript!

This demo is also available in the ./examples directory. Simply run npm start after installing everything and the webpack dev server will begin, where you can find the demo at localhost:3001.

We also have some examples available on Codesandbox using this component with the datetime library spacetime as well as with moment, showing how one might use this component in a real application.

๐Ÿ—๏ธ Installing

npm install react-timezone-select

๐Ÿ”ญ Usage

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 className='code'>
        <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)

๐Ÿ•น๏ธ Props

  • 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
    • Example timezone parameter:
     {
       value: 'America/Juneau'
       label: '(GMT-8:00) Alaska,
       abbrev: 'AHST',
       altName: 'Alaskan Standard Time'
     }
    
  • labelStyle - 'original' | 'altName' | 'abbrev'
  • timezones - Custom Timezone Object - see below..
  • Any other react-select props

๐Ÿ•’ Custom Timezones

New in v0.9.11+ we've shipped a prop to allow users to either fully replace the timezone options or append custom choices of their own.

The timezones prop takes a dictionary of timezones, i.e. an object where the key/value format is: { 'IANA Timezone Name' : 'Your Label' } - don't worry we'll prepend the (GMT...) part, just pass the city(s) or region(s) you want in your label.

For example:

import TimezoneSelect, { i18nTimezones } from 'react-timezone-select'
...

<TimezoneSelect
  value={selectedTimezone}
  onChange={setSelectedTimezone}
  timezones={{
     ...i18nTimezones,
    'America/Lima': 'Pittsburgh',
    'Europe/Berlin': 'Frankfurt',
  }}
/>

This will generate two additional choices in our dropdown, one with the label '(GMT-5:00) Pittsburgh' and another with '(GMT+1:00) Frankfurt'. One could also omit spreading in the i18nTimezones object and pass in ones own completely custom list of timezone choices.

๐Ÿšง Contributing

Pull requests are always welcome! Please stick to the prettier settings, and if adding new features, please consider adding test(s) and some notes in the README, where appropriate.

๐Ÿ™ Thanks

react-timezone-select's People

Contributors

ndom91 avatar dependabot[bot] avatar frecap avatar braingu-matt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.