2016-10-21 20:42:14 -02:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-10-30 16:01:10 -07:00
|
|
|
import { List, Map, fromJS } from 'immutable';
|
|
|
|
import { sortable } from 'react-sortable';
|
2016-10-28 19:13:26 +02:00
|
|
|
import ObjectControl from './ObjectControl';
|
|
|
|
import styles from './ListControl.css';
|
2016-10-21 20:42:14 -02:00
|
|
|
|
2016-10-30 16:01:10 -07:00
|
|
|
function ListItem(props) {
|
|
|
|
return <div {...props} className={`list-item ${ props.className }`}>{props.children}</div>;
|
|
|
|
}
|
|
|
|
ListItem.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
ListItem.displayName = 'list-item';
|
|
|
|
|
|
|
|
const SortableListItem = sortable(ListItem);
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
export default class ListControl extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.node,
|
2016-10-30 16:01:10 -07:00
|
|
|
field: PropTypes.node,
|
2016-10-21 20:42:14 -02:00
|
|
|
};
|
2016-10-28 19:13:26 +02:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-10-30 16:01:10 -07:00
|
|
|
this.state = { itemStates: Map() };
|
2016-10-28 19:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
handleChange = (e) => {
|
|
|
|
this.props.onChange(e.target.value.split(',').map(item => item.trim()));
|
|
|
|
};
|
|
|
|
|
2016-10-28 19:13:26 +02:00
|
|
|
handleAdd = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { value, onChange } = this.props;
|
|
|
|
|
|
|
|
onChange((value || List()).push(Map()));
|
|
|
|
};
|
|
|
|
|
|
|
|
handleChangeFor(index) {
|
|
|
|
return (newValue) => {
|
|
|
|
const { value, onChange } = this.props;
|
|
|
|
onChange(value.set(index, newValue));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRemove(index) {
|
|
|
|
return (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { value, onChange } = this.props;
|
|
|
|
onChange(value.remove(index));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleToggle(index) {
|
|
|
|
return (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { itemStates } = this.state;
|
|
|
|
this.setState({
|
|
|
|
itemStates: itemStates.setIn([index, 'collapsed'], !itemStates.getIn([index, 'collapsed'])),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
objectLabel(item) {
|
|
|
|
const { field } = this.props;
|
|
|
|
const fields = field.get('fields');
|
|
|
|
const first = fields.first();
|
|
|
|
const value = item.get(first.get('name'));
|
2016-10-30 16:01:10 -07:00
|
|
|
return value || `No ${ first.get('name') }`;
|
2016-10-28 19:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-30 16:01:10 -07:00
|
|
|
handleSort = (obj) => {
|
|
|
|
this.setState({ draggingIndex: obj.draggingIndex });
|
|
|
|
if ('items' in obj) {
|
|
|
|
this.props.onChange(fromJS(obj.items));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-28 19:13:26 +02:00
|
|
|
renderItem(item, index) {
|
2016-10-30 16:01:10 -07:00
|
|
|
const { value, field, getMedia, onAddMedia, onRemoveMedia } = this.props;
|
|
|
|
const { itemStates, draggedItem } = this.state;
|
2016-10-28 19:13:26 +02:00
|
|
|
const collapsed = itemStates.getIn([index, 'collapsed']);
|
|
|
|
const classNames = [styles.item, collapsed ? styles.collapsed : styles.expanded];
|
|
|
|
|
2016-10-30 16:01:10 -07:00
|
|
|
return (<SortableListItem
|
|
|
|
key={index}
|
|
|
|
updateState={this.handleSort}
|
|
|
|
items={value ? value.toJS() : []}
|
|
|
|
draggingIndex={this.state.draggingIndex}
|
|
|
|
sortId={index}
|
|
|
|
outline="list"
|
|
|
|
>
|
|
|
|
<div className={classNames.join(' ')}>
|
|
|
|
<div className={styles.objectLabel}>{this.objectLabel(item)}</div>
|
|
|
|
<div className={styles.objectControl}>
|
|
|
|
<ObjectControl
|
|
|
|
value={item}
|
|
|
|
field={field}
|
|
|
|
onChange={this.handleChangeFor(index)}
|
|
|
|
getMedia={getMedia}
|
|
|
|
onAddMedia={onAddMedia}
|
|
|
|
onRemoveMedia={onRemoveMedia}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<button className={styles.toggleButton} onClick={this.handleToggle(index)}>
|
|
|
|
{collapsed ? '+' : '-'}
|
|
|
|
</button>
|
|
|
|
<button className={styles.removeButton} onClick={this.handleRemove(index)}>x</button>
|
2016-10-28 19:13:26 +02:00
|
|
|
</div>
|
2016-10-30 16:01:10 -07:00
|
|
|
</SortableListItem>);
|
2016-10-28 19:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
renderListControl() {
|
2016-10-30 16:01:10 -07:00
|
|
|
const { value, field } = this.props;
|
2016-10-28 19:13:26 +02:00
|
|
|
return (<div>
|
|
|
|
{value && value.map((item, index) => this.renderItem(item, index))}
|
|
|
|
<div><button className={styles.addButton} onClick={this.handleAdd}>new</button></div>
|
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { value, field } = this.props;
|
|
|
|
|
|
|
|
if (field.get('fields')) {
|
|
|
|
return this.renderListControl();
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
return <input type="text" value={value ? value.join(', ') : ''} onChange={this.handleChange} />;
|
|
|
|
}
|
|
|
|
}
|