Skip to content

10xjs/date-input-controller

Repository files navigation

@10xjs/date-input-controller

A helper component for creating datetime inputs in React.

Build Status Coverage Status

import DateInputController from '@10xjs/date-input-controller';

const range = (start, end) => {
  return Array(...Array(1 + end - start)).map((_, i) => start + i);
}

const DateInput = ({value, min, max, onChange}) => (
  <DateInputController value={value} min={min} max={max} onChange={onChange}>
    {(props) => (
      <div>
        year{" "}
        <select value={props.year} onChange={(e) => props.setYear(e.target.value)}>
          {range(props.yearMin, props.yearMax).map((value) => (
            <option key={value} value={value}>{value}</option>
          ))}
        </select>{" "}

        month{" "}
        <select value={props.month} onChange={(e) => props.setMonth(e.target.value)}>
          {range(props.monthMin, props.monthMax).map((value) => (
            <option key={value} value={value}>
              {new Date(1970, value, 1).toLocaleString(
                'en-US',
                { month: "long" }
              )}
            </option>
          ))}
        </select>{" "}

        day{" "}
        <select value={props.day} onChange={(e) => props.setDay(e.target.value)}>
          {range(props.dayMin, props.dayMax).map((value) => (
            <option key={value} value={value}>{value}</option>
          ))}
        </select>{" "}

        <br/>

        time{" "}
        <select value={props.hour} onChange={(e) => props.setHour(e.target.value)}>
          {range(props.hourMin, props.hourMax).map((value) => (
            <option key={value} value={value}>
              {value.toLocaleString('en-US', {minimumIntegerDigits: 2})}
            </option>
          ))}
        </select>
        :
        <select value={props.minute} onChange={(e) => props.setMinute(e.props.target.value)}>
          {range(props.minuteMin, props.minuteMax).map((value) => (
            <option key={value} value={value}>
              {value.toLocaleString('en-US', {minimumIntegerDigits: 2})}
            </option>
          ))}
        </select>
        :
        <select value={props.second} onChange={(e) => props.setSecond(e.target.value)}>
          {range(props.secondMin, props.secondMax).map((value) => (
            <option key={value} value={value}>
              {value.toLocaleString('en-US', {minimumIntegerDigits: 2})}
            </option>
          ))}
        </select>
      </div>
    )}
  <DateInputController>
);

About

A helper component for creating datetime inputs in React.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy