Added activeIndex prop and onActive event to Carousel #1239
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.