Skip to content

Commit 90802d2

Browse files
Merge pull request #3844 from SashaRepin2/master
Fix(year dropdown options): Year dropdown scroll defaults to center of list
2 parents 3ed38ac + 11deb7a commit 90802d2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/year_dropdown_options.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,20 @@ export default class YearDropdownOptions extends React.Component {
5555

5656
componentDidMount() {
5757
const dropdownCurrent = this.dropdownRef.current;
58+
5859
if (dropdownCurrent) {
59-
dropdownCurrent.scrollTop =
60-
dropdownCurrent.scrollHeight / 2 - dropdownCurrent.clientHeight / 2;
60+
// Get array from HTMLCollection
61+
const dropdownCurrentChildren = dropdownCurrent.children
62+
? Array.from(dropdownCurrent.children)
63+
: null;
64+
const selectedYearOptionEl = dropdownCurrentChildren
65+
? dropdownCurrentChildren.find((childEl) => childEl.ariaSelected)
66+
: null;
67+
68+
dropdownCurrent.scrollTop = selectedYearOptionEl
69+
? selectedYearOptionEl.offsetTop +
70+
(selectedYearOptionEl.clientHeight - dropdownCurrent.clientHeight) / 2
71+
: (dropdownCurrent.scrollHeight - dropdownCurrent.clientHeight) / 2;
6172
}
6273
}
6374

0 commit comments

Comments
 (0)