Skip to content

Commit

Permalink
V3.2 (#275)
Browse files Browse the repository at this point in the history
* Remove Tooltip and StepNumber passed props in favor of useCopilot context

* Un-register step after name change

* Add changelog
  • Loading branch information
mohebifar committed Mar 22, 2023
1 parent 894cf94 commit 0d8362a
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 147 deletions.
7 changes: 7 additions & 0 deletions .changeset/blue-paws-refuse.md
@@ -0,0 +1,7 @@
---
"react-native-copilot": minor
---

Remove Tooltip and StepNumber passed props in favor of useCopilot context
Un-register the step after name change and re-register with the new name
Add tests for CopilotStep
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"build": "tsup",
"dev": "NODE_ENV=development yarn build --watch",
"lint": "eslint src/",
"test": "jest src/",
"test": "jest",
"changeset": "changeset",
"release": "changeset publish"
},
Expand All @@ -36,6 +36,7 @@
},
"homepage": "https://github.com/mohebifar/react-native-copilot#readme",
"devDependencies": {
"@testing-library/jest-native": "^5.4.2",
"@testing-library/react-native": "^12.0.0",
"@tsconfig/react-native": "^2.0.3",
"@types/jest": "^29.5.0",
Expand Down
57 changes: 7 additions & 50 deletions src/components/CopilotModal.tsx
Expand Up @@ -18,28 +18,19 @@ import {
type LayoutRectangle,
type ViewStyle,
} from "react-native";
import type { CopilotOptions, Step } from "../types";
import { useCopilot } from "../contexts/CopilotProvider";
import type { CopilotOptions } from "../types";
import { StepNumber } from "./default-ui/StepNumber";
import { Tooltip } from "./default-ui/Tooltip";
import {
styles,
ARROW_SIZE,
MARGIN,
STEP_NUMBER_DIAMETER,
STEP_NUMBER_RADIUS,
styles,
} from "./style";

type Props = CopilotOptions & {
prev: () => Promise<void>;
next: () => Promise<void>;
stop: () => Promise<void>;
nth: (stepNumber: number) => Promise<void>;
currentStepNumber?: number;
currentStep?: Step;
visible: boolean;
isFirstStep: boolean;
isLastStep: boolean;
};
type Props = CopilotOptions;

const noop = () => {};

Expand All @@ -57,15 +48,6 @@ export interface CopilotModalHandle {
export const CopilotModal = forwardRef<CopilotModalHandle, Props>(
function CopilotModal(
{
stop,
prev,
next,
nth,
currentStepNumber,
currentStep,
visible,
isFirstStep,
isLastStep,
easing = Easing.elastic(0.7),
animationDuration = 400,
tooltipComponent: TooltipComponent = Tooltip,
Expand All @@ -89,6 +71,7 @@ export const CopilotModal = forwardRef<CopilotModalHandle, Props>(
},
ref
) {
const { stop, currentStep, visible } = useCopilot();
const [tooltipStyles, setTooltipStyles] = useState({});
const [arrowStyles, setArrowStyles] = useState({});
const [animatedValues] = useState({
Expand Down Expand Up @@ -262,18 +245,6 @@ export const CopilotModal = forwardRef<CopilotModalHandle, Props>(
setLayout(undefined);
};

const handleNext = () => {
void next();
};

const handleNth = (index: number) => {
void nth(index);
};

const handlePrev = () => {
void prev();
};

const handleStop = () => {
reset();
void stop();
Expand Down Expand Up @@ -365,29 +336,15 @@ export const CopilotModal = forwardRef<CopilotModalHandle, Props>(
},
]}
>
<StepNumberComponent
isFirstStep={isFirstStep}
isLastStep={isLastStep}
currentStep={currentStep}
currentStepNumber={currentStepNumber ?? 0}
/>
<StepNumberComponent />
</Animated.View>

<Animated.View key="arrow" style={[styles.arrow, arrowStyles]} />
<Animated.View
key="tooltip"
style={[styles.tooltip, tooltipStyles, tooltipStyle]}
>
<TooltipComponent
isFirstStep={isFirstStep}
isLastStep={isLastStep}
currentStep={currentStep}
handleNext={handleNext}
handleNth={handleNth}
handlePrev={handlePrev}
handleStop={handleStop}
labels={labels}
/>
<TooltipComponent labels={labels} />
</Animated.View>
</>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/CopilotStep.tsx
Expand Up @@ -51,6 +51,9 @@ export const CopilotStep = ({

useEffect(() => {
if (active) {
if (registeredName.current && registeredName.current !== name) {
unregisterStep(registeredName.current);
}
registerStep({
name,
text,
Expand Down
20 changes: 12 additions & 8 deletions src/components/default-ui/StepNumber.tsx
@@ -1,11 +1,15 @@
import React from "react";
import { View, Text } from "react-native";
import { type StepNumberProps } from "../../types";
import React, { type FunctionComponent } from "react";
import { Text, View } from "react-native";
import { useCopilot } from "../../contexts/CopilotProvider";

import { styles } from "../style";

export const StepNumber = ({ currentStepNumber }: StepNumberProps) => (
<View style={styles.stepNumber}>
<Text style={[styles.stepNumberText]}>{currentStepNumber}</Text>
</View>
);
export const StepNumber: FunctionComponent<unknown> = () => {
const { currentStepNumber } = useCopilot();

return (
<View style={styles.stepNumber}>
<Text style={styles.stepNumberText}>{currentStepNumber}</Text>
</View>
);
};
83 changes: 46 additions & 37 deletions src/components/default-ui/Tooltip.tsx
Expand Up @@ -6,42 +6,51 @@ import { Button } from "./Button";
import { styles } from "../style";

import type { TooltipProps } from "../../types";
import { useCopilot } from "../../contexts/CopilotProvider";

export const Tooltip = ({
isFirstStep,
isLastStep,
handleNext,
handlePrev,
handleStop,
currentStep,
labels,
}: TooltipProps) => (
<View>
<View style={styles.tooltipContainer}>
<Text testID="stepDescription" style={styles.tooltipText}>
{currentStep.text}
</Text>
</View>
<View style={[styles.bottomBar]}>
{!isLastStep ? (
<TouchableOpacity onPress={handleStop}>
<Button>{labels.skip}</Button>
</TouchableOpacity>
) : null}
{!isFirstStep ? (
<TouchableOpacity onPress={handlePrev}>
<Button>{labels.previous}</Button>
</TouchableOpacity>
) : null}
{!isLastStep ? (
<TouchableOpacity onPress={handleNext}>
<Button>{labels.next}</Button>
</TouchableOpacity>
) : (
<TouchableOpacity onPress={handleStop}>
<Button>{labels.finish}</Button>
</TouchableOpacity>
)}
export const Tooltip = ({ labels }: TooltipProps) => {
const { goToNext, goToPrev, stop, currentStep, isFirstStep, isLastStep } =
useCopilot();

const handleStop = () => {
void stop();
};
const handleNext = () => {
void goToNext();
};

const handlePrev = () => {
void goToPrev();
};

return (
<View>
<View style={styles.tooltipContainer}>
<Text testID="stepDescription" style={styles.tooltipText}>
{currentStep?.text}
</Text>
</View>
<View style={[styles.bottomBar]}>
{!isLastStep ? (
<TouchableOpacity onPress={handleStop}>
<Button>{labels.skip}</Button>
</TouchableOpacity>
) : null}
{!isFirstStep ? (
<TouchableOpacity onPress={handlePrev}>
<Button>{labels.previous}</Button>
</TouchableOpacity>
) : null}
{!isLastStep ? (
<TouchableOpacity onPress={handleNext}>
<Button>{labels.next}</Button>
</TouchableOpacity>
) : (
<TouchableOpacity onPress={handleStop}>
<Button>{labels.finish}</Button>
</TouchableOpacity>
)}
</View>
</View>
</View>
);
);
};

0 comments on commit 0d8362a

Please sign in to comment.