1
0

index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Icon from '../Icon';
  4. export default class Input extends Component {
  5. render() {
  6. const { className = '', onChange, placeholder, error, left, right } = this.props;
  7. return (
  8. <div className={`g-input-container ${className}`}>
  9. <div className={`g-input-wrapper ${error ? 'error' : ''}`}>
  10. {left && <div className="g-input-left">{left}</div>}
  11. <input className="g-input" placeholder={placeholder} onChange={data => onChange && onChange(data)} />
  12. {right && <div className="g-input-right">{right}</div>}
  13. </div>
  14. <div hidden={!error} className="g-input-error">
  15. {error}
  16. </div>
  17. </div>
  18. );
  19. }
  20. }
  21. export class SelectInput extends Component {
  22. render() {
  23. const { className = '', onChange, placeholder, value, selectValue, onSelect } = this.props;
  24. return (
  25. <Input
  26. className={className}
  27. left={
  28. <span className="g-input-left-select" onClick={() => onSelect && onSelect()}>
  29. {selectValue}
  30. <Icon type="down" />
  31. </span>
  32. }
  33. value={value}
  34. placeholder={placeholder}
  35. onChange={data => onChange && onChange(data)}
  36. />
  37. );
  38. }
  39. }
  40. export class VerificationInput extends Component {
  41. constructor(props) {
  42. super(props);
  43. this.timeKey = null;
  44. this.state = { loading: 0 };
  45. }
  46. componentWillUnmount() {
  47. if (this.timeKey) clearTimeout(this.timeKey);
  48. }
  49. onSend() {
  50. const { onSend, time = 60 } = this.props;
  51. if (onSend) {
  52. onSend();
  53. }
  54. this.setTime(time);
  55. }
  56. setTime(time) {
  57. this.setState({ loading: time });
  58. this.timeKey = setTimeout(() => {
  59. this.setTime(time - 1);
  60. }, 1000);
  61. }
  62. render() {
  63. const { loading } = this.state;
  64. const { className = '', onChange, placeholder, value } = this.props;
  65. return (
  66. <Input
  67. className={className}
  68. right={
  69. loading <= 0 ? (
  70. <span className="g-input-right-verification" onClick={() => this.onSend()}>
  71. 获取验证码
  72. </span>
  73. ) : (
  74. <span className="g-input-right-verification-loading">等待{loading}秒</span>
  75. )
  76. }
  77. value={value}
  78. placeholder={placeholder}
  79. onChange={data => onChange && onChange(data)}
  80. />
  81. );
  82. }
  83. }