| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 | const _ = require('lodash');
const React = require('react');
const { Component } = require('react');
const { View, Text, Platform } = require('react-native');
const { Navigation } = require('react-native-navigation');
const Button = require('./Button');
const testIDs = require('../testIDs');
class PushedScreen extends Component {
  static get options() {
    return {
      _statusBar: {
        visible: false,
        drawBehind: true
      },
      topBar: {
        testID: testIDs.TOP_BAR_ELEMENT,
        rightButtons: {
          id: 'singleBtn',
          text: 'single',
          testID: testIDs.TOP_BAR_BUTTON
        },
        rightButtonColor: 'red',
      },
      layout: {
        backgroundColor: '#f5fcff'
      }
    };
  }
  constructor(props) {
    super(props);
    if (this.props.simulateLongRunningTask) {
      this.simulateLongRunningTask();
    }
    this.onClickPush = this.onClickPush.bind(this);
    this.onClickPop = this.onClickPop.bind(this);
    this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
    this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
    this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
    this.onClickSetStackRoot = this.onClickSetStackRoot.bind(this);
    this.state = { disabled: false };
  }
  simulateLongRunningTask() {
    // tslint:disable-next-line
    for (let i = 0; i < Math.pow(2, 25); i++);
  }
  listeners = [];
  componentDidMount() {
    this.listeners.push(
      Navigation.events().registerComponentDidAppearListener((event) => {
        if (this.state.previewComponentId === event.componentId) {
          this.setState({ disabled: event.type === 'ComponentDidAppear' });
        }
      })
    );
    if (Platform.OS === 'ios') {
      // this.listeners.push(
      //   Navigation.events().registerNativeEventListener((name, params) => {
      //     if (name === 'previewContext') {
      //       const { previewComponentId } = params;
      //       this.setState({ previewComponentId });
      //     }
      //   })
      // );
    }
  }
  componentWillUnmount() {
    this.listeners.forEach(listener => listener.remove && listener.remove());
  }
  render() {
    const stackPosition = this.getStackPosition();
    return (
      <View style={styles.root}>
        <Text testID={testIDs.PUSHED_SCREEN_HEADER} style={styles.h1}>{`Pushed Screen`}</Text>
        <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
        <Button title='Push' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
        {Platform.OS === 'ios' && (
          <Navigation.Element elementId='PreviewElement'>
            <Button testID={testIDs.SHOW_PREVIEW_BUTTON} onPress={this.onClickPush} onPressIn={this.onClickShowPreview} title='Push Preview' />
          </Navigation.Element>
        )}
        <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
        <Button title='Pop Previous' testID={testIDs.POP_PREVIOUS_BUTTON} onPress={this.onClickPopPrevious} />
        <Button title='Pop To Root' testID={testIDs.POP_TO_ROOT} onPress={this.onClickPopToRoot} />
        <Button title='Set Stack Root' testID={testIDs.SET_STACK_ROOT_BUTTON} onPress={this.onClickSetStackRoot} />
        <Button title='Push and Wait for Render' testID={testIDs.PUSH_BUTTON_WAIT_FOR_RENDER} onPress={this.onClickPushWaitForRender} />
        {stackPosition > 2 && <Button title='Pop To Stack Position 1' testID={testIDs.POP_STACK_POSITION_ONE_BUTTON} onPress={this.onClickPopToFirstPosition} />}
        <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
      </View>
    );
  }
  onClickShowPreview = async () => {
    await Navigation.push(this.props.componentId, {
      component: {
        name: 'navigation.playground.PushedScreen',
        passProps: {
          stackPosition: this.getStackPosition() + 1,
          previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
        },
        options: {
          topBar: {
            title: {
              text: `Pushed ${this.getStackPosition() + 1}`
            }
          },
          animations: {
            push: {
              enable: false
            }
          },
          preview: {
            elementId: 'PreviewElement',
            height: 400,
            commit: true,
            actions: [{
              id: 'action-cancel',
              title: 'Cancel'
            }]
          }
        }
      }
    });
  }
  async onClickPush() {
    if (this.state.disabled) {
      return;
    }
    await Navigation.push(this.props.componentId, {
      component: {
        name: 'navigation.playground.PushedScreen',
        passProps: {
          stackPosition: this.getStackPosition() + 1,
          previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
        },
        options: {
          topBar: {
            title: {
              text: `Pushed ${this.getStackPosition() + 1}`
            }
          }
        }
      }
    });
  }
  async onClickPop() {
    await Navigation.pop(this.props.componentId);
  }
  async onClickPopPrevious() {
    await Navigation.pop(_.last(this.props.previousScreenIds));
  }
  async onClickPopToFirstPosition() {
    await Navigation.popTo(this.props.previousScreenIds[0]);
  }
  async onClickPopToRoot() {
    await Navigation.popToRoot(this.props.componentId);
  }
  async onClickSetStackRoot() {
    await Navigation.setStackRoot(this.props.componentId, {
      component: {
        name: 'navigation.playground.PushedScreen',
        passProps: {
          stackPosition: this.getStackPosition() + 1,
          previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
        },
        options: {
          animations: {
            setStackRoot: {
              enable: false
            }
          },
          topBar: {
            title: {
              text: `Pushed ${this.getStackPosition() + 1}`
            }
          }
        }
      }
    });
  }
  onClickPushWaitForRender = async () => {
    await Navigation.push(this.props.componentId, {
      component: {
        name: 'navigation.playground.PushedScreen',
        passProps: {
          stackPosition: this.getStackPosition() + 1,
          previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId),
          simulateLongRunningTask: true
        },
        options: {
          layout: {
            backgroundColor: 'transparent'
          },
          topBar: {
            title: {
              text: `Pushed ${this.getStackPosition() + 1}`
            }
          },
          animations: {
            push: {
              waitForRender: true
            }
          }
        }
      }
    });
  }
  getStackPosition() {
    return this.props.stackPosition || 1;
  }
}
const styles = {
  root: {
    flexGrow: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#f5fcff'
  },
  h1: {
    fontSize: 24,
    textAlign: 'center',
    margin: 10
  },
  h2: {
    fontSize: 12,
    textAlign: 'center',
    margin: 10
  },
  footer: {
    fontSize: 10,
    color: '#888',
    marginTop: 10
  }
};
module.exports = PushedScreen;
 |