react-toolbox/components/input/index.jsx

109 lines
2.7 KiB
React
Raw Normal View History

import React from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
2015-10-09 20:48:30 +03:00
import style from './style.scss';
2015-09-20 23:21:11 +03:00
export default React.createClass({
mixins: [PureRenderMixin],
2015-09-20 23:21:11 +03:00
displayName: 'Input',
propTypes: {
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-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
},
getDefaultProps () {
return {
className: '',
disabled: false,
2015-10-09 20:48:30 +03:00
floating: true,
2015-09-20 23:21:11 +03:00
multiline: false,
required: false,
type: 'text'
};
},
getInitialState () {
2015-10-09 20:48:30 +03:00
return { value: this.props.value };
2015-09-20 23:21:11 +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-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-09-20 23:21:11 +03:00
onChange={this.onChange}
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-09 20:48:30 +03:00
onChange={this.onChange} />
2015-09-20 23:21:11 +03:00
);
}
},
render () {
2015-10-09 20:48:30 +03:00
let className = style.root;
let labelClassName = style.label;
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}`;
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-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>
);
},
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: value});
2015-09-20 23:21:11 +03:00
}
});