aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/controls/DateInput.tsx
blob: 87f87c5dd4d2e6cd394a7c156182cbb86ffd520f (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import * as $ from 'jquery';
import * as React from 'react';
import * as classNames from 'classnames';
import { pick } from 'lodash';
import * as theme from '../../app/theme';
import ClearIcon from '../icons-components/ClearIcon';
import { ButtonIcon } from '../ui/buttons';
import './styles.css';

interface Props {
  className?: string;
  format?: string;
  inputClassName?: string;
  // see http://api.jqueryui.com/datepicker/#option-maxDate for details
  maxDate?: Date | string | number;
  minDate?: Date | string | number;
  name: string;
  onChange: (value?: string) => void;
  placeholder: string;
  value?: string;
}

export default class DateInput extends React.PureComponent<Props> {
  input: HTMLInputElement;

  static defaultProps = {
    format: 'yy-mm-dd',
    maxDate: '+0'
  };

  componentDidMount() {
    this.attachDatePicker();
  }

  componentDidUpdate(prevProps: Props) {
    if ($.fn && ($.fn as any).datepicker && this.input) {
      if (prevProps.maxDate !== this.props.maxDate) {
        ($(this.input) as any).datepicker('option', { maxDate: this.props.maxDate });
      }
      if (prevProps.minDate !== this.props.minDate) {
        ($(this.input) as any).datepicker('option', { minDate: this.props.minDate });
      }
    }
  }

  handleChange = () => {
    const { value } = this.input;
    this.props.onChange(value);
  };

  handleResetClick = () => {
    this.props.onChange(undefined);
  };

  attachDatePicker() {
    const opts = {
      dateFormat: this.props.format,
      changeMonth: true,
      changeYear: true,
      maxDate: this.props.maxDate,
      minDate: this.props.minDate,
      onSelect: this.handleChange
    };

    if ($.fn && ($.fn as any).datepicker && this.input) {
      ($(this.input) as any).datepicker(opts);
    }
  }

  render() {
    const inputProps: { name?: string; placeholder?: string } = pick(this.props, [
      'placeholder',
      'name'
    ]);

    return (
      <span className={classNames('date-input-control', this.props.className)}>
        <input
          className={classNames('date-input-control-input', this.props.inputClassName)}
          onChange={this.handleChange}
          readOnly={true}
          ref={node => (this.input = node!)}
          type="text"
          value={this.props.value || ''}
          {...inputProps}
        />
        <span className="date-input-control-icon">
          <svg width="14" height="14" viewBox="0 0 16 16">
            <path d="M5.5 6h2v2h-2V6zm3 0h2v2h-2V6zm3 0h2v2h-2V6zm-9 6h2v2h-2v-2zm3 0h2v2h-2v-2zm3 0h2v2h-2v-2zm-3-3h2v2h-2V9zm3 0h2v2h-2V9zm3 0h2v2h-2V9zm-9 0h2v2h-2V9zm11-9v1h-2V0h-7v1h-2V0h-2v16h15V0h-2zm1 15h-13V4h13v11z" />
          </svg>
        </span>
        {this.props.value !== undefined && (
          <ButtonIcon
            className="button-tiny date-input-control-reset"
            color={theme.gray60}
            onClick={this.handleResetClick}>
            <ClearIcon size={12} />
          </ButtonIcon>
        )}
      </span>
    );
  }
}