/* * 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 { 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 ( (this.input = node!)} type="text" value={this.props.value || ''} {...inputProps} /> {this.props.value !== undefined && ( )} ); } }