#145: FormModel support for Dropdown

This commit is contained in:
SleepWalker 2016-06-18 15:13:28 +03:00
parent 3f77144a53
commit d5ca191ace
4 changed files with 76 additions and 26 deletions

View File

@ -93,7 +93,12 @@ class ContactForm extends Component {
</div> </div>
<div className={styles.formMargin}> <div className={styles.formMargin}>
<Dropdown label={messages.whichQuestion} items={CONTACT_CATEGORIES} block /> <Dropdown
{...form.bindField('category')}
label={messages.whichQuestion}
items={CONTACT_CATEGORIES}
block
/>
</div> </div>
<TextArea <TextArea

View File

@ -6,9 +6,9 @@ import classNames from 'classnames';
import { colors, COLOR_GREEN } from 'components/ui'; import { colors, COLOR_GREEN } from 'components/ui';
import styles from './dropdown.scss'; import styles from './dropdown.scss';
import FormComponent from './FormComponent'; import FormInputComponent from './FormInputComponent';
export default class Dropdown extends FormComponent { export default class Dropdown extends FormInputComponent {
static displayName = 'Dropdown'; static displayName = 'Dropdown';
static propTypes = { static propTypes = {
@ -18,11 +18,12 @@ export default class Dropdown extends FormComponent {
}), }),
PropTypes.string PropTypes.string
]).isRequired, ]).isRequired,
items: PropTypes.oneOfType([ items: PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string), PropTypes.string,
PropTypes.arrayOf(PropTypes.object) PropTypes.shape({
// TODO: ^^^ я тут хотел добавить вариант с <Message /> объектом, не уверен, что вышло верно id: PropTypes.string
]).isRequired, })
])).isRequired,
block: PropTypes.bool, block: PropTypes.bool,
color: PropTypes.oneOf(colors) color: PropTypes.oneOf(colors)
}; };
@ -33,7 +34,7 @@ export default class Dropdown extends FormComponent {
state = { state = {
isActive: false, isActive: false,
activeItem: this.props.label activeItem: null
}; };
componentDidMount() { componentDidMount() {
@ -46,11 +47,13 @@ export default class Dropdown extends FormComponent {
render() { render() {
const { color, block, items } = this.props; const { color, block, items } = this.props;
const {isActive, activeItem} = this.state; const {isActive} = this.state;
const label = this.formatMessage(activeItem); const activeItem = this.getActiveItem();
const label = this.formatMessage(activeItem.label);
return ( return (
<div>
<div className={classNames(styles[color], { <div className={classNames(styles[color], {
[styles.block]: block, [styles.block]: block,
[styles.opened]: isActive [styles.opened]: isActive
@ -59,13 +62,16 @@ export default class Dropdown extends FormComponent {
<span className={styles.toggleIcon} /> <span className={styles.toggleIcon} />
<div className={styles.menu}> <div className={styles.menu}>
{items.map((item, key) => ( {Object.entries(items).map(([value, label]) => (
<div className={styles.menuItem} key={key} onClick={this.onSelectItem(item)}> <div className={styles.menuItem} key={value} onClick={this.onSelectItem({value, label})}>
{item} {label}
</div> </div>
))} ))}
</div> </div>
</div> </div>
{this.renderError()}
</div>
); );
} }
@ -85,6 +91,32 @@ export default class Dropdown extends FormComponent {
}; };
} }
getActiveItem() {
const {items} = this.props;
let {activeItem} = this.state;
if (!activeItem) {
activeItem = {
label: this.props.label,
value: ''
};
if (!activeItem.label) {
const firstItem = Object.entries(items)[0];
activeItem = {
label: firstItem[1],
value: firstItem[0]
};
}
}
return activeItem;
}
getValue() {
return this.getActiveItem().value;
}
onToggle = (event) => { onToggle = (event) => {
event.preventDefault(); event.preventDefault();

View File

@ -1,4 +1,4 @@
import React, { Component } from 'react'; import { Component } from 'react';
import { intlShape } from 'react-intl'; import { intlShape } from 'react-intl';
@ -9,6 +9,13 @@ export default class FormComponent extends Component {
intl: intlShape.isRequired intl: intlShape.isRequired
}; };
/**
* Formats message resolving intl translations
*
* @param {string|object} message - message string, or intl message descriptor with an `id` field
*
* @return {string}
*/
formatMessage(message) { formatMessage(message) {
if (message && message.id && this.context && this.context.intl) { if (message && message.id && this.context && this.context.intl) {
message = this.context.intl.formatMessage(message); message = this.context.intl.formatMessage(message);
@ -16,4 +23,10 @@ export default class FormComponent extends Component {
return message; return message;
} }
/**
* Focuses this field
*/
focus() {
}
} }

View File

@ -1,4 +1,4 @@
import request from 'services/request'; import request from 'services/request';
export default ({subject = '', email = '', message = ''}) => export default ({subject = '', email = '', message = '', category = ''}) =>
request.post('/api/feedback', {subject, email, message}); request.post('/api/feedback', {subject, email, message, category});