123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import React, { Component } from 'react';
- import './index.less';
- import Icon from '../Icon';
- export default class Input extends Component {
- render() {
- const { className = '', onChange, placeholder, value, error, left, right, empty } = this.props;
- return (
- <div className={`g-input-container ${className}`}>
- <div className={`g-input-wrapper ${error ? 'error' : ''}`}>
- {left && <div className="g-input-left">{left}</div>}
- <input
- className={`g-input ${empty ? 'empty' : ''}`}
- placeholder={placeholder}
- value={value}
- onChange={data => onChange && onChange(data)} />
- {right && <div className="g-input-right">{right}</div>}
- </div>
- <div hidden={!error} className="g-input-error">
- {error}
- </div>
- </div>
- );
- }
- }
- export class SelectInput extends Component {
- constructor(props) {
- super(props);
- this.state = { showSelect: false };
- }
- render() {
- const { showSelect } = this.state;
- const { className = '', onChange, placeholder, value, error, selectValue, select, onSelect, empty } = this.props;
- return (
- <Input
- className={className}
- left={
- <span className="g-input-left-select" onClick={() => this.setState({ showSelect: !showSelect })}>
- {selectValue}
- <Icon type={showSelect ? 'up' : 'down'} />
- {showSelect && <ul className="select-list">{select.map((row) => {
- return <li onClick={() => {
- this.setState({ showSelect: false });
- if (onSelect) onSelect(row.value);
- }}>{row.label}</li>;
- })}</ul>}
- </span>
- }
- value={value}
- placeholder={placeholder}
- onChange={data => onChange && onChange(data)}
- error={error}
- empty={empty}
- />
- );
- }
- }
- export class VerificationInput extends Component {
- constructor(props) {
- super(props);
- this.timeKey = null;
- this.state = { loading: 0 };
- }
- componentWillUnmount() {
- if (this.timeKey) clearTimeout(this.timeKey);
- }
- onSend() {
- const { onSend, time = 60 } = this.props;
- if (onSend) {
- onSend()
- .then(() => {
- if (this.timeKey) clearTimeout(this.timeKey);
- this.setTime(time);
- });
- }
- }
- setTime(time) {
- this.setState({ loading: time });
- this.timeKey = setTimeout(() => {
- this.setTime(time - 1);
- }, 1000);
- }
- render() {
- const { loading } = this.state;
- const { className = '', onChange, placeholder, value, error, empty } = this.props;
- return (
- <Input
- className={className}
- right={
- loading <= 0 ? (
- <span className="g-input-right-verification" onClick={() => this.onSend()}>
- 获取验证码
- </span>
- ) : (<span className="g-input-right-verification-loading">等待{loading}秒</span>)
- }
- value={value}
- error={error}
- empty={empty}
- placeholder={placeholder}
- onChange={data => onChange && onChange(data)}
- />
- );
- }
- }
|