Skip to content

Commit

Permalink
Fix(year dropdown options): Year dropdown scroll defaults to center o…
Browse files Browse the repository at this point in the history
…f list
  • Loading branch information
SashaRepin2 committed Dec 3, 2022
1 parent 0edbe7c commit d7c2ce8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/year_dropdown_options.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,18 @@ export default class YearDropdownOptions extends React.Component {

componentDidMount() {
const dropdownCurrent = this.dropdownRef.current;

if (dropdownCurrent) {
dropdownCurrent.scrollTop =
dropdownCurrent.scrollHeight / 2 - dropdownCurrent.clientHeight / 2;
// Get array from HTMLCollection
const dropdownCurrentChildrenArr = Array.from(dropdownCurrent.children);
const selectedYearOptionEl = dropdownCurrentChildrenArr.find(
(childEl) => childEl.ariaSelected
);

dropdownCurrent.scrollTop = selectedYearOptionEl
? selectedYearOptionEl.offsetTop +
(selectedYearOptionEl.clientHeight - dropdownCurrent.clientHeight) / 2
: (dropdownCurrent.scrollHeight - dropdownCurrent.clientHeight) / 2;
}
}

Expand Down

0 comments on commit d7c2ce8

Please sign in to comment.