Skip to content
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

Feature: children can also be a function returning the actual children #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ import Sticky from 'react-sticky-state';

```

Sticky either takes its only child and adds the behavior and classes to it or wrappes all children inside an element if there are more than one. the tagname can be defined by props.
Sticky as `children` takes either:

### possible props
- its' only child and adds the behavior and classes to it, or
- wraps all children inside an element if there are more than one. (The `tagName` can be defined by props.)
- a function, which will receive `{ absolute: bool, sticky: bool, disabled: bool }` object as a first argument and should return a child or children. (Example usage: `<Sticky>{({ sticky }) => <Menu isCollapsed={sticky} />}</Sticky>`.)

### possible props

```javascript
static propTypes = {
Expand All @@ -88,7 +92,8 @@ static propTypes = {
none : PropTypes.string,
persist : PropTypes.bool,
active : PropTypes.bool
})
}),
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func])
};

static defaultProps = {
Expand Down
4 changes: 2 additions & 2 deletions dist/react-sticky-state.min.js

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions lib/react-sticky-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,17 @@ var ReactStickyState = function (_Component) {
ReactStickyState.prototype.render = function render() {
var _classNames, _classNames2, _classNames3, _classNames4;

var children = typeof this.props.children === 'function' ? this.props.children({
absolute: this.state.absolute,
disabled: this.state.disabled,
sticky: this.state.sticky
}) : this.props.children;

if (!this.state.initialized) {
return this.props.children;
return children;
}

var element = _react2.default.Children.only(this.props.children);
var element = _react2.default.Children.only(children);

var _props = this.props,
wrapperClass = _props.wrapperClass,
Expand Down Expand Up @@ -616,7 +622,7 @@ var ReactStickyState = function (_Component) {
element = _react2.default.createElement(
Comp,
(0, _extends3.default)({ ref: refName, style: style, className: className }, props),
this.props.children
children
);
}

Expand Down Expand Up @@ -664,7 +670,8 @@ ReactStickyState.propTypes = {
none: _propTypes2.default.string,
persist: _propTypes2.default.bool,
active: _propTypes2.default.bool
})
}),
children: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.func])
};
ReactStickyState.defaultProps = {
initialize: true,
Expand Down
17 changes: 13 additions & 4 deletions src/react-sticky-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class ReactStickyState extends Component {
none : PropTypes.string,
persist : PropTypes.bool,
active : PropTypes.bool
})
}),
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func])
};

static defaultProps = {
Expand Down Expand Up @@ -584,12 +585,20 @@ class ReactStickyState extends Component {
}

render() {
const children =
typeof this.props.children === 'function'
? this.props.children({
absolute: this.state.absolute,
disabled: this.state.disabled,
sticky: this.state.sticky,
})
: this.props.children;

if(!this.state.initialized){
return this.props.children;
return children;
}

let element = React.Children.only(this.props.children);
let element = React.Children.only(children);

const { wrapperClass, stickyClass, fixedClass, stateClass, disabledClass, absoluteClass, disabled, debug, tagName, ...props } = this.props;

Expand Down Expand Up @@ -621,7 +630,7 @@ class ReactStickyState extends Component {
element = React.cloneElement(element, { ref: refName, style: style, className: classNames(element.props.className, className) });
} else {
const Comp = this.props.tagName;
element = <Comp ref={ refName } style={ style } className={ className } {...props }>{this.props.children}</Comp>;
element = <Comp ref={ refName } style={ style } className={ className } {...props }>{children}</Comp>;
}

if (Can.sticky) {
Expand Down