Skip to content

Commit

Permalink
fix: remove requirement to use Allotment.Pane if using refs with chil…
Browse files Browse the repository at this point in the history
…dren
  • Loading branch information
johnwalley committed Sep 24, 2021
1 parent 266a565 commit 93a30bb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 37 deletions.
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,3 @@ export const App = () => (
</Allotment>
);
```

### Passing a `ref` to a child

If you need to attach a ref to a child component then you must wrap it in a `Allotment.Pane` component.

```jsx
import React from "react";
import { Allotment } from "allotment";

export const App = () => {
const ref = React.useRef(null);

return (
<Allotment>
<Allotment.Pane>
<ComponentA ref={ref}>
</Allotment.Pane>
<ComponentB>
</Allotment>
)
};
```

Under the hood the library needs to attach a ref to the immediate children. This overwrites any existing refs which will not be populated/called. This limitation may go away in a future release.
19 changes: 6 additions & 13 deletions src/allotment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const Allotment = ({
const splitViewContainerRef = useRef<HTMLDivElement>(null!);
const splitViewRef = useRef<SplitView | null>(null);
const splitViewViewRef = useRef<Record<string, HTMLElement>>({});
const viewRef = useRef<Record<string, HTMLElement>>({});

const childrenArray = useMemo(
() => React.Children.toArray(children).filter(React.isValidElement),
Expand Down Expand Up @@ -129,27 +128,21 @@ const Allotment = ({
return null;
}

const key = child.key ?? index;

return (
<div
key={child.key ?? index}
key={key}
ref={(el: HTMLElement | null) => {
if (el) {
splitViewViewRef.current[child.key ?? index] = el;
splitViewViewRef.current[key] = el;
} else {
delete splitViewViewRef.current[child.key ?? index];
delete splitViewViewRef.current[key];
}
}}
className={classNames(styles.splitViewView)}
>
{React.cloneElement(child, {
ref: (el: HTMLElement | null) => {
if (el) {
viewRef.current[child.key ?? index] = el;
} else {
delete viewRef.current[child.key ?? index];
}
},
})}
{child}
</div>
);
})}
Expand Down

0 comments on commit 93a30bb

Please sign in to comment.