Skip to content

Commit 03fdeee

Browse files
committed
Calculate scroll of TimeSelect
Changed hardcoded 30px-per-item scroll in order to have selectedDate/currH scrolled down in the middle of the TimeSelect. Now it saves a reference of list-item of selectedDate (with fallback to currH when selectedDate not provided) and calculates list's default scroll based on list's height and the referenced item's offset.
1 parent e052b4a commit 03fdeee

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/time.jsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ export default class Time extends React.Component {
4040

4141
componentDidMount() {
4242
// code to ensure selected time will always be in focus within time window when it first appears
43-
const multiplier = 60 / this.props.intervals;
44-
const currH = this.props.selected
45-
? getHour(this.props.selected)
46-
: getHour(newDate());
47-
this.list.scrollTop = 30 * (multiplier * currH);
43+
this.list.scrollTop =
44+
this.centerLi.offsetTop -
45+
this.list.clientHeight / 2 +
46+
this.centerLi.clientHeight;
4847
}
4948

5049
handleClick = time => {
@@ -123,6 +122,14 @@ export default class Time extends React.Component {
123122
key={i}
124123
onClick={this.handleClick.bind(this, time)}
125124
className={this.liClasses(time, currH, currM)}
125+
ref={li => {
126+
if (
127+
(currH === getHour(time) && currM === getMinute(time)) ||
128+
(currH === getHour(time) && !this.centerLi)
129+
) {
130+
this.centerLi = li;
131+
}
132+
}}
126133
>
127134
{formatDate(time, format)}
128135
</li>

0 commit comments

Comments
 (0)