-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autoscroll Course Planner to current year card #3640
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { PureComponent } from 'react'; | ||
import { PureComponent, Ref, createRef } from 'react'; | ||
import classnames from 'classnames'; | ||
import { flatMap, size, sortBy, toPairs, values } from 'lodash'; | ||
|
||
|
@@ -24,14 +24,31 @@ | |
|
||
type State = { | ||
readonly showSpecialSem: boolean; | ||
readonly currentYearCardRef: Ref<HTMLDivElement>; | ||
}; | ||
|
||
export default class PlannerYear extends PureComponent<Props, State> { | ||
override state = { | ||
// Always display Special Terms I and II if either one has modules | ||
showSpecialSem: this.hasSpecialTermModules(), | ||
currentYearCardRef: createRef<HTMLDivElement>(), | ||
}; | ||
|
||
override componentDidMount() { | ||
if (this.props.year !== config.academicYear) { | ||
return; | ||
} | ||
const currentYearCard = this.state.currentYearCardRef.current; | ||
const parentContainer = currentYearCard?.parentElement; | ||
if (!currentYearCard || !parentContainer) { | ||
return; | ||
} | ||
parentContainer.scrollTo({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could consider using |
||
left: currentYearCard.offsetLeft - parentContainer.offsetLeft, | ||
behavior: 'smooth', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For scrolling on component mount you probably don't want smooth, it's better for the component to appear already scrolled to the correct location. |
||
}); | ||
} | ||
|
||
hasSpecialTermModules() { | ||
const { semesters } = this.props; | ||
return size(semesters[3]) > 0 || size(semesters[4]) > 0; | ||
|
@@ -72,6 +89,7 @@ | |
|
||
return ( | ||
<section | ||
ref={year === config.academicYear ? this.state.currentYearCardRef : undefined} | ||
key={year} | ||
className={classnames(styles.year, { | ||
[styles.currentYear]: year === config.academicYear, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref should not be stored in state, see https://react.dev/reference/react/createRef