index.js 3.1 KB

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