Skip to content

Commit b05929d

Browse files
authored
Typescript/wizard move to typescript (#1390)
* Rename files * Rename files(2) * Rename files(3) * Move Wizard to typescript * Change from false to undefiend
1 parent 883f5d3 commit b05929d

File tree

11 files changed

+296
-114
lines changed

11 files changed

+296
-114
lines changed

demo/src/screens/componentScreens/WizardScreen.js renamed to demo/src/screens/componentScreens/WizardScreen.tsx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,30 @@ const stepTypes = _.map(Wizard.States, state => {
99
return <Text key={state}>{state}</Text>;
1010
});
1111

12-
export default class WizardScreen extends Component {
13-
constructor(props) {
14-
super(props);
12+
interface State {
13+
activeIndex: number;
14+
completedStepIndex?: number;
15+
allTypesIndex: number;
16+
selectedFlavor: string;
17+
customerName?: string;
18+
toastMessage?: string;
19+
}
1520

16-
this.state = {
17-
activeIndex: 0,
18-
completedStepIndex: undefined,
19-
allTypesIndex: 0,
20-
selectedFlavor: initialFlavor,
21-
customerName: undefined,
22-
toastMessage: undefined
23-
};
24-
}
21+
export default class WizardScreen extends Component<{}, State> {
22+
state = {
23+
activeIndex: 0,
24+
completedStepIndex: undefined,
25+
allTypesIndex: 0,
26+
selectedFlavor: initialFlavor,
27+
customerName: undefined,
28+
toastMessage: undefined
29+
};
2530

26-
onActiveIndexChanged = activeIndex => {
31+
onActiveIndexChanged = (activeIndex: number) => {
2732
this.setState({activeIndex});
2833
};
2934

30-
onAllTypesIndexChanged = allTypesIndex => {
35+
onAllTypesIndexChanged = (allTypesIndex: number) => {
3136
this.setState({allTypesIndex});
3237
};
3338

@@ -88,7 +93,7 @@ export default class WizardScreen extends Component {
8893
}
8994
};
9095

91-
renderNextButton = disabled => {
96+
renderNextButton = (disabled?: boolean) => {
9297
const {activeIndex} = this.state;
9398
const label = activeIndex === 2 ? 'done & reset' : 'next';
9499

@@ -104,12 +109,12 @@ export default class WizardScreen extends Component {
104109
);
105110
};
106111

107-
renderFlavorRadioButton = index => {
112+
renderFlavorRadioButton = (index: number) => {
108113
const value = flavors[index];
109114
return <RadioButton testID={value} marginL-10={index > 0} value={value} label={value}/>;
110115
};
111116

112-
setSelectedFlavor = selectedFlavor => {
117+
setSelectedFlavor = (selectedFlavor: string) => {
113118
this.setState({selectedFlavor});
114119
};
115120

@@ -130,7 +135,7 @@ export default class WizardScreen extends Component {
130135
);
131136
};
132137

133-
onNameEntered = customerName => {
138+
onNameEntered = (customerName: string) => {
134139
this.setState({customerName});
135140
};
136141

@@ -178,7 +183,7 @@ export default class WizardScreen extends Component {
178183
}
179184
};
180185

181-
getStepState(index) {
186+
getStepState(index: number) {
182187
const {activeIndex, completedStepIndex} = this.state;
183188
let state = Wizard.States.DISABLED;
184189
if (completedStepIndex > index - 1) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { WizardStepsConfig } from './types';
2+
export declare const StatesConfig: WizardStepsConfig;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import { WizardStepProps } from './types';
3+
declare const _default: React.ComponentClass<WizardStepProps & {
4+
useCustomTheme?: boolean | undefined;
5+
}, any>;
6+
export default _default;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { Component } from 'react';
2+
import WizardStep from './WizardStep';
3+
import { WizardProps, WizardStepProps, WizardStepStates, WizardStepConfig, WizardStepsConfig } from './types';
4+
export { WizardProps, WizardStepProps, WizardStepStates, WizardStepConfig, WizardStepsConfig };
5+
interface State {
6+
maxWidth: number;
7+
}
8+
/**
9+
* @description: Wizard Component: a wizard presents a series of steps in prescribed order
10+
* that the user needs to complete in order to accomplish a goal (e.g. purchase a product).
11+
*
12+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/WizardScreen.js
13+
* @notes: Use Wizard with nested Wizard.Step(s) to achieve the desired result.
14+
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Wizard/Wizard.gif?raw=true
15+
* @image: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Wizard/WizardPresets.png?raw=true
16+
*/
17+
declare class Wizard extends Component<WizardProps, State> {
18+
static displayName: string;
19+
static Step: typeof WizardStep;
20+
static States: typeof WizardStepStates;
21+
constructor(props: WizardProps);
22+
componentDidMount(): void;
23+
componentWillUnmount(): void;
24+
onOrientationChange: () => void;
25+
getMaxWidth(): number;
26+
renderChildren(): React.DetailedReactHTMLElement<any, HTMLElement>[] | null | undefined;
27+
render(): JSX.Element;
28+
}
29+
declare const _default: React.ComponentClass<WizardProps & {
30+
useCustomTheme?: boolean | undefined;
31+
}, any> & typeof Wizard;
32+
export default _default;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { ImageProps } from '../image';
2+
import { StyleProp, TextStyle, ViewStyle } from 'react-native';
3+
export declare enum WizardStepStates {
4+
ENABLED = "enabled",
5+
DISABLED = "disabled",
6+
ERROR = "error",
7+
SKIPPED = "skipped",
8+
COMPLETED = "completed"
9+
}
10+
export interface WizardStepProps {
11+
/**
12+
* The state of the step (Wizard.States.X)
13+
*/
14+
state: WizardStepStates;
15+
/**
16+
* The label of the item
17+
*/
18+
label?: string;
19+
/**
20+
* Additional styles for the label
21+
*/
22+
labelStyle?: StyleProp<TextStyle>;
23+
/**
24+
* Additional styles for the connector
25+
*/
26+
connectorStyle?: StyleProp<ViewStyle>;
27+
/**
28+
* Color of the step index (or of the icon, when provided)
29+
*/
30+
color?: string;
31+
/**
32+
* Color of the circle
33+
*/
34+
circleColor?: string;
35+
/**
36+
* The step's circle size (diameter)
37+
*/
38+
circleSize?: number;
39+
/**
40+
* Circle's background color
41+
*/
42+
circleBackgroundColor?: string;
43+
/**
44+
* Icon to replace the (default) index
45+
*/
46+
icon?: ImageProps;
47+
/**
48+
* Additional styles for the index's label (when icon is not provided)
49+
*/
50+
indexLabelStyle?: StyleProp<TextStyle>;
51+
/**
52+
* Whether the step should be enabled
53+
*/
54+
enabled?: boolean;
55+
/**
56+
* Extra text to be read in accessibility mode
57+
*/
58+
accessibilityInfo?: string;
59+
}
60+
export declare type WizardStepConfig = Omit<WizardStepProps, 'state'>;
61+
export interface WizardStepsConfig {
62+
enabled?: WizardStepConfig;
63+
disabled?: WizardStepConfig;
64+
error?: WizardStepConfig;
65+
skipped?: WizardStepConfig;
66+
completed?: WizardStepConfig;
67+
active?: WizardStepConfig;
68+
}
69+
export interface WizardProps {
70+
/**
71+
* The active step's index
72+
*/
73+
activeIndex: number;
74+
/**
75+
* The configuration of the active step (see Wizard.Step.propTypes)
76+
*/
77+
activeConfig?: WizardStepProps;
78+
/**
79+
* Callback that is called when the active step is changed (i.e. a step was clicked on).
80+
* The new activeIndex will be the input of the callback.
81+
*/
82+
onActiveIndexChanged?: (index: number) => void;
83+
/**
84+
* Add or override style of the container
85+
*/
86+
containerStyle?: StyleProp<ViewStyle>;
87+
testID?: string;
88+
}

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export {default as Dialog, DialogProps} from './components/dialog';
7575
export {default as PageControl, PageControlProps} from './components/pageControl';
7676
export {default as Carousel, CarouselProps, PageControlPosition} from './components/carousel';
7777
export {default as ActionSheet} from './components/actionSheet';
78+
export {default as Wizard, WizardProps, WizardStepProps, WizardStepStates, WizardStepConfig, WizardStepsConfig} from './components/wizard';
7879

7980
/* All components with manual typings */
8081
export {
@@ -120,4 +121,3 @@ export const SafeAreaInsetsManager;
120121
export const SafeAreaSpacerView;
121122
export const SelectableComponent;
122123
export const TextField;
123-
export const Wizard;

src/components/wizard/WizardStates.js renamed to src/components/wizard/WizardStates.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import Colors from '../../style/colors';
22
const checkMarkSmall = require('./assets/checkMarkSmall.png');
33
const exclamationSmall = require('./assets/exclamationSmall.png');
4+
import {WizardStepsConfig} from './types';
45

5-
export const States = {
6-
ENABLED: 'enabled',
7-
DISABLED: 'disabled',
8-
ERROR: 'error',
9-
SKIPPED: 'skipped',
10-
COMPLETED: 'completed'
11-
};
12-
13-
export const StatesConfig = {
6+
export const StatesConfig: WizardStepsConfig = {
147
enabled: {color: Colors.dark30, circleColor: Colors.dark60, enabled: true},
158
disabled: {color: Colors.dark50, circleColor: Colors.dark60},
169
error: {color: Colors.red30, icon: exclamationSmall, enabled: true, accessibilityInfo: 'Validation Error'},

src/components/wizard/WizardStep.js renamed to src/components/wizard/WizardStep.tsx

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import _ from 'lodash';
2-
import PropTypes from 'prop-types';
3-
import React from 'react';
2+
import React, {Component} from 'react';
43
import {StyleSheet} from 'react-native';
54
import View from '../view';
65
import Text from '../text';
76
import Image from '../image';
87
import TouchableOpacity from '../touchableOpacity';
9-
import {PureBaseComponent} from '../../commons';
8+
import {asBaseComponent} from '../../commons/new';
109
import Colors from '../../style/colors';
1110
import BorderRadiuses from '../../style/borderRadiuses';
1211
import Spacings from '../../style/spacings';
13-
import {States, StatesConfig} from './WizardStates';
12+
import {WizardStepProps, WizardProps} from './types';
13+
import {StatesConfig} from './WizardStates';
14+
15+
// Includes private props from the Wizard
16+
interface Props extends WizardStepProps, Omit<WizardProps, 'onActiveIndexChanged | containerStyle'> {
17+
index: number;
18+
maxWidth: number;
19+
onPress: WizardProps['onActiveIndexChanged'];
20+
}
1421

1522
/**
1623
* @description: WizardStep Component: a wizard presents a series of steps in prescribed order
@@ -19,62 +26,11 @@ import {States, StatesConfig} from './WizardStates';
1926
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/WizardScreen.js
2027
* @notes: Use Wizard with nested Wizard.Step(s) to achieve the desired result.
2128
*/
22-
export default class WizardStep extends PureBaseComponent {
29+
class WizardStep extends Component<Props> {
2330
static displayName = 'Wizard.Step';
2431

25-
static propTypes = {
26-
/**
27-
* The state of the step (Wizard.States.X)
28-
*/
29-
state: PropTypes.oneOf(Object.values(States)),
30-
/**
31-
* The label of the item
32-
*/
33-
label: PropTypes.string,
34-
/**
35-
* Additional styles for the label
36-
*/
37-
labelStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
38-
/**
39-
* Additional styles for the connector
40-
*/
41-
connectorStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
42-
/**
43-
* Color of the step index (or of the icon, when provided)
44-
*/
45-
color: PropTypes.string,
46-
/**
47-
* Color of the circle
48-
*/
49-
circleColor: PropTypes.string,
50-
/**
51-
* The step's circle size (diameter)
52-
*/
53-
circleSize: PropTypes.number,
54-
/**
55-
* Circle's background color
56-
*/
57-
circleBackgroundColor: PropTypes.string,
58-
/**
59-
* Icon to replace the (default) index
60-
*/
61-
icon: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
62-
/**
63-
* Additional styles for the index's label (when icon is not provided)
64-
*/
65-
indexLabelStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
66-
/**
67-
* Whether the step should be enabled
68-
*/
69-
enabled: PropTypes.bool,
70-
/**
71-
* Extra text to be read in accessibility mode
72-
*/
73-
accessibilityInfo: PropTypes.string
74-
};
75-
7632
getProps() {
77-
const props = this.getThemeProps();
33+
const props = this.props;
7834
const {state, activeConfig: propsActiveConfig, index, activeIndex} = props;
7935
const config = StatesConfig[state];
8036
const activeConfig = index === activeIndex ? propsActiveConfig : {};
@@ -88,7 +44,7 @@ export default class WizardStep extends PureBaseComponent {
8844
return `Step ${index + 1}, ${label}, ${extraInfo}`;
8945
}
9046

91-
renderCircle(props) {
47+
renderCircle(props: Props) {
9248
const {
9349
testID,
9450
index,
@@ -153,6 +109,8 @@ export default class WizardStep extends PureBaseComponent {
153109
}
154110
}
155111

112+
export default asBaseComponent<WizardStepProps>(WizardStep);
113+
156114
const styles = StyleSheet.create({
157115
connector: {
158116
borderWidth: 0.5,

0 commit comments

Comments
 (0)