Skip to content

Commit

Permalink
feat(components): add tracking to Carousel component (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucent1090 authored Jul 16, 2020
1 parent b5fc361 commit 46e8215
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
23 changes: 15 additions & 8 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,29 @@ const SORT_ORDER = {
'Getting Started',
'Design Principles',
'Contributing',
'Code of Conduct'
'Code of Conduct',
],
Advanced: [
'Static CSS',
'Base Components',
'Theme',
'Grid',
'Icons',
'Event Tracking',
],
Advanced: ['Static CSS', 'Base Components', 'Theme', 'Grid', 'Icons'],
Typography: ['Heading', 'SubHeading', 'Text'],
Layout: [],
Forms: [],
Components: [],
Icons: []
Icons: [],
};

addParameters({
options: {
storySort: sortStories(SORT_ORDER),
showRoots: true
showRoots: true,
},
docs: { components }
docs: { components },
});

const Story = styled.div`
Expand All @@ -50,11 +57,11 @@ const Story = styled.div`
}
`;

const withStoryStyles = storyFn => {
const withStoryStyles = (storyFn) => {
return <Story>{storyFn()}</Story>;
};

const withThemeProvider = storyFn => (
const withThemeProvider = (storyFn) => (
<ThemeProvider theme={light}>
<BaseStyles />
{storyFn()}
Expand All @@ -71,7 +78,7 @@ if (!__TEST__) {

addDecorator(withThemeProvider);

const withTrackingAction = storyFn => (
const withTrackingAction = (storyFn) => (
<TrackingRoot name="tracking-root" onDispatch={action('Tracking event')}>
<TrackingView name="tracking-view">{storyFn()}</TrackingView>
</TrackingRoot>
Expand Down
10 changes: 10 additions & 0 deletions src/components/Carousel/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const Carousel = ({
hideControls = false,
getAriaLabelledBy = () => {},
children,
tracking,
...props
}) => {
const slidesTotal = slides.length;
Expand All @@ -101,6 +102,7 @@ const Carousel = ({
animationDuration={animationDuration}
onNext={handleNextSlide}
onPrevious={handlePreviousSlide}
tracking={tracking}
>
{({
state,
Expand Down Expand Up @@ -221,6 +223,14 @@ Carousel.propTypes = {
* Add additional components inside a carousel.
*/
children: PropTypes.oneOfType([childrenPropType, childrenRenderPropType]),
/**
* Additional data that is dispatched with the tracking event.
*/
tracking: PropTypes.shape({
label: PropTypes.string.isRequired,
component: PropTypes.string,
customParameters: PropTypes.object,
}),
};

export default Carousel;
10 changes: 10 additions & 0 deletions src/components/Step/Step.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function Step({
stepInterval = 1,
animationDuration = 0,
stepDuration = 0,
tracking = {},
onPlay = () => {},
onPause = () => {},
onNext = () => {},
Expand All @@ -40,6 +41,7 @@ function Step({
stepInterval,
animationDuration,
stepDuration,
tracking,
onPlay,
onPause,
onNext,
Expand Down Expand Up @@ -110,6 +112,14 @@ Step.propTypes = {
* Function called with an object containing current state and prop getters.
*/
children: PropTypes.func,
/**
* Additional data that is dispatched with the tracking event.
*/
tracking: PropTypes.shape({
label: PropTypes.string.isRequired,
component: PropTypes.string,
customParameters: PropTypes.object,
}),
};

export default Step;
46 changes: 32 additions & 14 deletions src/components/Step/hooks/use-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useReducer, useEffect, useRef } from 'react';
import { isFunction } from 'lodash/fp';

import * as StepService from '../StepService';
import useClickHandler from '../../../hooks/use-click-handler';

export default function useStep(props = {}) {
if (__DEV__ && props.cycle && !props.totalSteps) {
Expand All @@ -40,6 +41,27 @@ export default function useStep(props = {}) {
const [state, dispatch] = useReducer(StepService.reducer, initialState);
const playingInterval = useRef(null);
const animationEndCallback = useRef(null);
const { onNext, onPrevious, onPause, onPlay, tracking } = props;
const handleNext = useClickHandler(
onNext,
{ label: 'next', ...tracking },
'carousel',
);
const handlePrevious = useClickHandler(
onPrevious,
{ label: 'previous', ...tracking },
'carousel',
);
const handlePause = useClickHandler(
onPause,
{ label: 'pause', ...tracking },
'carousel',
);
const handlePlay = useClickHandler(
onPlay,
{ label: 'play', ...tracking },
'carousel',
);

useEffect(() => {
const playable = shouldPlay();
Expand All @@ -56,7 +78,7 @@ export default function useStep(props = {}) {

// ACTIONS
function next() {
const { totalSteps, cycle, stepInterval, onNext } = props;
const { totalSteps, cycle, stepInterval } = props;
const newStep = StepService.calculateNextStep({
step: state.step,
stepInterval,
Expand All @@ -65,14 +87,14 @@ export default function useStep(props = {}) {
});

updateSlide(newStep, () => {
if (isFunction(onNext)) {
onNext(getStateAndHelpers());
if (isFunction(handleNext)) {
handleNext(getStateAndHelpers());
}
});
}

function previous() {
const { totalSteps, cycle, stepInterval, onPrevious } = props;
const { totalSteps, cycle, stepInterval } = props;
const newStep = StepService.calculatePreviousStep({
step: state.step,
stepInterval,
Expand All @@ -81,29 +103,25 @@ export default function useStep(props = {}) {
});

updateSlide(newStep, () => {
if (isFunction(onPrevious)) {
onPrevious(getStateAndHelpers());
if (isFunction(handlePrevious)) {
handlePrevious(getStateAndHelpers());
}
});
}

function pause() {
const { onPause } = props;

updatePause(true);

if (isFunction(onPause)) {
onPause(getStateAndHelpers());
if (isFunction(handlePause)) {
handlePause(getStateAndHelpers());
}
}

function play() {
const { onPlay } = props;

updatePause(false);

if (isFunction(onPlay)) {
onPlay(getStateAndHelpers());
if (isFunction(handlePlay)) {
handlePlay(getStateAndHelpers());
}
}

Expand Down

0 comments on commit 46e8215

Please sign in to comment.