react-toolbox/components/input/Input.js

117 lines
3.4 KiB
JavaScript
Raw Normal View History

import React from 'react';
2016-05-21 19:57:49 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2015-10-12 11:10:49 +03:00
import FontIcon from '../font_icon';
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,
2016-05-21 19:57:49 +03:00
theme: React.PropTypes.shape({
2016-05-25 01:25:43 +03:00
bar: React.PropTypes.string,
counter: React.PropTypes.string,
disabled: React.PropTypes.string,
error: React.PropTypes.string,
errored: React.PropTypes.string,
hidden: React.PropTypes.string,
hint: React.PropTypes.string,
icon: React.PropTypes.string,
input: React.PropTypes.string,
inputElement: React.PropTypes.string,
required: React.PropTypes.string,
withIcon: React.PropTypes.string
2016-05-21 19:57:49 +03:00
}),
2015-09-20 23:21:11 +03:00
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,
2016-05-21 19:57:49 +03:00
theme, type, value, ...others} = this.props;
2015-11-29 03:25:21 +03:00
const length = maxLength && value ? value.length : 0;
2016-05-21 19:57:49 +03:00
const labelClassName = classnames(theme.label, {[theme.fixed]: !floating});
2015-11-29 14:39:55 +03:00
2016-05-21 19:57:49 +03:00
const className = classnames(theme.input, {
[theme.disabled]: disabled,
[theme.errored]: error,
[theme.hidden]: type === 'hidden',
[theme.withIcon]: icon
2015-11-29 03:25:21 +03:00
}, 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,
2016-05-21 19:57:49 +03:00
className: classnames(theme.inputElement, {[theme.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}
2016-05-21 19:57:49 +03:00
{icon ? <FontIcon className={theme.icon} value={icon} /> : null}
<span className={theme.bar}></span>
2016-04-10 18:49:23 +03:00
{labelText
? <label className={labelClassName}>
{labelText}
2016-05-21 19:57:49 +03:00
{required ? <span className={theme.required}> * </span> : null}
2016-04-10 18:49:23 +03:00
</label>
: null}
2016-05-21 19:57:49 +03:00
{hint ? <span className={theme.hint}>{hint}</span> : null}
{error ? <span className={theme.error}>{error}</span> : null}
{maxLength ? <span className={theme.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
2016-05-21 19:57:49 +03:00
export default themr('ToolboxInput')(Input);
2016-05-25 01:25:43 +03:00
export { Input };