0.2.0 • Published 7 years ago

rn-countdown-demo v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

rn-countdown

npm npm npm

A countdown component for react-native APPs. You should use this component to request a verification code that supports custom styles for different status.

Preview

demo

Install

Install with npm:

npm install rn-countdown --save

or with yarn:

yarn add rn-countdown

Usage

import React, {Component} from 'react';
import {
  StyleSheet,
  View,
  Button,
  TextInput,
  Text,
  Dimensions
} from 'react-native';
import CountdownView from 'rn-countdown';

export default class RNCountdownDemo extends Component {

  state = {
    hasText: false
  };
  phoneNumber = '';

  shouldStartCountdown = () => {
    if (this.phoneNumber) return true;

    alert('电话号码不能为空!');
    return false;
  };
  
  handleNetworkFailed = () => alert('network failed');

  handleStopCountdown = () => {
    this.countdown && this.countdown.stopCountdown();
  };

  handleChangeText = text => {
    this.phoneNumber = text;
    this.setState({hasText: !!this.phoneNumber})
  };

  render() {
    const style = this.state.hasText ? {backgroundColor: 'rgb(59, 197, 81)', borderWidth: 0} : {};
    const title = this.state.hasText ? {color: '#fff'} : {};

    return (
      <View style={styles.container}>
        <View style={styles.phoneCell}>
          <View style={styles.phoneInfo}>
            <Text>账号:</Text>
            <TextInput
              style={styles.input}
              placeholder="请输入手机号码"
              underlineColorAndroid="transparent"
              onChangeText={this.handleChangeText}
            />
          </View>
          <CountdownView
            ref={r => this.countdown = r}
            time={10}
            title="发送验证码"
            overTitle="重新发送"
            style={[styles.countdown, style]}
            titleStyle={[styles.countdownTitle, title]}
            countingTitleTemplate="发送中({time})"
            countingStyle={styles.countingdown}
            countingTitleStyle={styles.countingTitle}
            shouldStartCountdown={this.shouldStartCountdown}
            onNetworkFailed={this.handleNetworkFailed}
          />
        </View>
        <Button title="停止" onPress={this.handleStopCountdown}/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  phoneCell: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: 15,
    height: 40,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderColor: '#ebebeb',
    width: Dimensions.get('window').width,
    backgroundColor: '#fff'
  },
  phoneInfo: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  input: {
    height: 30,
    width: Dimensions.get('window').width * 0.4,
    marginLeft: 10,
    padding: 0,
    fontSize: 14
  },
  countdown: {
    borderRadius: 15,
  },
  countingdown: {
    backgroundColor: 'transparent',
    borderWidth: StyleSheet.hairlineWidth
  },
  countdownTitle: {color: '#ccc'},
  countingTitle: {color: '#ccc'}
});

Props

PropTypeOptionalDefaultDescription
styleViewPropTypesYesnonecustom container style
titlestringYes获取短信验证码initial title
timenumberYes30stimer seconds
overTitlestringYes重新获取the title when countdown over
titleStyleobjectYesnonefont style of countdown title
countingStyleViewPropTypesYesnonecustom style when counting down
countingTitleTemplatestringYes{time}s后重新获取counting down title, must conform to the format that contain {time}
countingTitleStyleobjectYesnonecustom title style when counting down
shouldStartCountdownfunctionYesreturn truebefore start countdown, you can use this function to handle some business logic, return true to allow countdown, otherwise return false
onNetworkFailedfunctionYesnoneinvoke when the network is failed, so the countdown timer will be invalid in this situation, maybe you will use it to show some message for users

Methods

MethodDescription
startCountdownstart countdown
stopCountdownstop countdown anytime you want, such as network failed or other situations