2016-09-30 18:55:15 +00:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-09-12 15:14:07 +00:00
|
|
|
import radium from 'radium';
|
|
|
|
|
import componentStyles from './styles';
|
2016-09-30 18:55:15 +00:00
|
|
|
import InputField from '../InputField';
|
2016-09-12 15:14:07 +00:00
|
|
|
|
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-10-03 17:54:22 +00:00
|
|
|
defaultValue: PropTypes.string,
|
2016-09-14 20:31:54 +00:00
|
|
|
error: PropTypes.string,
|
2016-09-12 15:14:07 +00:00
|
|
|
iconName: PropTypes.string,
|
|
|
|
|
name: PropTypes.string,
|
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
|
placeholder: PropTypes.string,
|
2016-09-14 20:31:54 +00:00
|
|
|
style: PropTypes.object,
|
2016-09-12 15:14:07 +00:00
|
|
|
type: PropTypes.string,
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-14 20:31:54 +00:00
|
|
|
renderHeading = () => {
|
|
|
|
|
const { error, placeholder } = this.props;
|
|
|
|
|
const { value } = this.state;
|
|
|
|
|
const { errorStyles, placeholderStyles } = componentStyles;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <div style={errorStyles}>{error}</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <div style={placeholderStyles(value)}>{placeholder}</div>;
|
|
|
|
|
}
|
2016-09-12 15:14:07 +00:00
|
|
|
|
|
|
|
|
render () {
|
2016-09-14 20:31:54 +00:00
|
|
|
const { error, iconName, name, placeholder, style, type } = this.props;
|
2016-09-23 18:04:01 +00:00
|
|
|
const { containerStyles, iconStyles, iconErrorStyles, inputErrorStyles, inputStyles } = componentStyles;
|
2016-09-12 15:14:07 +00:00
|
|
|
const { value } = this.state;
|
2016-09-23 18:04:01 +00:00
|
|
|
const { onInputChange } = this;
|
2016-09-12 15:14:07 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div style={containerStyles}>
|
2016-09-14 20:31:54 +00:00
|
|
|
{this.renderHeading()}
|
2016-09-12 15:14:07 +00:00
|
|
|
<input
|
|
|
|
|
name={name}
|
|
|
|
|
onChange={onInputChange}
|
2016-09-23 18:04:01 +00:00
|
|
|
className="input-with-icon"
|
2016-09-12 15:14:07 +00:00
|
|
|
placeholder={placeholder}
|
2016-09-19 18:33:43 +00:00
|
|
|
ref={(r) => { this.input = r; }}
|
2016-09-23 18:04:01 +00:00
|
|
|
style={[inputStyles(value, type), inputErrorStyles(error), style]}
|
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-10-03 17:54:22 +00:00
|
|
|
{iconName && <i className={iconName} style={[iconStyles(value), iconErrorStyles(error), style]} />}
|
2016-09-12 15:14:07 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default radium(InputFieldWithIcon);
|