Skip to content

Commit

Permalink
Add optional "data" to step
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed Apr 9, 2024
1 parent 59d4a9b commit 7bff2a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/types/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export type Step = Simplify<
* The tooltip's body.
*/
content: ReactNode;
/**
* Additional data you can set to the step.
*/
data?: any;
/**
* Don't show the Beacon before the tooltip.
* @default false
Expand Down
19 changes: 14 additions & 5 deletions test/__fixtures__/CustomOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { useReducer } from 'react';

import { standardSteps } from './steps';

import Joyride, { STATUS, Status } from '../../src';
import Joyride, { LIFECYCLE, STATUS, Status } from '../../src';
import { CallBackProps, Props, Step } from '../../src/types';

interface CustomOptionsProps extends Omit<Props, 'run' | 'steps'> {}
interface CustomOptionsProps extends Omit<Props, 'run' | 'steps'> {
finishedCallback: () => void;
}

interface State {
index: number;
Expand All @@ -28,11 +30,14 @@ const tourSteps = [
target: '.outro h2 span',
placement: 'top' as const,
content: "Text only steps — Because sometimes you don't really need a proper heading",
data: {
last: true,
},
},
];

export default function CutomOptionss(props: Omit<CustomOptionsProps, 'run' | 'steps'>) {
const { callback, ...rest } = props;
export default function CustomOptions(props: Omit<CustomOptionsProps, 'run' | 'steps'>) {
const { callback, finishedCallback, ...rest } = props;
const [{ run, steps }, setState] = useReducer(
(previousState: State, nextState: Partial<State>) => ({
...previousState,
Expand All @@ -50,7 +55,7 @@ export default function CutomOptionss(props: Omit<CustomOptionsProps, 'run' | 's
};

const handleJoyrideCallback = (data: CallBackProps) => {
const { status } = data;
const { lifecycle, status, step } = data;

if (([STATUS.FINISHED, STATUS.SKIPPED] as Array<Status>).includes(status)) {
setState({ run: false });
Expand All @@ -59,6 +64,10 @@ export default function CutomOptionss(props: Omit<CustomOptionsProps, 'run' | 's
setState({ index: data.index });

callback?.(data);

if (lifecycle === LIFECYCLE.COMPLETE && step.data?.last) {
finishedCallback();
}
};

return (
Expand Down
3 changes: 3 additions & 0 deletions test/tours/custom-options.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import { cleanup, fireEvent, render, screen, waitFor } from '../__fixtures__/tes
const getCallbackResponse = callbackResponseFactory({ size: 4 });

const mockCallback = vi.fn();
const mockFinishedCallback = vi.fn();
const mockGetPopper = vi.fn();

describe('Joyride > Custom Options', () => {
render(
<Customized
callback={mockCallback}
finishedCallback={mockFinishedCallback}
floaterProps={{
getPopper: (popper, type) => {
mockGetPopper(popper, type);
Expand Down Expand Up @@ -147,5 +149,6 @@ describe('Joyride > Custom Options', () => {
);

expect(mockGetPopper).toHaveBeenCalledTimes(8);
expect(mockFinishedCallback).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 7bff2a8

Please sign in to comment.