fleet/frontend/components/forms/fields/InputFieldWithIcon/InputFieldWithIcon.jsx
Mike Stone 40af0d29ef Forgot password (#162)
* GradientButton components

* Style guide updates

* Display errors and override styles for InputFieldWithIcon

* Envelope Icon

* Login page form submission (#157)

* API client utility

* moves test helpers to the test directory

* Utility to namespace local storage keys

* LoginSuccessfulPage component

* Check icon

* adds auth to redux state

* successful form submission

* Allow tests to load dummy SVG static images & test fixes

* ForgotPassword Page, Form & route

* Email validator
2016-09-14 16:31:54 -04:00

82 lines
2 KiB
JavaScript

import React, { Component, PropTypes } from 'react';
import radium from 'radium';
import Icon from '../../../icons/Icon';
import componentStyles from './styles';
class InputFieldWithIcon extends Component {
static propTypes = {
error: PropTypes.string,
iconName: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func,
placeholder: PropTypes.string,
style: PropTypes.object,
type: PropTypes.string,
};
static defaultProps = {
style: {},
type: 'text',
};
constructor (props) {
super(props);
this.state = { value: null };
}
onInputChange = (evt) => {
evt.preventDefault();
const { value } = evt.target;
const { onChange } = this.props;
this.setState({ value });
return onChange(evt);
}
iconVariant = () => {
const { error } = this.props;
const { value } = this.state;
if (error) return 'error';
if (value) return 'colored';
return 'default';
}
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>;
}
render () {
const { error, iconName, name, placeholder, style, type } = this.props;
const { containerStyles, iconStyles, inputErrorStyles, inputStyles } = componentStyles;
const { value } = this.state;
const { iconVariant, onInputChange } = this;
return (
<div style={containerStyles}>
{this.renderHeading()}
<input
name={name}
onChange={onInputChange}
placeholder={placeholder}
style={[inputStyles(value), inputErrorStyles(error), style]}
type={type}
/>
<Icon name={iconName} style={iconStyles} variant={iconVariant()} />
</div>
);
}
}
export default radium(InputFieldWithIcon);