react-toolbox/components/input/index.jsx

108 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';
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,
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
state = {
value: this.props.value
};
2015-09-20 23:21:11 +03:00
2015-10-22 02:31:17 +03:00
onChange = (event) => {
2015-10-10 12:25:47 +03:00
this.setState({value: event.target.value}, () => {
2015-10-09 20:48:30 +03:00
if (this.props.onChange) this.props.onChange(event, this);
});
2015-10-22 02:31:17 +03:00
};
2015-09-20 23:21:11 +03:00
renderInput () {
2015-10-09 20:48:30 +03:00
let className = style.input;
if (this.state.value && this.state.value.length > 0) className += ` ${style.filled}`;
2015-09-20 23:21:11 +03:00
if (this.props.multiline) {
return (
<textarea
ref='input'
role='input'
2015-09-20 23:21:11 +03:00
{...this.props}
2015-10-09 20:48:30 +03:00
className={className}
2015-10-21 09:13:24 +03:00
onChange={this.onChange}
2015-09-20 23:21:11 +03:00
value={this.state.value} />
);
} else {
return (
<input
ref='input'
role='input'
2015-09-20 23:21:11 +03:00
{...this.props}
2015-10-09 20:48:30 +03:00
className={className}
2015-09-20 23:21:11 +03:00
value={this.state.value}
2015-10-21 09:13:24 +03:00
onChange={this.onChange} />
2015-09-20 23:21:11 +03:00
);
}
}
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 }
{ this.props.error ? <span className={style.error}>{this.props.error}</span> : 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-09-20 23:21:11 +03:00
getValue () {
2015-10-09 20:48:30 +03:00
return this.state.value;
}
2015-09-20 23:21:11 +03:00
2015-10-09 20:48:30 +03:00
setValue (value) {
this.setState({value});
2015-09-20 23:21:11 +03:00
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Input;