react-toolbox/components/input/Input.js

102 lines
2.9 KiB
JavaScript
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,
error: React.PropTypes.string,
2015-10-09 20:48:30 +03:00
floating: React.PropTypes.bool,
2016-03-10 07:23:35 +03:00
hint: React.PropTypes.string,
icon: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.element
]),
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: '',
2016-03-10 07:23:35 +03:00
hint: '',
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 () {
2016-03-10 07:23:35 +03:00
const { children, disabled, error, floating, hint, icon,
2016-03-13 03:05:19 +03:00
label: labelText, maxLength, multiline, required,
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,
required,
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>
2016-04-10 18:49:23 +03:00
{labelText
? <label className={labelClassName}>
{labelText}
{required ? <span className={style.required}> * </span> : null}
</label>
: null}
{hint ? <span className={style.hint}>{hint}</span> : null}
2015-11-29 03:25:21 +03:00
{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;