fleet/frontend/components/forms/fields/InputFieldWithIcon/InputFieldWithIcon.jsx

88 lines
2.2 KiB
React
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Icon from 'components/icons/Icon';
import InputField from '../InputField';
const baseClass = 'input-icon-field';
class InputFieldWithIcon extends InputField {
static propTypes = {
2016-09-19 18:33:43 +00:00
autofocus: PropTypes.bool,
error: PropTypes.string,
2016-12-01 18:57:19 +00:00
hint: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
iconName: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func,
placeholder: PropTypes.string,
2016-12-01 18:57:19 +00:00
tabIndex: PropTypes.number,
type: PropTypes.string,
className: PropTypes.string,
};
renderHeading = () => {
const { error, placeholder, value } = this.props;
const labelClasses = classnames(
`${baseClass}__label`,
{ [`${baseClass}__label--hidden`]: !value }
);
if (error) {
return <div className={`${baseClass}__errors`}>{error}</div>;
}
return <div className={labelClasses}>{placeholder}</div>;
}
2016-12-01 18:57:19 +00:00
renderHint = () => {
const { hint } = this.props;
if (hint) {
return <span className={`${baseClass}__hint`}>{hint}</span>;
}
return false;
}
render () {
2016-12-01 18:57:19 +00:00
const { className, error, iconName, name, placeholder, tabIndex, type, value } = this.props;
const { onInputChange, renderHint } = this;
const inputClasses = classnames(
`${baseClass}__input`,
'input-with-icon',
className,
{ [`${baseClass}__input--error`]: error },
{ [`${baseClass}__input--password`]: type === 'password' && value }
);
const iconClasses = classnames(
`${baseClass}__icon`,
{ [`${baseClass}__icon--error`]: error },
{ [`${baseClass}__icon--active`]: value }
);
return (
<div className={baseClass}>
{this.renderHeading()}
<input
name={name}
onChange={onInputChange}
className={inputClasses}
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}
type={type}
value={value}
/>
2016-11-28 19:20:15 +00:00
{iconName && <Icon name={iconName} className={iconClasses} />}
2016-12-01 18:57:19 +00:00
{renderHint()}
</div>
);
}
}
export default InputFieldWithIcon;