YRLoading.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Created by Arison on 2019/8/14.
  3. */
  4. 'use strict'
  5. import React from 'react';
  6. import PropTypes from 'prop-types'
  7. import { StyleSheet, Dimensions, Text, View, Modal, ActivityIndicator } from 'react-native';
  8. const SCREEN_WIDTH = Dimensions.get('window').width;
  9. const SCREEN_HEIGHT = Dimensions.get('window').height;
  10. export class EasyLoading {
  11. constructor() {
  12. }
  13. static bind(loading, key = 'default') {
  14. loading && (this.map[key] = loading);
  15. }
  16. static unBind(key = 'default') {
  17. this.map[key] = null
  18. delete this.map[key];
  19. }
  20. static show(text = 'Loading...', timeout = -1, key = 'default') {
  21. this.map[key] && this.map[key].setState({ "isShow": true, "text": text, "timeout": timeout });
  22. }
  23. static dismis(text = 'Loading...', key = 'default') {
  24. this.map[key] && this.map[key].setState({ text: text, isShow: false });
  25. }
  26. }
  27. EasyLoading.map = {};
  28. export class Loading extends React.Component {
  29. static propTypes = {
  30. type: PropTypes.string,
  31. color: PropTypes.string,
  32. textStyle: PropTypes.any,
  33. loadingStyle: PropTypes.any,
  34. };
  35. constructor(props) {
  36. super(props);
  37. let handle = 0;
  38. this.state = {
  39. isShow: false,
  40. timeout: -1,
  41. text: "Loading..."
  42. }
  43. EasyLoading.bind(this, this.props.type || 'default');
  44. }
  45. componentWillUnmount() {
  46. clearTimeout(this.handle);
  47. EasyLoading.unBind(this.props.type || 'default');
  48. }
  49. render() {
  50. clearTimeout(this.handle);
  51. (this.state.timeout != -1) && (this.handle = setTimeout(() => {
  52. EasyLoading.dismis(this.props.type || 'default');
  53. }, this.state.timeout));
  54. return (
  55. <Modal
  56. animationType={"fade"}
  57. transparent={true}
  58. visible={this.state.isShow}
  59. onRequestClose={() => {this.setState({
  60. isShow:false
  61. }) } }>
  62. <View style={[styles.load_box, this.props.loadingStyle]}>
  63. <ActivityIndicator animating={true} color={this.props.color || '#FFF'} size={'large'} style={styles.load_progress} />
  64. <Text style={[styles.load_text, this.props.textStyle]}>{this.state.text}</Text>
  65. </View>
  66. </Modal>
  67. );
  68. }
  69. }
  70. const styles = StyleSheet.create({
  71. load_box: {
  72. width: 100,
  73. height: 100,
  74. backgroundColor: '#0008',
  75. alignItems: 'center',
  76. marginLeft: SCREEN_WIDTH / 2 - 50,
  77. marginTop: SCREEN_HEIGHT / 2 - 50,
  78. borderRadius: 10
  79. },
  80. load_progress: {
  81. position: 'absolute',
  82. width: 100,
  83. height: 90
  84. },
  85. load_text: {
  86. marginTop: 70,
  87. color: '#FFF',
  88. }
  89. });