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,
|
2016-04-07 04:24:31 +03:00
|
|
|
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,
|
2016-04-07 04:24:31 +03:00
|
|
|
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-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: '',
|
2016-03-10 07:23:35 +03:00
|
|
|
hint: '',
|
2015-10-20 08:40:51 +03:00
|
|
|
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-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 () {
|
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
|
|
|
|
2016-03-06 15:56:35 +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,
|
2016-03-06 15:56:35 +03:00
|
|
|
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,
|
2016-03-21 00:35:40 +03:00
|
|
|
required,
|
2015-12-05 12:49:29 +03:00
|
|
|
type,
|
2015-12-17 01:25:01 +03:00
|
|
|
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}
|
2016-03-10 16:45:51 +03:00
|
|
|
{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-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;
|