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

109 lines
2.9 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
2016-05-02 12:45:42 +05:30
import classNames from 'classnames';
import { uniqueId, omit } from 'functions';
2016-05-02 12:45:42 +05:30
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
]),
2017-02-27 11:17:31 +05:30
error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
type: PropTypes.string.isRequired,
payload: PropTypes.object.isRequired
})
]),
2016-05-02 12:45:42 +05:30
icon: PropTypes.string,
2016-05-30 10:10:59 +05:30
skin: PropTypes.oneOf(skins),
color: PropTypes.oneOf(colors),
center: PropTypes.bool
2016-05-30 10:10:59 +05:30
};
static defaultProps = {
color: COLOR_GREEN,
skin: SKIN_DARK
2016-05-02 12:45:42 +05:30
};
render() {
const { color, skin, center } = this.props;
2016-05-30 10:10:59 +05:30
let { icon, label } = this.props;
2016-05-02 12:45:42 +05:30
const props = omit({
2016-05-02 12:45:42 +05:30
type: 'text',
...this.props
}, Object.keys(Input.propTypes).filter((prop) => prop !== 'placeholder'));
2016-05-02 12:45:42 +05:30
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 = (
<span className={classNames(styles.textFieldIcon, icons[icon])} />
2016-05-02 12:45:42 +05:30
);
}
return (
<div className={baseClass}>
{label}
<div className={styles.textFieldContainer}>
<input ref={this.setEl}
className={classNames(
styles[`${skin}TextField`],
styles[`${color}TextField`],
{
[styles.textFieldCenter]: center
}
)}
{...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);
}
}