Skip to content

Don't break when a child is null #283

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

Merged
merged 5 commits into from
Jul 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/SplitPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ function toPx(value, unit = 'px', size) {
}
}

function removeNullChildren(children) {
return React.Children.toArray(children).filter(c => c);
}

export function getUnit(size) {
if(size.endsWith('px')) {
return 'px';
Expand Down Expand Up @@ -187,7 +191,7 @@ class SplitPane extends Component {
const minSizes = this.getPanePropMinMaxSize(props, 'minSize');
const maxSizes = this.getPanePropMinMaxSize(props, 'maxSize');

const resizersSize = this.getResizersSize();
const resizersSize = this.getResizersSize(removeNullChildren(this.props.children));
const splitPaneSizePx = split === 'vertical'
? splitPaneDimensions.width - resizersSize
: splitPaneDimensions.height - resizersSize;
Expand All @@ -207,7 +211,7 @@ class SplitPane extends Component {
}

getPanePropSize(props) {
return React.Children.map(props.children, child => {
return removeNullChildren(props.children).map(child => {
const value = child.props['size'] || child.props['initialSize'];
if (value === undefined) {
return DEFAULT_PANE_SIZE;
Expand All @@ -218,7 +222,7 @@ class SplitPane extends Component {
}

getPanePropMinMaxSize(props, key) {
return React.Children.map(props.children, child => {
return removeNullChildren(props.children).map(child => {
const value = child.props[key];
if (value === undefined) {
return key === 'maxSize' ? DEFAULT_PANE_MAX_SIZE : DEFAULT_PANE_MIN_SIZE;
Expand Down Expand Up @@ -338,16 +342,17 @@ class SplitPane extends Component {
this.paneElements[idx] = el;
}

getResizersSize() {
return (React.Children.count(this.props.children) - 1) * this.props.resizerSize;
getResizersSize(children) {
return (children.length - 1) * this.props.resizerSize;
}

render() {
const { children, className, split } = this.props;
const notNullChildren = removeNullChildren(this.props.children)
const sizes = this.getSizes();
const resizersSize = this.getResizersSize();
const resizersSize = this.getResizersSize(notNullChildren);

const elements = children.reduce((acc, child, idx) => {
const elements = notNullChildren.reduce((acc, child, idx) => {
let pane;
const resizerIndex = idx - 1;
const isPane = child.type === Pane;
Expand Down