react-toolbox/components/input/Input.jsx

89 lines
2.5 KiB
React
Raw Normal View History

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 {
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,
2016-02-24 01:55:27 +03:00
error: React.PropTypes.node,
2015-10-09 20:48:30 +03:00
floating: React.PropTypes.bool,
2016-01-20 01:14:17 +03:00
icon: React.PropTypes.any,
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
handleChange = (event) => {
if (this.props.onChange) this.props.onChange(event.target.value, event);
};
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
const valuePresent = value !== null && value !== undefined && value !== '' && !Number.isNaN(value);
2015-11-29 14:39:55 +03:00
const InputElement = React.createElement(multiline ? 'textarea' : 'input', {
...others,
className: ClassNames(style.input, {[style.filled]: valuePresent}),
2015-11-29 14:39:55 +03:00
onChange: this.handleChange,
ref: 'input',
role: 'input',
2015-12-03 02:48:32 +03:00
disabled,
2015-12-05 12:49:29 +03:00
type,
value,
maxLength
2015-11-29 14:39:55 +03:00
});
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-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Input;