react-toolbox/components/input/index.jsx

86 lines
2.7 KiB
React
Raw Normal View History

import React from 'react';
2015-10-23 02:26:12 +03:00
import style from './style';
2015-10-12 11:10:49 +03:00
import FontIcon from '../font_icon';
import Tooltip from '../tooltip';
2015-09-20 23:21:11 +03:00
2015-10-22 02:31:17 +03:00
class Input extends React.Component {
static propTypes = {
2015-09-20 23:21:11 +03:00
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
error: React.PropTypes.string,
2015-10-09 20:48:30 +03:00
floating: React.PropTypes.bool,
2015-10-12 11:10:49 +03:00
icon: React.PropTypes.string,
2015-09-20 23:21:11 +03:00
label: React.PropTypes.string,
2015-11-18 11:19:58 +03:00
maxLength: React.PropTypes.number,
2015-09-20 23:21:11 +03:00
multiline: React.PropTypes.bool,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onFocus: React.PropTypes.func,
onKeyPress: React.PropTypes.func,
required: React.PropTypes.bool,
type: React.PropTypes.string,
value: React.PropTypes.any
};
2015-09-20 23:21:11 +03:00
static defaultProps = {
className: '',
disabled: false,
floating: true,
multiline: false,
required: false,
type: 'text'
};
2015-09-20 23:21:11 +03:00
renderInput () {
2015-10-09 20:48:30 +03:00
let className = style.input;
2015-11-08 01:38:26 +03:00
if (this.props.value && this.props.value.length > 0) className += ` ${style.filled}`;
2015-09-20 23:21:11 +03:00
if (this.props.multiline) {
2015-11-08 01:38:26 +03:00
return <textarea ref='input' role='input' {...this.props} className={className} />;
2015-09-20 23:21:11 +03:00
} else {
2015-11-08 01:38:26 +03:00
return <input ref='input' role='input' {...this.props} className={className} />;
2015-09-20 23:21:11 +03:00
}
}
2015-09-20 23:21:11 +03:00
2015-11-18 11:19:58 +03:00
renderUnderline () {
const error = this.props.error ? <span className={style.error}>{this.props.error}</span> : null;
let counter = null;
if (this.props.maxLength) {
const length = this.props.value ? this.props.value.length : 0;
if (length > 0) counter = <span className={style.counter}>{length} / {this.props.maxLength}</span>;
}
if (error || counter) return <span className={style.underline}>{error}{counter}</span>;
}
2015-09-20 23:21:11 +03:00
render () {
2015-10-09 20:48:30 +03:00
let className = style.root;
2015-10-12 13:53:42 +03:00
let labelClassName = style.label;
2015-10-09 20:48:30 +03:00
if (this.props.error) className += ` ${style.errored}`;
if (this.props.disabled) className += ` ${style.disabled}`;
if (this.props.className) className += ` ${this.props.className}`;
if (this.props.type === 'hidden') className += ` ${style.hidden}`;
2015-10-12 13:53:42 +03:00
if (this.props.icon) className += ` ${style['with-icon']}`;
2015-10-09 20:48:30 +03:00
if (!this.props.floating) labelClassName += ` ${style.fixed}`;
2015-09-20 23:21:11 +03:00
return (
<div data-react-toolbox='input' className={className}>
{ this.renderInput() }
2015-10-12 11:10:49 +03:00
{ this.props.icon ? <FontIcon className={style.icon} value={this.props.icon} /> : null }
2015-10-09 20:48:30 +03:00
<span className={style.bar}></span>
{ this.props.label ? <label className={labelClassName}>{this.props.label}</label> : null }
2015-11-18 11:19:58 +03:00
{ this.renderUnderline() }
{ this.props.tooltip ? <Tooltip label={this.props.tooltip}/> : null }
2015-09-20 23:21:11 +03:00
</div>
);
}
2015-09-20 23:21:11 +03:00
blur () {
this.refs.input.blur();
}
2015-09-20 23:21:11 +03:00
focus () {
this.refs.input.focus();
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Input;