Skip to content

Commit f690be2

Browse files
committed
use TextInput as Timepicker
1 parent 3c623cb commit f690be2

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

src/components/widgets/DateTimePicker.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@ import {CalendarMultiselectIcon, RecentIcon} from 'plotly-icons';
33
import {ms2DateTime, dateTime2ms} from 'plotly.js/src/lib/dates';
44
import DayPicker from 'react-day-picker';
55
import PropTypes from 'prop-types';
6-
import React, {Component, Fragment} from 'react';
6+
import React, {Component} from 'react';
77
import TextInput from './TextInput';
88
import Dropdown from './Dropdown';
99

10+
function formatDigits(number, nbDigits) {
11+
const string = number.toString();
12+
if (string.length !== nbDigits) {
13+
return (
14+
new Array(nbDigits - string.length)
15+
.fill(0)
16+
.join()
17+
.replace(',', '') + number
18+
);
19+
}
20+
return number;
21+
}
22+
1023
export default class DateTimePicker extends Component {
1124
constructor() {
1225
super();
@@ -19,6 +32,7 @@ export default class DateTimePicker extends Component {
1932
this.toPlotlyJSDate = this.toPlotlyJSDate.bind(this);
2033
this.onMonthChange = this.onMonthChange.bind(this);
2134
this.onYearChange = this.onYearChange.bind(this);
35+
this.updateTime = this.updateTime.bind(this);
2236
}
2337

2438
componentWillReceiveProps(nextProps) {
@@ -41,10 +55,12 @@ export default class DateTimePicker extends Component {
4155

4256
getYearOptions(current) {
4357
const yearAsNumber = parseInt(current, 10);
58+
// eslint-disable-next-line
4459
const lastFive = new Array(5).fill(0).map((year, index) => {
45-
const newOption = yearAsNumber - (5 - index);
60+
const newOption = yearAsNumber - (5 - index); // eslint-disable-line
4661
return {label: newOption, value: newOption};
4762
});
63+
// eslint-disable-next-line
4864
const nextFive = new Array(5).fill(0).map((year, index) => {
4965
const newOption = yearAsNumber + (index + 1);
5066
return {label: newOption, value: newOption};
@@ -82,12 +98,21 @@ export default class DateTimePicker extends Component {
8298
this.props.onChange(this.toPlotlyJSDate(currentDateInJS));
8399
}
84100

101+
getTime(JSDate) {
102+
return `${formatDigits(JSDate.getHours(), 2)}:${formatDigits(
103+
JSDate.getMinutes(),
104+
2
105+
)}:${formatDigits(JSDate.getSeconds(), 2)}.${formatDigits(JSDate.getMilliseconds(), 3)}`;
106+
}
107+
108+
updateTime(value) {
109+
const date = this.props.value.split(' ')[0];
110+
this.props.onChange(date + ' ' + value);
111+
}
112+
85113
render() {
86114
const JSDate = new Date(this.props.value);
87-
const currentMonth = JSDate.getMonth();
88115
const currentYear = JSDate.getFullYear();
89-
const monthOptions = this.getMonthOptions();
90-
const yearOptions = this.getYearOptions(currentYear);
91116

92117
return (
93118
<div className="datetimepicker-container">
@@ -124,14 +149,14 @@ export default class DateTimePicker extends Component {
124149
<div className="datetimepicker-datepicker-container">
125150
<div className="datetimepicker-datepicker-navbar">
126151
<Dropdown
127-
options={monthOptions}
128-
value={currentMonth}
152+
options={this.getMonthOptions()}
153+
value={JSDate.getMonth()}
129154
className="datimepicker-monthpicker"
130155
clearable={false}
131156
onChange={this.onMonthChange}
132157
/>
133158
<Dropdown
134-
options={yearOptions}
159+
options={this.getYearOptions(currentYear)}
135160
value={currentYear}
136161
className="datimepicker-yearpicker"
137162
clearable={false}
@@ -151,10 +176,9 @@ export default class DateTimePicker extends Component {
151176
{this.state.timepickerOpen ? (
152177
<div>
153178
<TextInput
154-
value={JSDate}
155-
onUpdate={value => {
156-
this.props.onChange(this.toPlotlyJSDate(value));
157-
}}
179+
value={this.getTime(JSDate)}
180+
onUpdate={this.updateTime}
181+
placeHolder="hh:mm:ss.xxx"
158182
/>
159183
<span className="datetimepicker-date-units">
160184
{new Date(this.props.value).toLocaleTimeString('en-US').split(' ')[1] === 'PM'

src/styles/components/widgets/_datetimepicker.scss

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ $width: 220px;
2222
width: 200px;
2323
height: 30px;
2424
margin: 0 auto;
25-
padding-top: 2px;
2625
}
2726

2827
.datetimepicker-container-input {
@@ -72,7 +71,11 @@ $width: 220px;
7271

7372
.DayPicker-wrapper {
7473
width: $width;
75-
padding-bottom: 8px;
74+
padding-bottom: 5px;
75+
}
76+
77+
.DayPicker-Day {
78+
outline: none !important;
7679
}
7780

7881
.DayPicker-Day--highlighted {
@@ -81,12 +84,12 @@ $width: 220px;
8184
outline: none;
8285
}
8386

84-
.DayPicker-Weekday {
85-
color: #577290 !important;
87+
.DayPicker-Day--highlighted:hover {
88+
background-color: var(--color-accent) !important;
8689
}
8790

88-
.DayPicker-Day--highlighted:hover {
89-
color: var(--color-text-base);
91+
.DayPicker-Weekday {
92+
color: #577290 !important;
9093
}
9194

9295
.datimepicker-yearpicker,

0 commit comments

Comments
 (0)