2019-01-07 01:25:33 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2016-11-03 19:40:54 +00:00
|
|
|
import classnames from 'classnames';
|
2016-10-19 20:22:18 +00:00
|
|
|
|
2016-12-22 19:26:18 +00:00
|
|
|
import Icon from 'components/icons/Icon';
|
2016-09-30 18:55:15 +00:00
|
|
|
import InputField from '../InputField';
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
const baseClass = 'input-icon-field';
|
|
|
|
|
|
2016-09-30 18:55:15 +00:00
|
|
|
class InputFieldWithIcon extends InputField {
|
2016-09-12 15:14:07 +00:00
|
|
|
static propTypes = {
|
2016-09-19 18:33:43 +00:00
|
|
|
autofocus: PropTypes.bool,
|
2016-09-14 20:31:54 +00:00
|
|
|
error: PropTypes.string,
|
2016-12-01 18:57:19 +00:00
|
|
|
hint: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
|
2016-09-12 15:14:07 +00:00
|
|
|
iconName: PropTypes.string,
|
|
|
|
|
name: PropTypes.string,
|
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
|
placeholder: PropTypes.string,
|
2016-12-01 18:57:19 +00:00
|
|
|
tabIndex: PropTypes.number,
|
2016-09-12 15:14:07 +00:00
|
|
|
type: PropTypes.string,
|
2016-11-29 22:29:14 +00:00
|
|
|
className: PropTypes.string,
|
2016-09-12 15:14:07 +00:00
|
|
|
};
|
|
|
|
|
|
2016-09-14 20:31:54 +00:00
|
|
|
renderHeading = () => {
|
2016-11-07 16:42:39 +00:00
|
|
|
const { error, placeholder, value } = this.props;
|
2016-11-03 19:40:54 +00:00
|
|
|
|
|
|
|
|
const labelClasses = classnames(
|
|
|
|
|
`${baseClass}__label`,
|
|
|
|
|
{ [`${baseClass}__label--hidden`]: !value }
|
|
|
|
|
);
|
2016-09-14 20:31:54 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
2016-11-03 19:40:54 +00:00
|
|
|
return <div className={`${baseClass}__errors`}>{error}</div>;
|
2016-09-14 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
return <div className={labelClasses}>{placeholder}</div>;
|
2016-09-14 20:31:54 +00:00
|
|
|
}
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2016-12-01 18:57:19 +00:00
|
|
|
renderHint = () => {
|
|
|
|
|
const { hint } = this.props;
|
|
|
|
|
|
|
|
|
|
if (hint) {
|
|
|
|
|
return <span className={`${baseClass}__hint`}>{hint}</span>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 15:14:07 +00:00
|
|
|
render () {
|
2016-12-01 18:57:19 +00:00
|
|
|
const { className, error, iconName, name, placeholder, tabIndex, type, value } = this.props;
|
|
|
|
|
const { onInputChange, renderHint } = this;
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
const inputClasses = classnames(
|
|
|
|
|
`${baseClass}__input`,
|
|
|
|
|
'input-with-icon',
|
2016-11-29 22:29:14 +00:00
|
|
|
className,
|
2016-11-03 19:40:54 +00:00
|
|
|
{ [`${baseClass}__input--error`]: error },
|
2016-11-08 14:36:05 +00:00
|
|
|
{ [`${baseClass}__input--password`]: type === 'password' && value }
|
2016-11-03 19:40:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const iconClasses = classnames(
|
|
|
|
|
`${baseClass}__icon`,
|
|
|
|
|
{ [`${baseClass}__icon--error`]: error },
|
|
|
|
|
{ [`${baseClass}__icon--active`]: value }
|
|
|
|
|
);
|
|
|
|
|
|
2016-09-12 15:14:07 +00:00
|
|
|
return (
|
2016-11-03 19:40:54 +00:00
|
|
|
<div className={baseClass}>
|
2016-09-14 20:31:54 +00:00
|
|
|
{this.renderHeading()}
|
2016-09-12 15:14:07 +00:00
|
|
|
<input
|
|
|
|
|
name={name}
|
|
|
|
|
onChange={onInputChange}
|
2016-11-03 19:40:54 +00:00
|
|
|
className={inputClasses}
|
2016-09-12 15:14:07 +00:00
|
|
|
placeholder={placeholder}
|
2016-09-19 18:33:43 +00:00
|
|
|
ref={(r) => { this.input = r; }}
|
2016-12-01 18:57:19 +00:00
|
|
|
tabIndex={tabIndex}
|
2016-09-12 15:14:07 +00:00
|
|
|
type={type}
|
2016-10-03 17:54:22 +00:00
|
|
|
value={value}
|
2016-09-12 15:14:07 +00:00
|
|
|
/>
|
2016-11-28 19:20:15 +00:00
|
|
|
{iconName && <Icon name={iconName} className={iconClasses} />}
|
2016-12-01 18:57:19 +00:00
|
|
|
{renderHint()}
|
2016-09-12 15:14:07 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
export default InputFieldWithIcon;
|