index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, value, 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} value={value} 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. constructor(props) {
  23. super(props);
  24. this.state = { showSelect: false };
  25. }
  26. render() {
  27. const { showSelect } = this.state;
  28. const { className = '', onChange, placeholder, value, error, selectValue, select, onSelect } = this.props;
  29. return (
  30. <Input
  31. className={className}
  32. left={
  33. <span className="g-input-left-select" onClick={() => this.setState({ showSelect: !showSelect })}>
  34. {selectValue}
  35. <Icon type={showSelect ? 'up' : 'down'} />
  36. {showSelect && <ul className="select-list">{select.map((row) => {
  37. return <li onClick={() => {
  38. this.setState({ showSelect: false });
  39. if (onSelect) onSelect(row.value);
  40. }}>{row.label}</li>;
  41. })}</ul>}
  42. </span>
  43. }
  44. value={value}
  45. placeholder={placeholder}
  46. onChange={data => onChange && onChange(data)}
  47. error={error}
  48. />
  49. );
  50. }
  51. }
  52. export class VerificationInput extends Component {
  53. constructor(props) {
  54. super(props);
  55. this.timeKey = null;
  56. this.state = { loading: 0 };
  57. }
  58. componentWillUnmount() {
  59. if (this.timeKey) clearTimeout(this.timeKey);
  60. }
  61. onSend() {
  62. const { onSend, time = 60 } = this.props;
  63. if (onSend) {
  64. onSend()
  65. .then(() => {
  66. console.log(time);
  67. this.setTime(time);
  68. });
  69. }
  70. }
  71. setTime(time) {
  72. this.setState({ loading: time });
  73. this.timeKey = setTimeout(() => {
  74. this.setTime(time - 1);
  75. }, 1000);
  76. }
  77. render() {
  78. const { loading } = this.state;
  79. const { className = '', onChange, placeholder, value } = this.props;
  80. return (
  81. <Input
  82. className={className}
  83. right={
  84. loading <= 0 ? (
  85. <span className="g-input-right-verification" onClick={() => this.onSend()}>
  86. 获取验证码
  87. </span>
  88. ) : (
  89. <span className="g-input-right-verification-loading">等待{loading}秒</span>
  90. )
  91. }
  92. value={value}
  93. placeholder={placeholder}
  94. onChange={data => onChange && onChange(data)}
  95. />
  96. );
  97. }
  98. }