Skip to content

Commit

Permalink
Merge pull request #3844 from SashaRepin2/master
Browse files Browse the repository at this point in the history
Fix(year dropdown options): Year dropdown scroll defaults to center of list
  • Loading branch information
martijnrusschen committed Jan 30, 2023
2 parents 3ed38ac + 11deb7a commit 90802d2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/year_dropdown_options.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,20 @@ 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 dropdownCurrentChildren = dropdownCurrent.children
? Array.from(dropdownCurrent.children)
: null;
const selectedYearOptionEl = dropdownCurrentChildren
? dropdownCurrentChildren.find((childEl) => childEl.ariaSelected)
: null;

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

Expand Down

0 comments on commit 90802d2

Please sign in to comment.