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

date-input-control

v0.1.0

Published

Capture dates using day, month and year components

Downloads

533

Readme

Date input control

Create a date input control for capturing dates in any format:

  • DD-MM-YYYY
  • MM-DD-YYYY
  • YYYY-MM-DD
  • MM-YY
  • etc.

What this does

This package allows you to create a single date input control using a sequence of numeric input boxes. This pattern is especially useful for date of birth capture where a calendar-based date picker may be cumbersome to use. It will:

  • Move focus from one field to the next as the user types
  • Move focus if pressing a date separator character (, or .. or /)
  • Move focus between fields when pressing Backspace or Delete
  • Move focus between fields when pressing left or right arrow keys
  • Prevent the entry of non-numeric characters

What this doesn't do

  • Validation

    You'll need to implement your own validation logic to check that the date parts are valid (e.g. month cannot be 0, day cannot be 99) and that the date itself is valid (e.g. date cannot be February 31st).

  • Create elements

    All this package does is attach event listeners to elements that you provide. You'll need to create your own numeric year, month and day fields.

How to use it

First you will need some input fields. There are two requirements in order for this package to function correctly:

  1. Inputs must be of type text. You cannot use type number.
  2. Inputs must have a maxLength attribute.

It is recommended that you add inputMode="numeric" and pattern="[0-9*]" on each input which will enable the numeric keyboard on mobile devices.

<fieldset role="group">
  <legend>Date of birth</legend>
  <input type="text" maxlength="2" id="dd" inputMode="numeric" pattern="[0-9]*">
  <input type="text" maxlength="2" id="mm" inputMode="numeric" pattern="[0-9]*">
  <input type="text" maxlength="4" id="yyyy" inputMode="numeric" pattern="[0-9]*">
</fieldset>

Install the date-input-control package in your project.

$ npm install date-input-control

Import createDateInputControl and call it with an array of input element refs.

import { createDateInputControl } from 'date-input-control';

const dd = document.getElementById('dd');
const mm = document.getElementById('mm');
const yyyy = document.getElementById('yyyy');

createDateInputControl([dd, mm, yyyy]);

The order of the inputs provided determines the focus behaviour.

// For DD-MM-YYYY
createDateInputControl([dd, mm, yyyy]);
// For MM-DD-YYYY
createDateInputControl([mm, dd, yyyy]);
// For YYYY-MM-DD
createDateInputControl([yyyy, mm, dd]);

You can provide as many or as few inputs as required.

// For credit card expiry date
createDateInputControl([mm, yyyy]);
// For capturing date and time
createDateInputControl([yyyy, mm, dd, hh, ss]);

You can also provide a NodeList.

const inputs = document.querySelectorAll('.date input');
createDateInputControl(inputs);

To remove the listeners that were attached to your input elements you can call the returned function.

const unsubscribe = createDateInputControl(inputs);
unsubscribe();