import _ from 'lodash';
import React, {Component} from 'react';
import {StyleSheet, ScrollView} from 'react-native';
import {View, Button, Wizard, Text, RadioGroup, RadioButton, TextField, Toast} from 'react-native-ui-lib';
const flavors = ['Chocolate', 'Vanilla'];
const initialFlavor = flavors[0];
const stepTypes = _.map(Wizard.States, state => {
return {state};
});
interface State {
activeIndex: number;
completedStepIndex?: number;
allTypesIndex: number;
selectedFlavor: string;
customerName?: string;
toastMessage?: string;
}
export default class WizardScreen extends Component<{}, State> {
state = {
activeIndex: 0,
completedStepIndex: undefined,
allTypesIndex: 0,
selectedFlavor: initialFlavor,
customerName: undefined,
toastMessage: undefined
};
onActiveIndexChanged = (activeIndex: number) => {
this.setState({activeIndex});
};
onAllTypesIndexChanged = (allTypesIndex: number) => {
this.setState({allTypesIndex});
};
closeToast = () => {
setTimeout(() => {
this.setState({toastMessage: undefined});
}, 2000);
};
reset = () => {
const {customerName, selectedFlavor} = this.state;
this.setState({
activeIndex: 0,
completedStepIndex: undefined,
selectedFlavor: initialFlavor,
customerName: undefined,
toastMessage: `${customerName}, you bought some ${selectedFlavor.toLowerCase()}`
},
this.closeToast);
};
goToPrevStep = () => {
const {activeIndex: prevActiveIndex} = this.state;
const activeIndex = prevActiveIndex === 0 ? 0 : prevActiveIndex - 1;
this.setState({activeIndex});
};
renderPrevButton = () => {
return (
);
};
goToNextStep = () => {
const {activeIndex: prevActiveIndex, completedStepIndex: prevCompletedStepIndex} = this.state;
const reset = prevActiveIndex === 2;
if (reset) {
this.reset();
return;
}
const activeIndex = prevActiveIndex + 1;
let completedStepIndex: number | undefined = prevCompletedStepIndex;
if (!prevCompletedStepIndex || prevCompletedStepIndex < prevActiveIndex) {
completedStepIndex = prevActiveIndex;
}
if (activeIndex !== prevActiveIndex || completedStepIndex !== prevCompletedStepIndex) {
this.setState({activeIndex, completedStepIndex});
}
};
renderNextButton = (disabled?: boolean) => {
const {activeIndex} = this.state;
const label = activeIndex === 2 ? 'done & reset' : 'next';
return (
);
};
renderFlavorRadioButton = (index: number) => {
const value = flavors[index];
return 0} value={value} label={value}/>;
};
setSelectedFlavor = (selectedFlavor: string) => {
this.setState({selectedFlavor});
};
renderSelectItems = () => {
const {selectedFlavor} = this.state;
return (
Please select your purchase
{this.renderFlavorRadioButton(0)}
{this.renderFlavorRadioButton(1)}
{this.renderNextButton()}
);
};
onNameEntered = (customerName: string) => {
this.setState({customerName});
};
renderCustomerDetails = () => {
const {customerName} = this.state;
return (
{this.renderPrevButton()}
{/* @ts-expect-error */}
{this.renderNextButton(_.isNil(customerName) || customerName.trim().length === 0)}
);
};
renderCheckout = () => {
return (
Complete the purchase
{this.renderPrevButton()}
{this.renderNextButton()}
);
};
renderCurrentStep = () => {
const {activeIndex} = this.state;
switch (activeIndex) {
case 0:
default:
return this.renderSelectItems();
case 1:
return this.renderCustomerDetails();
case 2:
return this.renderCheckout();
}
};
getStepState(index: number) {
const {activeIndex, completedStepIndex} = this.state;
let state = Wizard.States.DISABLED;
if (completedStepIndex && completedStepIndex > index - 1) {
state = Wizard.States.COMPLETED;
} else if (activeIndex === index || completedStepIndex === index - 1) {
state = Wizard.States.ENABLED;
}
return state;
}
render() {
const {activeIndex, allTypesIndex, toastMessage} = this.state;
return (
{this.renderCurrentStep()}
All step types: (Wizard.States.X)
{stepTypes}
{_.map(Wizard.States, state => {
return ;
})}
Wizard
{!_.isNil(toastMessage) && }
);
}
}
const styles = StyleSheet.create({
scrollView: {
flex: 1
},
container: {
flex: 1
},
allTypes: {
justifyContent: 'space-between'
},
stepContainer: {
flex: 1,
justifyContent: 'space-between',
margin: 20
}
});