index.js 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Input, Spin } from 'antd';
import { getCert, init, sign } from '../../utils/uca';

class UCA extends Component {

  constructor(props, context) {
    super(props, context);
    this.state = {
      ready: false,
    };
  }

  componentDidMount() {
    this.task = setInterval(() => {
      const res = init();
      if (res === 0) {
        clearInterval(this.task);
        this.task = null;
        this.setState({
          ready: true,
        });
      }
    }, 300);
  }

  componentWillUnmount() {
    if (this.task) {
      clearInterval(this.task);
    }
  }

  onChange = (value) => {
    if (this.state.ready && this.props.onChange) {
36 37 38 39 40 41 42 43 44 45 46 47 48 49
      this.props.onChange(this.valueToObj(value));
    }
  };
  objToValue = (obj) => {
    return obj ? obj.input : '';
  };
  valueToObj = (value) => {
    if (!value) {
      return value;
    }
    if (this.props.data) {
      return sign(value, this.props.data);
    } else {
      return getCert(value);
50 51 52 53
    }
  };
  render() {
    const onChange = (e) => {
54
      return this.onChange(e.target.value);
55
    };
56 57
    const { loading, value, style, ...rest } = this.props;
    const { width, height } = style || {};
58
    return (
59 60 61 62 63
      <div style={{ width, height }}>
        <Spin spinning={!this.state.ready || loading} size="small">
          <Input {...rest} disabled={!this.state.ready || loading} value={this.objToValue(value)} onChange={onChange} type="password" />
        </Spin>
      </div>
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    );
  }
}

UCA.propTypes = {
  loading: PropTypes.bool,
  data: PropTypes.string,
};

UCA.defaultProps = {
  loading: false,
};

export default UCA;