Skip to content

Commit 40bb40f

Browse files
authored
Merge pull request #41 from postillonmedia/master
Exclude the StepNumber into its own component and made it replaceable via props
2 parents 493e532 + 452b6a8 commit 40bb40f

5 files changed

Lines changed: 67 additions & 9 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ copilot({
136136
})(RootComponent)
137137
```
138138

139+
### Custom step number component
140+
You can customize the step number by passing a component to the `copilot` HOC maker. If you are looking for an example step number component, take a look at [the default step number implementation](https://github.com/okgrow/react-native-copilot/blob/master/src/components/StepNumber.js).
141+
142+
```js
143+
const StepNumberComponent = ({
144+
isFirstStep,
145+
isLastStep,
146+
currentStep,
147+
currentStepNumber,
148+
}) => (
149+
// ...
150+
);
151+
152+
copilot({
153+
stepNumberComponent: StepNumberComponent
154+
})(RootComponent)
155+
```
156+
139157
### Custom components as steps
140158
The components wrapped inside `CopilotStep`, will receive a `copilot` prop of type `Object` which the outermost rendered element of the component or the element that you want the tooltip be shown around, must extend.
141159

src/components/CopilotModal.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React, { Component } from 'react';
33
import { Animated, Easing, View, Text, NativeModules, Modal, StatusBar, Platform } from 'react-native';
44
import Tooltip from './Tooltip';
5+
import StepNumber from './StepNumber';
56
import styles, { MARGIN, ARROW_SIZE, STEP_NUMBER_DIAMETER, STEP_NUMBER_RADIUS } from './style';
67

78
type Props = {
@@ -16,6 +17,7 @@ type Props = {
1617
easing: ?func,
1718
animationDuration: ?number,
1819
tooltipComponent: ?React$Component,
20+
stepNumberComponent: ?React$Component,
1921
overlay: 'svg' | 'view',
2022
animated: boolean,
2123
androidStatusBarVisible: boolean,
@@ -39,6 +41,7 @@ class CopilotModal extends Component<Props, State> {
3941
easing: Easing.elastic(0.7),
4042
animationDuration: 400,
4143
tooltipComponent: Tooltip,
44+
stepNumberComponent: StepNumber,
4245
// If react-native-svg native module was avaialble, use svg as the default overlay component
4346
overlay: typeof NativeModules.RNSVGSvgViewManager !== 'undefined' ? 'svg' : 'view',
4447
// If animated was not specified, rely on the default overlay type
@@ -235,20 +238,25 @@ class CopilotModal extends Component<Props, State> {
235238
}
236239

237240
renderTooltip() {
238-
const { tooltipComponent: TooltipComponent } = this.props;
241+
const { tooltipComponent: TooltipComponent, stepNumberComponent: StepNumberComponent } = this.props;
239242

240243
return [
241244
<Animated.View
242245
key="stepNumber"
243246
style={[
244-
styles.stepNumber,
247+
styles.stepNumberContainer,
245248
{
246249
left: this.state.animatedValues.stepNumberLeft,
247250
top: Animated.add(this.state.animatedValues.top, -STEP_NUMBER_RADIUS),
248251
},
249252
]}
250253
>
251-
<Text style={[styles.stepNumberText]}>{this.props.currentStepNumber}</Text>
254+
<StepNumberComponent
255+
isFirstStep={this.props.isFirstStep}
256+
isLastStep={this.props.isLastStep}
257+
currentStep={this.props.currentStep}
258+
currentStepNumber={this.props.currentStepNumber}
259+
/>
252260
</Animated.View>,
253261
<Animated.View key="arrow" style={[styles.arrow, this.state.arrow]} />,
254262
<Animated.View key="tooltip" style={[styles.tooltip, this.state.tooltip]}>

src/components/StepNumber.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @flow
2+
import React from 'react';
3+
import { View, Text } from 'react-native';
4+
5+
import styles from './style';
6+
7+
import type { Step } from '../types';
8+
9+
type Props = {
10+
isFirstStep: boolean,
11+
isLastStep: boolean,
12+
currentStep: Step,
13+
currentStepNumber: number,
14+
};
15+
16+
const StepNumber = ({
17+
isFirstStep,
18+
isLastStep,
19+
currentStep,
20+
currentStepNumber,
21+
}: Props) => (
22+
<View style={styles.stepNumber}>
23+
<Text style={[styles.stepNumberText]}>{currentStepNumber}</Text>
24+
</View>
25+
);
26+
27+
export default StepNumber;

src/components/style.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,21 @@ export default StyleSheet.create({
3636
tooltipContainer: {
3737
flex: 1,
3838
},
39-
stepNumber: {
39+
stepNumberContainer: {
4040
position: 'absolute',
4141
width: STEP_NUMBER_DIAMETER,
4242
height: STEP_NUMBER_DIAMETER,
43+
overflow: 'hidden',
44+
zIndex: ZINDEX + 1,
45+
},
46+
stepNumber: {
47+
flex: 1,
48+
alignItems: 'center',
49+
justifyContent: 'center',
4350
borderWidth: 2,
4451
borderRadius: STEP_NUMBER_RADIUS,
4552
borderColor: '#FFFFFF',
4653
backgroundColor: '#27ae60',
47-
overflow: 'hidden',
48-
alignItems: 'center',
49-
justifyContent: 'center',
50-
zIndex: ZINDEX + 1,
5154
},
5255
stepNumberText: {
5356
fontSize: 10,

src/hocs/copilot.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type State = {
2929
const copilot = ({
3030
overlay,
3131
tooltipComponent,
32+
stepNumberComponent,
3233
animated,
3334
androidStatusBarVisible,
3435
} = {}) =>
@@ -160,7 +161,7 @@ const copilot = ({
160161

161162
componentWillUnmount() {
162163
this.mounted = false;
163-
};
164+
}
164165

165166
render() {
166167
return (
@@ -181,6 +182,7 @@ const copilot = ({
181182
isLastStep={this.isLastStep()}
182183
currentStepNumber={this.getStepNumber()}
183184
currentStep={this.state.currentStep}
185+
stepNumberComponent={stepNumberComponent}
184186
tooltipComponent={tooltipComponent}
185187
overlay={overlay}
186188
animated={animated}

0 commit comments

Comments
 (0)