npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-time-input-polyfill

v1.0.11

Published

A pre-built, plug-and-play, fully accessible React component that will produce an `input[type='time']` element with a built in polyfill for IE and Safari support.

Downloads

5,329

Readme

{DEPRECATED} react-time-input-polyfill

IMPORTANT! This package has been renamed

The new name for the polyfill is:

@time-input-polyfill/react

You can install the new version using:

npm i @time-input-polyfill/react

Version 1 ("react-time-input-polyfill") is not supported anymore, please migrate to version 2.

Migration details

There were breaking changes made in v2.0.0 for the sake of making the usage of the component more streamlined.

See the v2 release notes for more details.

DEPRECATED documentation...

hits per month badge

This is a pre-built, plug-and-play, fully accessible React component that will produce an <input type="time"> element with a built in polyfill for IE and Safari support.

  • ✔️ Modeled after the Chrome 78 and Firefox 70 desktop implementations.
  • ✔️ Fully keyboard and screen reader accessible.
  • ✔️ Sends back the same values as real time inputs (24 hour time).
  • ✔️ Only downloads the full polyfill code in the browsers that need it

You may have already come across the plain JavaScript version. This is not just a wrapper component though. This package was built from the ground up in React, for React. It does import some functionality from the original though where it made sense to.

You can view a demo of react-time-input-polyfill in action here: https://dan503.github.io/react-time-input-polyfill/

You can view a demo of the original plain javascript version here: https://dan503.github.io/time-input-polyfill/

Install

The component needs an ES6 compatible environment to run in. It also needs React installed on the project. Take a look at create-react-app to get started with React.

You can then install this polyfill component with npm:

npm i react-time-input-polyfill

Usage

/* TimeInput.js */

import React from 'react'

// Import the component into your project
import TimeInputPolyfill from 'react-time-input-polyfill'

export const TimeInput = ({ label, currentValue, onInputChange }) => {
    return (
        <label>
            <span>{label}</span>
            <TimeInputPolyfill

                // set the value through props
                value={currentValue}

                // onChange will run every time the value is updated
                onChange={({ value, element }) => {
                    console.log({

                        // The current value in 24 hour time format
                        value,

                        // The <input> HTML element
                        element,

                    })

                    // Export the new value to the parent component
                    onInputChange(value)
                }}
            />
        </label>
    )
}
/* ExampleForm.js */

import React, { useState } from 'react'

// import your local time input component into your form component
import { TimeInput } from './TimeInput'

export const ExampleForm = ()=> {

    // Use state to keep track of the value
    const [inputValue, setInputValue] = useState('20:30') // default to 8:30 PM

    return (
        <form>
            <TimeInput
                label="Label text"

                // Use the state value to set the time
                currentValue={inputValue}

                // Use the set state function to update the time when it changes
                onInputChange={ newValue => setInputValue(newValue) }
            />
            <button type="submit">Submit</button>
        </form>
    )
}

You can also force-enable the polyfill so that it is active in modern browsers that support <input type="time"> natively. This is helpful when it comes to debugging since it gives you access to modern dev tools (just make sure to disable it again when you are done).

/* TimeInput.js */

import React from 'react'
import TimeInputPolyfill from 'react-time-input-polyfill'

export const TimeInput = ({ label, currentValue, onInputChange }) => {
    return (
        <label>
            <span>{label}</span>
            <TimeInputPolyfill
                value={currentValue}

                /*  Force browsers that support input[type=time]
                    to use the polyfill.
                    (useful for testing and debugging)
                */  forcePolyfill={true}

                onChange={({ value, element }) => {
                    onInputChange(value)
                }}
            />
        </label>
    )
}

Content Security Policy (CSP) work around

The way that the polyfill avoids downloading the full polyfill code in modern browsers is by injecting the following script tag onto the page:

<script src="https://cdn.jsdelivr.net/npm/react-time-input-polyfill@1/dist/timePolyfillHelpers.js"></script>

That downloads the extra helper functions that the polyfill needs to function.

Your CSP might not allow for this.

To work around the issue, first create a timePolyfillHelpers.js file and ensure that whatever you are using to compile your JS also compiles this file as it's own separate thing. Don't import it into your main js file.

// timePolyfillHelpers.js

// ES5
require('react-time-input-polyfill/timePolyfillHelpers.js')

// ES6
import 'react-time-input-polyfill/timePolyfillHelpers.js'

Then when using the component, add a polyfillSource prop that points to the compiled helpers file on your server.

<TimeInput
    value={currentValue}
    onChange={({ value }) => setCurrentValue(value)}
    polyfillSource="/path/to/local/timePolyfillHelpers.js"
/>