1
0

index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import { Icon } from 'antd';
  4. export function Input(props) {
  5. const { className = '', value, onChange, placeholder, empty } = props;
  6. return <input className={`${className} ${empty ? 'empty' : ''}`} value={value} placeholder={placeholder} onChange={data => onChange && onChange(data)} />;
  7. }
  8. export function Textarea(props) {
  9. const { className = '', value, onChange, placeholder, empty } = props;
  10. return <textarea className={`${className} ${empty ? 'empty' : ''}`} value={value} placeholder={placeholder} onChange={data => onChange && onChange(data)} />;
  11. }
  12. export class DefaultInput extends Component {
  13. render() {
  14. const { className = '', onChange, placeholder, value, error, left, right, empty } = this.props;
  15. return (
  16. <div className={`g-input-container ${className}`}>
  17. <div className={`g-input-wrapper ${error ? 'error' : ''}`}>
  18. {left && <div className="g-input-left">{left}</div>}
  19. <Input
  20. className="g-input"
  21. placeholder={placeholder}
  22. value={value}
  23. empty={empty}
  24. onChange={data => onChange && onChange(data)}
  25. />
  26. {right && <div className="g-input-right">{right}</div>}
  27. </div>
  28. <div hidden={!error} className="g-input-error">
  29. {error}
  30. </div>
  31. </div>
  32. );
  33. }
  34. }
  35. export class SelectInput extends Component {
  36. constructor(props) {
  37. super(props);
  38. this.state = { showSelect: false };
  39. }
  40. render() {
  41. const { showSelect } = this.state;
  42. const { className = '', onChange, placeholder, value, error, empty, selectValue, select, onSelect } = this.props;
  43. return (
  44. <DefaultInput
  45. className={className}
  46. left={
  47. <span className="g-input-left-select" onClick={() => this.setState({ showSelect: !showSelect })}>
  48. {selectValue}
  49. <Icon type={showSelect ? 'up' : 'down'} />
  50. {showSelect && (
  51. <ul className="select-list">
  52. {select.map(row => {
  53. return (
  54. <li
  55. onClick={() => {
  56. this.setState({ showSelect: false });
  57. if (onSelect) onSelect(row.value);
  58. }}
  59. >
  60. {row.label}
  61. </li>
  62. );
  63. })}
  64. </ul>
  65. )}
  66. </span>
  67. }
  68. value={value}
  69. placeholder={placeholder}
  70. onChange={data => onChange && onChange(data)}
  71. error={error}
  72. empty={empty}
  73. />
  74. );
  75. }
  76. }
  77. export class VerificationInput extends Component {
  78. constructor(props) {
  79. super(props);
  80. this.timeKey = null;
  81. this.state = { loading: 0 };
  82. }
  83. componentWillUnmount() {
  84. if (this.timeKey) clearTimeout(this.timeKey);
  85. }
  86. onSend() {
  87. const { onSend, time = 60 } = this.props;
  88. if (onSend) {
  89. onSend().then(() => {
  90. if (this.timeKey) clearTimeout(this.timeKey);
  91. this.setTime(time);
  92. });
  93. }
  94. }
  95. setTime(time) {
  96. this.setState({ loading: time });
  97. this.timeKey = setTimeout(() => {
  98. this.setTime(time - 1);
  99. }, 1000);
  100. }
  101. render() {
  102. const { loading } = this.state;
  103. const { className = '', onChange, placeholder, value, error, empty } = this.props;
  104. return (
  105. <DefaultInput
  106. className={className}
  107. right={
  108. loading <= 0 ? (
  109. <span className="g-input-right-verification" onClick={() => this.onSend()}>
  110. 获取验证码
  111. </span>
  112. ) : (<span className="g-input-right-verification-loading">等待{loading}秒</span>)
  113. }
  114. value={value}
  115. error={error}
  116. empty={empty}
  117. placeholder={placeholder}
  118. onChange={data => onChange && onChange(data)}
  119. />
  120. );
  121. }
  122. }