2016-05-02 14:50:50 +05:30
|
|
|
import React, { PropTypes } from 'react';
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2016-07-29 22:44:52 +05:30
|
|
|
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';
|
2016-05-02 14:50:50 +05:30
|
|
|
import FormInputComponent from './FormInputComponent';
|
2016-05-02 12:45:42 +05:30
|
|
|
|
2016-05-02 14:50:50 +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 14:50:50 +05:30
|
|
|
]),
|
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),
|
2016-06-16 18:41:55 +05:30
|
|
|
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() {
|
2016-06-16 18:41:55 +05:30
|
|
|
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
|
|
|
|
2016-07-29 22:44:52 +05:30
|
|
|
const props = omit({
|
2016-05-02 12:45:42 +05:30
|
|
|
type: 'text',
|
|
|
|
...this.props
|
2016-07-29 22:44:52 +05:30
|
|
|
}, 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');
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:50:50 +05:30
|
|
|
label = this.formatMessage(label);
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
label = (
|
|
|
|
<label className={styles.textFieldLabel} htmlFor={props.id}>
|
|
|
|
{label}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:50:50 +05:30
|
|
|
props.placeholder = this.formatMessage(props.placeholder);
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
let baseClass = styles.formRow;
|
|
|
|
if (icon) {
|
|
|
|
baseClass = styles.formIconRow;
|
|
|
|
icon = (
|
2016-06-16 18:41:55 +05:30
|
|
|
<span className={classNames(styles.textFieldIcon, icons[icon])} />
|
2016-05-02 12:45:42 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={baseClass}>
|
|
|
|
{label}
|
|
|
|
<div className={styles.textFieldContainer}>
|
2016-05-02 14:50:50 +05:30
|
|
|
<input ref={this.setEl}
|
|
|
|
className={classNames(
|
|
|
|
styles[`${skin}TextField`],
|
2016-06-16 18:41:55 +05:30
|
|
|
styles[`${color}TextField`],
|
|
|
|
{
|
|
|
|
[styles.textFieldCenter]: center
|
|
|
|
}
|
2016-05-02 14:50:50 +05:30
|
|
|
)}
|
|
|
|
{...props}
|
|
|
|
/>
|
2016-05-02 12:45:42 +05:30
|
|
|
{icon}
|
|
|
|
</div>
|
2016-05-02 14:50:50 +05:30
|
|
|
{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);
|
|
|
|
}
|
|
|
|
}
|