Skip to content

Commit

Permalink
refactor(list-wrapper): use innerRef instance instead of outerRef
Browse files Browse the repository at this point in the history
  • Loading branch information
ozsirmajotform committed Dec 25, 2023
1 parent b6ae462 commit e0985b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 18 additions & 16 deletions src/components/Panels/SlidesPanel/ListWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { usePageVisibility } from '../../../utils/hooks';

const ListWrapper = ({
component: Component,
onPageAdd,
onSortEnd,
pageCount,
reportSettings,
Expand Down Expand Up @@ -37,11 +38,19 @@ const ListWrapper = ({
width: width,
}), [reportSettings]);

const _onSortEnd = (sortEvent, nativeEvent) => {
const _onSortEnd = useCallback((sortEvent, nativeEvent) => {
if (onSortEnd) {
onSortEnd(sortEvent, nativeEvent, outerRef.current);
}
};
}, [onSortEnd]);

useEffect(() => {
if (selectedPageIndex !== -1) {
const instance = outerRef.current.getWrappedInstance();
const list = instance.sortablePageListRef.current;
list.scrollToItem(selectedPageIndex);
}
}, [selectedPageIndex]);

useEffect(() => { // for page thumbnails actions
if (!scrollTracking) {
Expand All @@ -52,16 +61,7 @@ const ListWrapper = ({

usePageVisibility(index => {
if (scrollTracking && index !== selectedPageIndex && !Number.isNaN(index)) {
const instance = outerRef.current.getWrappedInstance();
const list = instance.sortableOuterRef.current;
setSelectedPageIndex(index);
if (selectedPageIndex === -1 && index !== 1) {
setTimeout(() => {
list.scrollTo({ behavior: 'smooth', top: ((index - 1) * 120) + 7 });
}, 300);
} else {
list.scrollTo({ behavior: 'smooth', top: ((index - 1) * 120) + 7 });
}
} else if (!scrollTracking && index === selectedPageIndex) {
setScrollTracking(true);
}
Expand All @@ -74,13 +74,13 @@ const ListWrapper = ({
if (!e.target.classList.contains('controllerItem')) { // for page thumbnails actions
scrollToTarget(`pageActions-id-${order}`);
}
}, [selectedPageIndex]);
}, []);

const onPageAdd = index => {
const handlePageAdd = useCallback(index => {
setScrollTracking(false);
setSelectedPageIndex(index);
otherProps.onPageAdd(index);
};
onPageAdd(index);
}, [onPageAdd]);

return (
<>
Expand All @@ -107,7 +107,7 @@ const ListWrapper = ({
<div className="jfReport-pane-footer">
<PageAdder
additionalClass="forOptions"
onPageAdd={onPageAdd}
onPageAdd={handlePageAdd}
pageCount={pageCount}
/>
</div>
Expand All @@ -117,6 +117,7 @@ const ListWrapper = ({

ListWrapper.propTypes = {
component: PropTypes.any,
onPageAdd: PropTypes.func,
onSortEnd: PropTypes.func,
pageCount: PropTypes.number,
reportSettings: PropTypes.shape({
Expand All @@ -128,6 +129,7 @@ ListWrapper.propTypes = {

ListWrapper.defaultProps = {
component: null,
onPageAdd: () => {},
onSortEnd: () => {},
pageCount: 0,
reportSettings: {},
Expand Down
2 changes: 0 additions & 2 deletions src/components/Panels/SlidesPanel/SortablePageList.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const SortablePageList = Component => {
super(props);

this.sortablePageListRef = createRef();
this.sortableOuterRef = createRef();
}

get itemData() {
Expand Down Expand Up @@ -86,7 +85,6 @@ const SortablePageList = Component => {
itemCount={pageCount}
itemData={this.itemData}
itemSize={127}
outerRef={this.sortableOuterRef}
width={width}
>
{SortablePageItemRenderer}
Expand Down

0 comments on commit e0985b8

Please sign in to comment.