2015-10-11 20:07:09 +03:00
|
|
|
import React from 'react';
|
2016-05-21 19:57:49 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2016-05-28 18:44:29 +03:00
|
|
|
import { INPUT } from '../identifiers.js';
|
|
|
|
import InjectedFontIcon from '../font_icon/FontIcon.js';
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
const factory = (FontIcon) => {
|
|
|
|
class Input extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
children: React.PropTypes.any,
|
|
|
|
className: React.PropTypes.string,
|
|
|
|
disabled: React.PropTypes.bool,
|
2016-05-25 01:25:43 +03:00
|
|
|
error: React.PropTypes.string,
|
2016-05-28 18:44:29 +03:00
|
|
|
floating: React.PropTypes.bool,
|
2016-05-25 01:25:43 +03:00
|
|
|
hint: React.PropTypes.string,
|
2016-05-28 18:44:29 +03:00
|
|
|
icon: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.element
|
|
|
|
]),
|
|
|
|
label: React.PropTypes.string,
|
|
|
|
maxLength: React.PropTypes.number,
|
|
|
|
multiline: React.PropTypes.bool,
|
2016-06-09 20:21:22 +03:00
|
|
|
name: React.PropTypes.string,
|
2016-05-28 18:44:29 +03:00
|
|
|
onBlur: React.PropTypes.func,
|
|
|
|
onChange: React.PropTypes.func,
|
|
|
|
onFocus: React.PropTypes.func,
|
|
|
|
onKeyPress: React.PropTypes.func,
|
|
|
|
required: React.PropTypes.bool,
|
|
|
|
theme: React.PropTypes.shape({
|
|
|
|
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
|
|
|
|
}),
|
|
|
|
type: React.PropTypes.string,
|
|
|
|
value: React.PropTypes.any
|
|
|
|
};
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
hint: '',
|
|
|
|
disabled: false,
|
|
|
|
floating: true,
|
|
|
|
multiline: false,
|
|
|
|
required: false,
|
|
|
|
type: 'text'
|
|
|
|
};
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2016-06-24 04:10:24 +03:00
|
|
|
componentDidMount () {
|
2016-07-21 16:15:35 +03:00
|
|
|
if (this.props.multiline) {
|
|
|
|
window.addEventListener('resize', this.handleAutoresize);
|
2016-08-04 04:34:28 +03:00
|
|
|
this.handleAutoresize();
|
2016-07-21 16:15:35 +03:00
|
|
|
}
|
2016-06-24 04:10:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (!this.props.multiline && nextProps.multiline) {
|
|
|
|
window.addEventListener('resize', this.handleAutoresize);
|
|
|
|
} else if (this.props.multiline && !nextProps.multiline) {
|
|
|
|
window.removeEventListener('resize', this.handleAutoresize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 04:41:24 +03:00
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
const { multiline, value } = this.props;
|
|
|
|
|
|
|
|
// resize the textarea, if nessesary
|
|
|
|
if (multiline && (prevProps.value && prevProps.value.length) !== (value && value.length)) {
|
|
|
|
this.handleAutoresize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 04:10:24 +03:00
|
|
|
componentWillUnmount () {
|
2016-08-04 04:40:10 +03:00
|
|
|
if (this.props.multiline) window.removeEventListener('resize', this.handleAutoresize);
|
2016-06-24 04:10:24 +03:00
|
|
|
}
|
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
handleChange = (event) => {
|
2016-08-04 04:45:38 +03:00
|
|
|
const { onChange, multiline, maxLength } = this.props;
|
|
|
|
const valueFromEvent = event.target.value;
|
|
|
|
|
|
|
|
// trim value to maxLength if that exists (only on multiline inputs)
|
|
|
|
const haveToTrim = (multiline && maxLength && event.target.value.length > maxLength);
|
|
|
|
const value = haveToTrim ? valueFromEvent.substr(0, maxLength) : valueFromEvent;
|
|
|
|
|
|
|
|
// propagate to to store and therefore to the input
|
|
|
|
if (onChange) onChange(value, event);
|
2016-05-28 18:44:29 +03:00
|
|
|
};
|
2016-06-24 04:10:24 +03:00
|
|
|
|
|
|
|
handleAutoresize = () => {
|
|
|
|
const element = this.refs.input;
|
|
|
|
// compute the height difference between inner height and outer height
|
|
|
|
const style = getComputedStyle(element, null);
|
2016-06-25 13:26:20 +03:00
|
|
|
const heightOffset = style.boxSizing === 'content-box'
|
|
|
|
? -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom))
|
|
|
|
: parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
|
|
|
|
|
2016-06-24 04:10:24 +03:00
|
|
|
// resize the input to its content size
|
|
|
|
element.style.height = 'auto';
|
|
|
|
element.style.height = `${element.scrollHeight + heightOffset}px`;
|
|
|
|
}
|
2015-11-25 13:24:06 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
blur () {
|
|
|
|
this.refs.input.blur();
|
|
|
|
}
|
2015-11-22 21:20:05 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
focus () {
|
|
|
|
this.refs.input.focus();
|
|
|
|
}
|
2015-11-22 21:20:05 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
render () {
|
|
|
|
const { children, disabled, error, floating, hint, icon,
|
2016-06-06 20:37:51 +03:00
|
|
|
name, label: labelText, maxLength, multiline, required,
|
2016-05-28 18:44:29 +03:00
|
|
|
theme, type, value, ...others} = this.props;
|
|
|
|
const length = maxLength && value ? value.length : 0;
|
|
|
|
const labelClassName = classnames(theme.label, {[theme.fixed]: !floating});
|
2015-11-29 14:39:55 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
const className = classnames(theme.input, {
|
|
|
|
[theme.disabled]: disabled,
|
|
|
|
[theme.errored]: error,
|
|
|
|
[theme.hidden]: type === 'hidden',
|
|
|
|
[theme.withIcon]: icon
|
|
|
|
}, this.props.className);
|
2015-09-20 23:21:11 +03:00
|
|
|
|
2016-07-10 14:42:35 +03:00
|
|
|
const valuePresent = value !== null
|
|
|
|
&& value !== undefined
|
|
|
|
&& value !== ''
|
|
|
|
&& !(typeof value === Number && isNaN(value));
|
2016-03-06 15:56:35 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
const InputElement = React.createElement(multiline ? 'textarea' : 'input', {
|
|
|
|
...others,
|
|
|
|
className: classnames(theme.inputElement, {[theme.filled]: valuePresent}),
|
|
|
|
onChange: this.handleChange,
|
|
|
|
ref: 'input',
|
|
|
|
role: 'input',
|
2016-06-06 20:37:51 +03:00
|
|
|
name,
|
2016-05-28 18:44:29 +03:00
|
|
|
disabled,
|
|
|
|
required,
|
|
|
|
type,
|
2016-08-04 04:45:38 +03:00
|
|
|
value
|
2016-05-28 18:44:29 +03:00
|
|
|
});
|
2015-11-29 14:39:55 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
return (
|
|
|
|
<div data-react-toolbox='input' className={className}>
|
|
|
|
{InputElement}
|
|
|
|
{icon ? <FontIcon className={theme.icon} value={icon} /> : null}
|
|
|
|
<span className={theme.bar}></span>
|
|
|
|
{labelText
|
|
|
|
? <label className={labelClassName}>
|
|
|
|
{labelText}
|
|
|
|
{required ? <span className={theme.required}> * </span> : null}
|
|
|
|
</label>
|
|
|
|
: null}
|
|
|
|
{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}
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-28 18:44:29 +03:00
|
|
|
return Input;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Input = factory(InjectedFontIcon);
|
2016-08-02 22:51:13 +03:00
|
|
|
export default themr(INPUT, null, { withRef: true })(Input);
|
2016-05-28 18:44:29 +03:00
|
|
|
export { factory as inputFactory };
|
2016-05-25 01:25:43 +03:00
|
|
|
export { Input };
|