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

99 lines
2.5 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';
2016-05-30 10:10:59 +05:30
import { colors, skins, SKIN_DARK, COLOR_GREEN } from 'components/ui';
2016-05-02 12:45:42 +05:30
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,
2016-05-30 10:10:59 +05:30
skin: PropTypes.oneOf(skins),
color: PropTypes.oneOf(colors)
};
static defaultProps = {
color: COLOR_GREEN,
skin: SKIN_DARK
2016-05-02 12:45:42 +05:30
};
render() {
2016-05-30 10:10:59 +05:30
const { color, skin } = this.props;
let { icon, 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);
}
}