Giter Site home page Giter Site logo

pocesar / react-native-stager Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 4.0 242 KB

A performant wizard-like multi stages component for React Native without a router

License: MIT License

TypeScript 58.08% Python 6.13% Java 4.68% Objective-C 15.68% JavaScript 15.42%
react-native react wizard wizard-steps performant react-native-component typescript

react-native-stager's Introduction

Build Status Coverage Status npm version

react-native-stager

A performant wizard-like multi stages component for React Native without a router

Why?

Using a router solution to create a multi-step wizard-like interface is good, but sometimes you want to keep all your state in just one parent component without having to rely on redux for example, enter the Stager

How?

import React from 'react'
import { View, TouchableOpacity, Text } from 'react-native'
import Stager, { Stage } from 'react-native-stager'

class MyWizard extends React.Component {
  render() {
    return (
      <Stager onChange={(stage, direction) => {
        // stage == step 1 || step 2
        // direction = 1 = next | -1 = prev | 0 = reset / initial
      }}>
        <Stage key="step 1" continue={() => true}>
          {({ instance, context }) => (
            <View>
              <TouchableOpacity onPress={context.notify}>
                <Text>{'Hello'}</Text>
              </TouchableOpacity>
            </View>
          )}
        </Stage>

        <Stage key="step 2" noPrevious loaded={(cb) => this.setState({ loaded: true }, cb)}>
          {() => (
            <Text>{'World'}</Text>
          )}
        </Stage>
      </Stager>
    )
  }
}

export default MyWizard

Components and API

Stager

The root component that will hold the steps. Accepts an onChange prop that receives the transitioning stage name and the direction (-1 = prev / 1 = next / 0 = reset/initial). Can be safely nested.

<Stager onChange={(stage, direction) => {
  // do something nice
  }}>
<Stager>

Stage

Need to set inside Stager. Can use continue, noPrevious and loaded props. Notice that the children must always be a function. The key prop is required.

It receives an object with instance (this current Stage) and context (the current Stager)

<Stager>
  <Stage key="step 1">
    {({ instance, context }) => (
      <Text>{'This is step 1'}</Text>
    )}
  </Stage>
</Stager>

When using continue, you always need to signal to the Stage that it should re-evaluate the continue function, to see if you're able to continue. This is so the component doesn't re-render everytime everytime a children changes.

<Stager>
  <Stage
    key="step 1"
    continue={() => this.state.canContinue}
    >
    {({ instance, context }) => (
      <View>
        <Text>{'This is step 1'}</Text>
        <Button title="can continue" onPress={() => {
          this.setState({
            canContinue: true
          }, instance.refresh)
        }} />
      </View>
    )}
  </Stage>

  <Stage
    key="step 2"
    loaded={(cb) => this.setState({ canContinue: false }, cb)}
    continue={() => this.state.canContinue}
    >
    {({ instance, context }) => (
      <View>
        <Text>{'This is step 1'}</Text>
        <Button title="can continue" onPress={() => {
          this.setState({
            canContinue: true
          }, instance.refresh)
        }} />
      </View>
    )}
  </Stage>
</Stager>

StageButtons

The internal implementation of the StageButtons are merely for a quick prototype standpoint (to get the stage going), and you should style if using your own. It doesn't matter where you put them, they will always be below the current active stage. Notice that you CAN set the style to use position: absolute and place it anywhere in the stage.

<Stager>
  <StageButtons>
    {({ context }) => (
      <View>
        <Button title="<" onPress={context.prev} />
        <Button title=">" onPress={context.next} />
      </View>
    )}
  </StageButtons>
</Stager>

StageProgress

The same thing with StageButtons, it's just an ugly placeholder to show functionality. Replace it with your own

<Stager>
  <StageProgress>
    {({ context }) => (
      <View key="progress" style={styles.progressView}>
        <View  style={styles.progressOutterFlex}>
          <View style={styles.progressFlex}>
            {context.state.stages.map((stage, index) => (
                <View key={index} style={[
                  styles.progressIndicator,
                  {
                    flex: (1 / context.state.stages.length) / 2,
                  },
                  {
                    backgroundColor: context.state.currentStage && context.state.stages.indexOf(stage) <= context.state.stages.indexOf(context.state.currentStage) ? 'blue' : 'gray'
                  }
                 ]} />
              )
            )}
          </View>
          <View style={styles.progressPad} />
        </View>
      </View>
    )}
  </StageProgress>
