2015-10-11 20:07:09 +03:00
|
|
|
import React from 'react';
|
2015-11-29 03:25:21 +03:00
|
|
|
import ClassNames from 'classnames';
|
2015-10-12 11:10:49 +03:00
|
|
|
import FontIcon from '../font_icon';
|
2015-11-29 03:25:21 +03:00
|
|
|
import style from './style';
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
class Input extends React.Component {
|
2015-10-20 08:40:51 +03:00
|
|
|
static propTypes = {
|
2015-11-29 14:39:55 +03:00
|
|
|
children: React.PropTypes.any,
|
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-10-20 08:40:51 +03:00
|
|
|
};
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
disabled: false,
|
|
|
|
floating: true,
|
|
|
|
multiline: false,
|
|
|
|
required: false,
|
|
|
|
type: 'text'
|
|
|
|
};
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2015-11-25 13:24:06 +03:00
|
|
|
handleChange = (event) => {
|
|
|
|
if (this.props.onChange) this.props.onChange(event.target.value, event);
|
|
|
|
};
|
|
|
|
|
2015-09-20 23:21:11 +03:00
|
|
|
renderInput () {
|
2015-11-29 03:25:21 +03:00
|
|
|
const {multiline, value, ...others} = this.props;
|
|
|
|
const className = ClassNames(style.input, {[style.filled]: value});
|
2015-11-25 13:24:06 +03:00
|
|
|
|
2015-11-29 03:25:21 +03:00
|
|
|
return React.createElement(multiline ? 'textarea' : 'input', {
|
|
|
|
...others,
|
2015-11-25 13:24:06 +03:00
|
|
|
className,
|
|
|
|
onChange: this.handleChange,
|
|
|
|
ref: 'input',
|
2015-11-29 03:25:21 +03:00
|
|
|
role: 'input',
|
|
|
|
value
|
2015-11-25 13:24:06 +03:00
|
|
|
});
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2015-11-22 21:20:05 +03:00
|
|
|
blur () {
|
|
|
|
this.refs.input.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
focus () {
|
|
|
|
this.refs.input.focus();
|
|
|
|
}
|
|
|
|
|
2015-09-20 23:21:11 +03:00
|
|
|
render () {
|
2015-11-29 14:39:55 +03:00
|
|
|
const { children, disabled, error, floating, icon,
|
|
|
|
label: labelText, maxLength, multiline, type, value, ...others} = this.props;
|
2015-11-29 03:25:21 +03:00
|
|
|
const length = maxLength && value ? value.length : 0;
|
|
|
|
const labelClassName = ClassNames(style.label, {[style.fixed]: !floating});
|
2015-11-29 14:39:55 +03:00
|
|
|
|
2015-11-29 03:25:21 +03:00
|
|
|
const className = ClassNames(style.root, {
|
|
|
|
[style.disabled]: disabled,
|
|
|
|
[style.errored]: error,
|
|
|
|
[style.hidden]: type === 'hidden',
|
|
|
|
[style.withIcon]: icon
|
|
|
|
}, this.props.className);
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2015-11-29 14:39:55 +03:00
|
|
|
const InputElement = React.createElement(multiline ? 'textarea' : 'input', {
|
|
|
|
...others,
|
|
|
|
className: ClassNames(style.input, {[style.filled]: value}),
|
|
|
|
onChange: this.handleChange,
|
|
|
|
ref: 'input',
|
|
|
|
role: 'input',
|
|
|
|
value
|
|
|
|
});
|
|
|
|
|
2015-09-20 23:21:11 +03:00
|
|
|
return (
|
|
|
|
<div data-react-toolbox='input' className={className}>
|
2015-11-29 14:39:55 +03:00
|
|
|
{InputElement}
|
2015-11-29 03:25:21 +03:00
|
|
|
{icon ? <FontIcon className={style.icon} value={icon} /> : null}
|
2015-10-09 20:48:30 +03:00
|
|
|
<span className={style.bar}></span>
|
2015-11-29 03:25:21 +03:00
|
|
|
{labelText ? <label className={labelClassName}>{labelText}</label> : null}
|
|
|
|
{error ? <span className={style.error}>{error}</span> : null}
|
|
|
|
{maxLength ? <span className={style.counter}>{length}/{maxLength}</span> : null}
|
2015-11-29 14:39:55 +03:00
|
|
|
{children}
|
2015-09-20 23:21:11 +03:00
|
|
|
</div>
|
|
|
|
);
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-21 13:25:07 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
|
|
|
export default Input;
|