Skip to content

fix(attach-step): Composed components being wrapped #161

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

Merged
merged 9 commits into from
Jan 29, 2025
Merged
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
59 changes: 24 additions & 35 deletions package/src/lib/components/attach-step/AttachStep.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import React, {
useEffect,
useRef,
} from "react";
import { LayoutChangeEvent, StyleProp, View } from "react-native";
import { LayoutChangeEvent, StyleProp, View, ViewStyle } from "react-native";

import { SpotlightTourContext } from "../../SpotlightTour.context";

export interface ChildProps<T> {
export interface ChildProps {
/**
* A React children, if any.
*/
Expand All @@ -27,18 +27,14 @@ export interface ChildProps<T> {
* A React reference.
*/
ref: RefObject<unknown>;
/**
* The style prop.
*/
style: StyleProp<T>;
}

export interface AttachStepProps<T> {
export interface AttachStepProps {
/**
* The element in which the spotlight will be to wrapped to in the specified
* step of the tour.
*/
children: ReactElement<ChildProps<T>>;
children: ReactElement<ChildProps>;
/**
* When `AttachStep` wraps a Functional Component, it needs to add an
* additional `View` on top of it to be able to measure the layout upon
Expand All @@ -58,6 +54,10 @@ export interface AttachStepProps<T> {
* It can be a single index or multiple ones.
*/
index: number | Array<number>;
/**
* Style applied to AttachStep wrapper
*/
style?: StyleProp<ViewStyle>;
}

/**
Expand All @@ -67,7 +67,7 @@ export interface AttachStepProps<T> {
* @param props the component props
* @returns an AttachStep React element
*/
export function AttachStep<T>({ children, fill = false, index }: AttachStepProps<T>): ReactElement {
export function AttachStep({ children, fill = false, index, style }: AttachStepProps): ReactElement {
const { current, changeSpot } = useContext(SpotlightTourContext);

const ref = useRef<View>(null);
Expand All @@ -91,31 +91,20 @@ export function AttachStep<T>({ children, fill = false, index }: AttachStepProps
updateSpot();
}, [updateSpot]);

if (typeof children.type === "function") {
const { style, ...rest } = children.props;
const childStyle = style ?? { };

return (
<View
testID="attach-wrapper-view"
ref={ref}
style={{ alignSelf: fill ? "stretch" : "flex-start", ...childStyle }}
collapsable={false}
focusable={false}
onLayout={updateSpot}
>
{cloneElement(
children,
rest,
children.props.children,
)}
</View>
);
}

return cloneElement(
children,
{ ...children.props, onLayout, ref },
children.props?.children,
return (
<View
testID="attach-wrapper-view"
ref={ref}
style={[{ alignSelf: fill ? "stretch" : "flex-start" }, style]}
collapsable={false}
focusable={false}
onLayout={onLayout}
>
{cloneElement(
children,
children.props,
children.props.children,
)}
</View>
);
}
12 changes: 6 additions & 6 deletions package/test/integration/lib/AttachStep.component.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from "@assertive-ts/core";
import { render, waitFor, within } from "@testing-library/react-native";
import React, { ReactElement, forwardRef } from "react";
import { Text } from "react-native";
Expand Down Expand Up @@ -26,18 +25,19 @@ function CustomText(): ReactElement {

suite("[Integration] AttachStep.component.test.tsx", () => {
describe("when a native component is passed as child", () => {
it("renders the child without wrapping it on a native View", async () => {
const { getByText, queryByTestId } = render(
it("renders the child wrapping it on a native View", async () => {
const { getByTestId } = render(
<SpotlightTourProvider steps={[]}>
<AttachStep index={0}>
<NativeText />
</AttachStep>
</SpotlightTourProvider>,
);

await waitFor(() => getByText("Native Text"));

expect(queryByTestId("attach-wrapper-view")).toBeNull();
await waitFor(() =>
within(getByTestId("attach-wrapper-view"))
.getByText("Native Text"),
);
});
});

Expand Down
Loading