Skip to content

Commit

Permalink
fix(react): fix passing ref to wrapper not working properly (fix #409)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grsmto committed Nov 14, 2019
1 parent 186bffa commit 9f3a15c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/simplebar-react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const getOptions = function(obj) {
const SimpleBar = React.forwardRef(
({ children, scrollableNodeProps = {}, ...otherProps }, ref) => {
let instance;
const elRef = useRef({});
const scrollableNodeRef = useRef();
let scrollableNodeRef = useRef();
const elRef = useRef();
const contentNodeRef = useRef();
let options = {};
let rest = {};
Expand Down Expand Up @@ -66,7 +66,7 @@ const SimpleBar = React.forwardRef(
}

useEffect(() => {
const scrollableNodeRef = scrollableNodeProps.ref || scrollableNodeRef;
scrollableNodeRef = scrollableNodeProps.ref || scrollableNodeRef;

if (elRef.current) {
instance = new SimpleBarJS(elRef.current, {
Expand All @@ -89,7 +89,7 @@ const SimpleBar = React.forwardRef(
instance.unMount();
instance = null;
};
});
}, []);

return (
<div ref={elRef} data-simplebar {...rest}>
Expand Down
26 changes: 24 additions & 2 deletions packages/simplebar/demo/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const renderScrollbar = props => {

const Demo = () => {
const [isHidden, setHidden] = React.useState(true);
const scrollableElRef = React.createRef();
// const scrollableElRef = React.createRef();

const handleShowClick = React.useCallback(() => {
setHidden(false);
Expand Down Expand Up @@ -282,7 +282,7 @@ const Demo = () => {
className="demo1"
autoHide={false}
data-simplebar-force-visible="x"
scrollableNodeProps={{ ref: scrollableElRef }}
// scrollableNodeProps={{ ref: scrollableElRef }}
>
{({ scrollableNodeRef, contentNodeRef }) => (
<List
Expand Down Expand Up @@ -326,6 +326,28 @@ const Demo = () => {
</div>
</div>
</section>
<section>
<div className="col">
<h2>SimpleBar-React + refs</h2>
<SimpleBarReact
className="demo1"
autoHide={false}
data-simplebar-force-visible="x"
>
{({ scrollableNodeRef, contentNodeRef }) => {
return (
<div ref={scrollableNodeRef}>
<div ref={contentNodeRef}>
{[...Array(50)].map((x, i) => (
<p key={i}>Some content</p>
))}
</div>
</div>
);
}}
</SimpleBarReact>
</div>
</section>
</section>
);
};
Expand Down

0 comments on commit 9f3a15c

Please sign in to comment.