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

Added activeIndex prop and onActive event to Carousel #1239

Merged
merged 1 commit into from
Mar 15, 2017

Conversation

MarcoMedrano
Copy link
Contributor

This fixes #1187
Using same names than in Tabs

What does this PR do?

Adds activeIndex so the dev can set by code which will be the active child in the Carousel.
Adds onActive so the user can be notified when user swaps or chooses another child in Carousel or when this event is fired automatically by Carousel using autoplay={true}.

Where should the reviewer start?

State has been modified on constructor to get the value from props if there is one:
activeIndex: props.activeIndex || 0,

What testing has been done on this PR?

Just manual test with both Carousel and Tabs.
<Carousel autoplay={true} persistentNav={false} activeIndex={this.state.activeIndex} onActive={index => {console.log(index); this.setState({activeIndex:index})}} >

<Tabs activeIndex={this.state.activeIndex} activeIndex={this.state.activeIndex} onActive={index => {console.log(index); this.setState({activeIndex:index})}} >

How should this be manually tested?

I tested it together a tab to have same behaviors using all combinations of props with activeIndex only, with onActive only with both together and without both.

Do the grommet docs need to be updated?

Yes should be added this doc (copied from https://grommet.github.io/docs/tabs):

activeIndex {number}
Active tab index. Defaults to 0.

onActive {function}
Function that will be called with the active tab index when the currently active tab changes.

Is this change backwards compatible or is it a breaking change?

Yes, it is. If both activeIndex and onActive are excluded, it works as always worked.

  • If only activeIndex is included then Carousel shows only that index no matter if the user clicks on Carousel button's or swaps. Even if Carousel has autoplay={true} index is changed only by the property (I determined this as the expected behavior after think a bit :D and check Tabs behavior).

  • If only onActive is included then Carousel works as before but notifies the index even if Carousel has autoplay={true}.

  • If both are included so last two statements are true.

This is working as Tabs does.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.03%) to 26.47% when pulling 07a212f on MarcoMedrano:Fix-#1187 into 4a9fe6f on grommet:master.


if (this.props.onActive) {
this.props.onActive(index);
announceFunc();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to call announceFunc here as we already call when setState is invoked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:o Right , mm and not. The announceFunc is only called if this is true ! this.props.hasOwnProperty('activeIndex') If that is false the dev will set the state through componentWillReceiveProps, but they cant explicitly call the announceFunc that is why I called the announceFunc once the dev has updated the props by itself (activeIndex={..}), fired by this.props.onActive(index);
However my code can lead to call twice announceFunc, I think the fix will be:

if (this.props.onActive) {
this.props.onActive(index);
if (this.props.hasOwnProperty('activeIndex')) announceFunc(); }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added announce every time we set activeIndex

@alansouzati
Copy link
Collaborator

thanks for your contribution. I will fix the extra call in my local system.

@alansouzati alansouzati merged commit f1f9040 into grommet:master Mar 15, 2017
nathanstitt pushed a commit to nathanstitt/grommet that referenced this pull request May 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Carousel does not interact neither has events
4 participants