accounts-frontend/src/components/ui/form/Input.jsx

92 lines
2.4 KiB
React
Raw Normal View History

import React, { PropTypes } from 'react';
2016-05-02 12:45:42 +05:30
import classNames from 'classnames';
import { uniqueId } from 'functions';
import icons from 'components/ui/icons.scss';
import styles from './form.scss';
import FormInputComponent from './FormInputComponent';
2016-05-02 12:45:42 +05:30
export default class Input extends FormInputComponent {
2016-05-02 12:45:42 +05:30
static displayName = 'Input';
static propTypes = {
placeholder: PropTypes.oneOfType([
PropTypes.shape({
id: PropTypes.string
}),
PropTypes.string
]),
label: PropTypes.oneOfType([
PropTypes.shape({
id: PropTypes.string
}),
PropTypes.string
]),
2016-05-02 12:45:42 +05:30
error: PropTypes.string,
icon: PropTypes.string,
skin: PropTypes.oneOf(['dark', 'light']),
2016-05-02 23:06:05 +05:30
color: PropTypes.oneOf(['green', 'blue', 'red', 'lightViolet', 'darkBlue', 'violet'])
2016-05-02 12:45:42 +05:30
};
render() {
let { icon, color = 'green', skin = 'dark', label } = this.props;
2016-05-02 12:45:42 +05:30
const props = {
type: 'text',
...this.props
};
if (label) {
if (!props.id) {
props.id = uniqueId('input');
}
label = this.formatMessage(label);
2016-05-02 12:45:42 +05:30
label = (
<label className={styles.textFieldLabel} htmlFor={props.id}>
{label}
</label>
);
}
props.placeholder = this.formatMessage(props.placeholder);
2016-05-02 12:45:42 +05:30
let baseClass = styles.formRow;
if (icon) {
baseClass = styles.formIconRow;
icon = (
<div className={classNames(styles.textFieldIcon, icons[icon])} />
);
}
return (
<div className={baseClass}>
{label}
<div className={styles.textFieldContainer}>
<input ref={this.setEl}
className={classNames(
styles[`${skin}TextField`],
styles[`${color}TextField`]
)}
{...props}
/>
2016-05-02 12:45:42 +05:30
{icon}
</div>
{this.renderError()}
2016-05-02 12:45:42 +05:30
</div>
);
}
getValue() {
return this.el.value;
}
focus() {
this.el.focus();
setTimeout(this.el.focus.bind(this.el), 10);
}
}