@ui5/webcomponents-react
Version:
React Wrapper for UI5 Web Components and additional components
87 lines • 2.54 kB
JavaScript
import { useEffect, useRef } from 'react';
export const useTableScrollHandles = (ref, dispatch) => {
let analyticalTableRef = useRef(null);
if (ref) {
analyticalTableRef = ref;
}
const scrollToRef = useRef({});
useEffect(() => {
if (analyticalTableRef.current) {
Object.assign(analyticalTableRef.current, {
scrollTo: (offset, align) => {
if (typeof scrollToRef.current?.scrollToOffset === 'function') {
scrollToRef.current.scrollToOffset(offset, {
align
});
} else {
dispatch({
type: 'TRIGGER_PROG_SCROLL',
payload: {
direction: 'vertical',
type: 'offset',
args: [offset, {
align
}]
}
});
}
},
scrollToItem: (index, align) => {
if (typeof scrollToRef.current?.scrollToIndex === 'function') {
scrollToRef.current.scrollToIndex(index, {
align
});
} else {
dispatch({
type: 'TRIGGER_PROG_SCROLL',
payload: {
direction: 'vertical',
type: 'item',
args: [index, {
align
}]
}
});
}
},
horizontalScrollTo: (offset, align) => {
if (typeof scrollToRef.current?.horizontalScrollToOffset === 'function') {
scrollToRef.current.horizontalScrollToOffset(offset, {
align
});
} else {
dispatch({
type: 'TRIGGER_PROG_SCROLL',
payload: {
direction: 'horizontal',
type: 'offset',
args: [offset, {
align
}]
}
});
}
},
horizontalScrollToItem: (index, align) => {
if (typeof scrollToRef.current?.horizontalScrollToIndex === 'function') {
scrollToRef.current.horizontalScrollToIndex(index, {
align
});
} else {
dispatch({
type: 'TRIGGER_PROG_SCROLL',
payload: {
direction: 'horizontal',
type: 'item',
args: [index, {
align
}]
}
});
}
}
});
}
}, []);
return [analyticalTableRef, scrollToRef];
};