All files / datepicker timepicker.js

4.26% Statements 4/94
0% Branches 0/51
0% Functions 0/17
4.35% Lines 4/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251                                  5x 5x 5x 5x                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
 
import * as React from 'react';
 
import {getOverrides, mergeOverrides} from '../helpers/overrides.js';
import {LocaleContext} from '../locale/index.js';
import {Select, filterOptions} from '../select/index.js';
 
import type {OptionT} from '../select/index.js';
import type {TimePickerPropsT, TimePickerStateT} from './types.js';
 
const MINUTE = 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const NOON = DAY / 2;
 
function dateToSeconds(date: Date) {
  const seconds = date.getSeconds();
  const minutes = date.getMinutes() * MINUTE;
  const hours = date.getHours() * HOUR;
  return seconds + minutes + hours;
}
 
function secondsToHourMinute(seconds) {
  const d = new Date(seconds * 1000);
  return [d.getUTCHours(), d.getUTCMinutes()];
}
 
function secondsToLabel(seconds, format) {
  let [hours, minutes] = secondsToHourMinute(seconds);
  const zeroPrefix = n => (n < 10 ? `0${n}` : n);
 
  if (format === '12') {
    const isAfterNoon = seconds >= NOON;
    if (isAfterNoon) {
      hours -= 12;
    }
 
    if (hours === 0) {
      hours = 12;
    }
 
    return `${hours}:${zeroPrefix(minutes)} ${isAfterNoon ? 'PM' : 'AM'}`;
  }
 
  return `${zeroPrefix(hours)}:${zeroPrefix(minutes)}`;
}
 
/**
 * Converts a time string, e.g. 10:00, to one or more possible TimePicker
 * options representing that time.
 */
function stringToOptions(
  str: string,
  format: '12' | '24' = '12',
): Array<OptionT> {
  // leading zero is optional, AM/PM is optional
  const twelveHourRegex = /^(1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]?)?$/;
  // leading zero is optional
  const twentyFourHourRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/;
  const regex = format === '12' ? twelveHourRegex : twentyFourHourRegex;
  const match = str.match(regex);
  if (!match) {
    return [];
  }
  const hours = Number(match[1]);
  const minutes = Number(match[2]);
 
  let hoursMinutes = [];
  switch (format) {
    case '24': {
      hoursMinutes = [{hours, minutes}];
      break;
    }
    case '12':
    default: {
      const twelveHours = hours % 12;
      const meridiem = match[3];
      // if there's no AM/PM, add both AM and PM options
      if (!meridiem) {
        hoursMinutes = [
          {hours: twelveHours, minutes},
          {hours: twelveHours + 12, minutes},
        ];
      } else {
        const twentyFourHours =
          meridiem.toLowerCase()[0] === 'a' ? twelveHours : twelveHours + 12;
 
        hoursMinutes = [{hours: twentyFourHours, minutes}];
      }
      break;
    }
  }
 
  return hoursMinutes.map(({hours, minutes}) => {
    const secs = hours * 3600 + minutes * 60;
    return {id: secs, label: secondsToLabel(secs, format)};
  });
}
 
class TimePicker extends React.Component<TimePickerPropsT, TimePickerStateT> {
  static defaultProps = {
    format: '12',
    step: 900,
    creatable: false,
  };
 
  state = {steps: [], value: null};
 
  componentDidMount() {
    const steps = this.buildSteps();
    if (this.props.value instanceof Date) {
      this.setState({
        steps: steps,
        value: this.buildSelectedOption(this.props.value, this.props.format),
      });
    } else {
      const seconds = dateToSeconds(new Date());
      let closestStep = NOON;
      steps.forEach(step => {
        if (Math.abs(step - seconds) < Math.abs(closestStep - seconds)) {
          closestStep = step;
        }
      });
      this.setState({
        steps: steps,
        value: {id: closestStep, label: secondsToLabel(closestStep)},
      });
      this.handleChange(closestStep);
    }
  }
 
  componentDidUpdate(prevProps: TimePickerPropsT) {
    const formatChanged = prevProps.format !== this.props.format;
    const stepChanged = prevProps.step !== this.props.step;
    if (formatChanged || stepChanged) {
      const steps = this.buildSteps();
      this.setState({steps});
    }
  }
 
  handleChange = (seconds: number) => {
    const date = this.props.value || new Date();
    const [hours, minutes] = secondsToHourMinute(seconds);
    date.setHours(hours, minutes, 0);
    this.props.onChange && this.props.onChange(date);
  };
 
  buildSteps = () => {
    const {step = 900} = this.props;
 
    let stepCount = DAY / step;
    if (!Number.isInteger(stepCount)) {
      const previousStepCount = stepCount;
      stepCount = Math.round(stepCount);
 
      if (__DEV__) {
        // eslint-disable-next-line no-console
        console.warn(
          `Provided step value (${step}) does not spread evenly across a day. Rounding from ${previousStepCount} total steps to ${stepCount}.`,
        );
      }
    }
 
    const options = [];
    for (let i = 0; i < DAY; i += step) {
      options.push(i);
    }
    return options;
  };
 
  creatableFilterOptions: typeof filterOptions = (
    options,
    filterValue,
    excludeOptions,
    newProps,
  ) => {
    const result = stringToOptions(filterValue, this.props.format);
    if (result.length) {
      return result;
    }
    return filterOptions(options, filterValue, excludeOptions, newProps);
  };
 
  buildSelectedOption(value: Date, format: '12' | '24' = '12') {
    const secs = dateToSeconds(value);
    return {
      id: secs,
      label: secondsToLabel(secs, format),
    };
  }
 
  render() {
    const {overrides = {}} = this.props;
 
    const [OverriddenSelect, selectProps] = getOverrides(
      overrides.Select,
      Select,
    );
    const selectOverrides = mergeOverrides(
      {Dropdown: {style: {maxHeight: '126px'}}},
      // $FlowFixMe
      selectProps && selectProps.overrides,
    );
    // $FlowFixMe
    selectProps.overrides = selectOverrides;
 
    const value =
      this.props.value instanceof Date
        ? this.buildSelectedOption(this.props.value, this.props.format)
        : this.state.value;
 
    return (
      <LocaleContext.Consumer>
        {locale => (
          <OverriddenSelect
            aria-label={locale.datepicker.timePickerAriaLabel}
            options={this.state.steps.map(n => ({
              id: n,
              label: secondsToLabel(n, this.props.format),
            }))}
            filterOptions={
              this.props.creatable ? this.creatableFilterOptions : undefined
            }
            onChange={params => {
              this.setState({value: params.value[0]});
 
              const date = this.props.value || new Date();
              const seconds = params.value[0].id;
              const [hours, minutes] = secondsToHourMinute(seconds);
              date.setHours(hours, minutes, 0);
              this.handleChange(params.value[0].id);
            }}
            value={value}
            clearable={false}
            {...selectProps}
          />
        )}
      </LocaleContext.Consumer>
    );
  }
}
 
export default TimePicker;