aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-ui/src/nfvo-components/datepicker/Datepicker.jsx
blob: b4bc8be9ec499198e66660f91bce7373ad478f58 (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
import React from 'react';
import PropTypes from 'prop-types';
import DatePicker from 'react-datepicker';
import moment from 'moment';
import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';

class CustomInput extends React.Component {
    static propTypes = {
        placeHolderText: PropTypes.string,
        onChange: PropTypes.func,
        onClick: PropTypes.func,
        value: PropTypes.string
    };

    render() {
        const {
            placeholderText,
            onClick,
            onClear,
            inputRef,
            value: date
        } = this.props;
        const text = date ? date : placeholderText;
        const textStyle = date ? '' : 'placeholder';
        return (
            <div
                onClick={onClick}
                ref={inputRef}
                className="datepicker-custom-input">
                <div className={`datepicker-text ${textStyle}`}>{text}</div>
                {date && (
                    <SVGIcon
                        onClick={e => {
                            e.stopPropagation();
                            onClear();
                        }}
                        name="close"
                        className="clear-input"
                    />
                )}
                <SVGIcon name="calendar" />
            </div>
        );
    }
}

const parseDate = (date, format) => {
    return typeof date === 'number' ? moment.unix(date) : moment(date, format);
};

class Datepicker extends React.Component {
    static propTypes = {
        date: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        format: PropTypes.string,
        onChange: PropTypes.func,
        selectsStart: PropTypes.bool,
        selectsEnd: PropTypes.bool,
        startDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        endDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
        disabled: PropTypes.bool,
        label: PropTypes.string,
        isRequired: PropTypes.bool
    };
    render() {
        let {
            date,
            format,
            onChange,
            selectsStart = false,
            startDate = null,
            endDate = null,
            selectsEnd = false,
            disabled = false,
            inputRef
        } = this.props;
        const placeholderText = 'Enter a date';
        const props = {
            format,
            onChange,
            disabled,
            selected: date ? parseDate(date, format) : date,
            selectsStart,
            selectsEnd,
            placeholderText,
            startDate: startDate ? parseDate(startDate, format) : startDate,
            endDate: endDate ? parseDate(endDate, format) : endDate
        };

        return (
            <div className="customized-date-picker">
                <DatePicker
                    calendarClassName="customized-date-picker-calendar"
                    customInput={
                        <CustomInput
                            inputRef={inputRef}
                            onClear={() => onChange(undefined)}
                            placeholderText={placeholderText}
                        />
                    }
                    minDate={selectsEnd && props.startDate}
                    maxDate={selectsStart && props.endDate}
                    {...props}
                />
            </div>
        );
    }
}

export default Datepicker;