All files / datepicker timezone-picker.js

0% Statements 0/45
0% Branches 0/43
0% Functions 0/11
0% Lines 0/40

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                                                                                                                                                                                                                                                                               
/*
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.
*/
// global Intl
// @flow
 
import * as React from 'react';
import {
  findTimeZone,
  getZonedTime,
  listTimeZones,
} from 'timezone-support/dist/index-2012-2022.js';
import {formatZonedTime} from 'timezone-support/dist/parse-format.js';
 
import {getOverrides, mergeOverrides} from '../helpers/overrides.js';
import {LocaleContext} from '../locale/index.js';
import {Select} from '../select/index.js';
 
import type {TimezonePickerPropsT, TimezonePickerStateT} from './types.js';
 
class TimezonePicker extends React.Component<
  TimezonePickerPropsT,
  TimezonePickerStateT,
> {
  state = {timezones: [], value: null};
 
  componentDidMount() {
    const timezones = this.buildTimezones(this.props.date || new Date());
 
    if (__BROWSER__) {
      const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
      this.setState({timezones, value: tz});
 
      const option = timezones.find(o => o.id === tz);
      option && this.props.onChange && this.props.onChange(option);
    } else {
      this.setState({timezones});
    }
  }
 
  componentDidUpdate(prevProps: TimezonePickerPropsT) {
    const prevTime = prevProps.date ? prevProps.date.getTime() : 0;
    const nextTime = this.props.date ? this.props.date.getTime() : 0;
    if (prevTime !== nextTime) {
      const timezones = this.buildTimezones(this.props.date || new Date());
      this.setState({timezones});
 
      const option = timezones.find(o => o.id === this.state.value);
      option && this.props.onChange && this.props.onChange(option);
    }
  }
 
  buildTimezones = (compareDate: Date) => {
    const timezones = listTimeZones()
      .map(zone => {
        const timezone = findTimeZone(zone);
        const zonedTime = getZonedTime(compareDate, timezone);
        const formatted = formatZonedTime(
          zonedTime,
          `z - [${zone}] ([GMT] Z)`,
        ).replace('_', ' ');
 
        const option = {
          id: zone,
          label: formatted,
          offset: zonedTime.zone.offset,
        };
        if (this.props.mapLabels) {
          option.label = this.props.mapLabels(option);
        }
        return option;
      })
      // Removes 'noisy' timezones without a letter acronym.
      .filter(option => option.label[0] !== '-' && option.label[0] !== '+')
      // Sorts W -> E, prioritizes america. could be more nuanced based on system tz but simple for now
      .sort((a, b) => {
        const offsetDelta = b.offset - a.offset;
        if (offsetDelta !== 0) return offsetDelta;
 
        if (a.label < b.label) return -1;
        if (a.label > b.label) return 1;
        return 0;
      });
    return timezones;
  };
 
  render() {
    const {overrides = {}} = this.props;
    const [OverriddenSelect, selectProps] = getOverrides(
      overrides.Select,
      Select,
    );
    const selectOverrides = mergeOverrides(
      {
        Dropdown: {style: {maxHeight: '360px'}},
      },
      // $FlowFixMe
      selectProps && selectProps.overrides,
    );
    // $FlowFixMe
    selectProps.overrides = selectOverrides;
 
    return (
      <LocaleContext.Consumer>
        {locale => (
          <OverriddenSelect
            aria-label={locale.datepicker.timezonePickerAriaLabel}
            options={this.state.timezones}
            clearable={false}
            onChange={params => {
              if (params.type === 'clear') {
                this.setState({value: ''});
                this.props.onChange && this.props.onChange(null);
              } else {
                this.setState({value: params.option.id});
                this.props.onChange && this.props.onChange(params.option);
              }
            }}
            value={
              this.props.value || this.state.value
                ? [{id: this.props.value || this.state.value}]
                : null
            }
            {...selectProps}
          />
        )}
      </LocaleContext.Consumer>
    );
  }
}
 
export default TimezonePicker;