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

feat(core): Allow multiple indexes in a single AttachStep #143

Open
wants to merge 1 commit into
base: main
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
19 changes: 16 additions & 3 deletions package/src/lib/components/attach-step/AttachStep.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export interface AttachStepProps<T> {
fill?: boolean;
/**
* The index of the `steps` array to which the step is attached to.
* It can be a single index or multiple ones.
*/
index: number;
index: number | Array<number>;
}

/**
Expand All @@ -57,12 +58,12 @@ export function AttachStep<T>({ children, fill = false, index }: AttachStepProps
const childRef = useRef<View>(null);

useEffect(() => {
if (current === index) {
if (checkIndex(index, current)) {
childRef.current?.measureInWindow((x, y, width, height) => {
changeSpot({ height, width, x, y });
});
}
}, [changeSpot, current, index]);
}, [changeSpot, current, index.toString()]);
Copy link
Author

Choose a reason for hiding this comment

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

This is needed to compare arrays, otherwise, they will always differ and useEffect will be called infinite times.


if (typeof children.type === "function") {
const { style, ...rest } = children.props;
Expand Down Expand Up @@ -91,3 +92,15 @@ export function AttachStep<T>({ children, fill = false, index }: AttachStepProps
children.props?.children,
);
}

function checkIndex<T>(index: AttachStepProps<T>["index"], current: number | undefined): boolean {
if (current === undefined) {
return false;
}

if (Array.isArray(index)) {
return index.includes(current);
}

return current === index;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function TestScreen(): React.ReactElement {
<Text>{"Test Tour 1"}</Text>
</AttachStep>

<AttachStep index={1}>
<AttachStep index={[1]}>
Copy link
Author

Choose a reason for hiding this comment

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

this ensures using an array of indexes will break no other behaviors.

<Text>{"Test Tour 2"}</Text>
</AttachStep>

Expand Down Expand Up @@ -267,4 +267,47 @@ describe("[Integration] TourOverlay.component.test.tsx", () => {
});
});
});

context("when an AttachStep has multiple indexes", () => {
it("renders all the steps correctly", async() => {
const spy = Sinon.spy<(values: StopParams) => void>(() => undefined);

function TestView(): React.ReactElement {
const { start } = useSpotlightTour();

useEffect(() => {
start();
}, []);

return <View>
<AttachStep index={[0, 1, 2]}>
<Text>{"Test Tour"}</Text>
</AttachStep>
</View>;
}

const { getByText } = render(
<SpotlightTourProvider steps={STEPS} onStop={spy}>
<TestView />
</SpotlightTourProvider>,
);

await waitFor(() => getByText("Step 1"));

fireEvent.press(getByText("Next"));

await waitFor(() => getByText("Step 2"));

fireEvent.press(getByText("Next"));

await waitFor(() => getByText("Step 3"));

fireEvent.press(getByText("Stop"));

Sinon.assert.calledOnceWithExactly(spy, {
index: 2,
isLast: true,
});
});
});
});