</Stager>

Caveats

  • Since you need to use function children, your shouldComponentUpdate might go crazy. To counter that assign a class member for your function that returns your component
  • The default progress and prev / next buttons are dull, and most likely won't match your application style. For that, use StageProgress and StageButtons wherever you feel like it
  • Children Stage won't automatically update (since Stage has shouldComponentUpdate to return false), so you need, on the instance, to call refresh whenever you need to update your prev / next buttons

License

MIT

react-native-stager's People

Contributors

pocesar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-native-stager's Issues

Screenshots or Demo Video

Before I invest time into using this for my project, can you please provide screenshots and/or a demo video? Thanks!

Override stager/stage styles

Hey, first of all I wanted to thank you for your hard work in this component, it's been super useful and the most adequate solution for my use case.

Cutting to the chase, I want the stage to use 100% of the available height so, no matter what, the stage buttons are always at the bottom of the screen. Is that possible?

StageButtons

How can you hide the previous button and show the next button dependent on the stage index?

I tried:

{context.state.currentStage !== 1 &&
    <Button transparent large onPress={context.prev}>
        <Icon name="ios-arrow-dropleft-circle" />
    </Button>
}
{context.state.stages.length !== context.currentStage() &&
    <Button transparent rounded large onPress={context.next}>
        <Icon name="ios-arrow-dropright-circle" />
    </Button>
}

StageButton onPress called twice then three times

Hi there, I'm with an issue at next button when Stage is loaded it calls onPress twice.

<StageButtons key="buttons">
                            {({context, index})=> (
                                <View style={styles.buttonContainer}>

                                    <TouchableHighlight underlayColor={'#e85e46'} style={styles.button} onPress={context.prev}>
                                        <Text style={styles.buttonText}>{'Voltar'}</Text>
                                    </TouchableHighlight>

                                    <TouchableHighlight underlayColor={'#e85e46'} style={styles.button} onPress={RegisterCompanyMain.handleNext(context)}>
                                        <Text style={styles.buttonText}>{RegisterCompanyMain.buttonLabel(context)}</Text>
                                    </TouchableHighlight>
                                </View>
                            )}

                        </StageButtons>

instance.refresh && state

The following code for updating the state does not update the <Text/>.

<Text>{this.state.gender}</Text>
<Button
  transparent
  style={{ justifyContent: 'flex-start' }}
  onPress={() => {
    this.setState({ gender: 'female' }, instance.refresh);
  }}
>

Unable to resolve module

screen shot 2017-12-27 at 12 39 34 pm

v0.2.3 works fine.

"dependencies": {
    "@expo/vector-icons": "6.2.2",
    "axios": "0.17.1",
    "bson-objectid": "1.2.2",
    "clone": "2.1.1",
    "expo": "24.0.2",
    "lodash": "4.17.4",
    "moment": "2.20.1",
    "native-base": "2.3.5",
    "prop-types": "15.6.0",
    "react": "16.2.0",
    "react-native": "0.51.0",
    "react-native-atoz-list": "1.0.2",
    "react-native-datepicker": "1.6.0",
    "react-native-easy-grid": "0.1.15",
    "react-native-fit-image": "1.5.4",
    "react-native-image-progress": "1.0.1",
    "react-native-modal": "4.1.1",
    "react-native-pin-input": "1.2.9",
    "react-native-stager": "0.2.4",
    "react-native-star-rating": "1.0.8",
    "react-native-svg-image": "2.0.1",
    "react-native-vector-icons": "4.4.3",
    "react-navigation": "1.0.0-beta.21",
    "react-redux": "5.0.6",
    "redux": "3.7.2",
    "redux-logger": "3.0.6",
    "redux-thunk": "2.2.0",
    "sentry-expo": "1.7.0",
    "slack-notify": "0.1.6",
    "validator": "9.2.0"
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.