import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { List, Map, fromJS } from 'immutable'; import { partial } from 'lodash'; import c from 'classnames'; import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc'; import { Icon, ListItemTopBar } from 'UI'; import ObjectControl from 'EditorWidgets/Object/ObjectControl'; function ListItem(props) { return
{props.children}
; } ListItem.propTypes = { className: PropTypes.string, children: PropTypes.node, }; ListItem.displayName = 'list-item'; function valueToString(value) { return value ? value.join(',').replace(/,([^\s]|$)/g, ', $1') : ''; } const SortableListItem = SortableElement(ListItem); const TopBar = ({ onAdd, listLabel, collapsed, onCollapseToggle, itemsCount }) => (
{itemsCount} {listLabel}
); const SortableList = SortableContainer(({ items, renderItem }) => { return
{items.map(renderItem)}
; }); const valueTypes = { SINGLE: 'SINGLE', MULTIPLE: 'MULTIPLE', }; export default class ListControl extends Component { static propTypes = { onChange: PropTypes.func.isRequired, onChangeObject: PropTypes.func.isRequired, value: PropTypes.oneOfType([ ImmutablePropTypes.list, PropTypes.string, ]), field: PropTypes.node, forID: PropTypes.string, mediaPaths: ImmutablePropTypes.map.isRequired, getAsset: PropTypes.func.isRequired, onOpenMediaLibrary: PropTypes.func.isRequired, onAddAsset: PropTypes.func.isRequired, onRemoveInsertedMedia: PropTypes.func.isRequired, classNameWrapper: PropTypes.string.isRequired, setActiveStyle: PropTypes.func.isRequired, setInactiveStyle: PropTypes.func.isRequired, }; constructor(props) { super(props); this.state = { collapsed: false, itemsCollapsed: List(), value: valueToString(props.value), }; this.valueType = null; } /** * Always update so that each nested widget has the option to update. This is * required because ControlHOC provides a default `shouldComponentUpdate` * which only updates if the value changes, but every widget must be allowed * to override this. */ shouldComponentUpdate() { return true; } componentDidMount() { const { field } = this.props; if (field.get('fields')) { this.valueType = valueTypes.MULTIPLE; } else if (field.get('field')) { this.valueType = valueTypes.SINGLE; } } componentWillUpdate(nextProps) { if (this.props.field === nextProps.field) return; if (nextProps.field.get('fields')) { this.valueType = valueTypes.MULTIPLE; } else if (nextProps.field.get('field')) { this.valueType = valueTypes.SINGLE; } } handleChange = (e) => { const { onChange } = this.props; const oldValue = this.state.value; const newValue = e.target.value; const listValue = e.target.value.split(','); if (newValue.match(/,$/) && oldValue.match(/, $/)) { listValue.pop(); } const parsedValue = valueToString(listValue); this.setState({ value: parsedValue }); onChange(listValue.map(val => val.trim())); }; handleFocus = () => { this.props.setActiveStyle(); } handleBlur = (e) => { const listValue = e.target.value.split(',').map(el => el.trim()).filter(el => el); this.setState({ value: valueToString(listValue) }); this.props.setInactiveStyle(); }; handleAdd = (e) => { e.preventDefault(); const { value, onChange } = this.props; const parsedValue = (this.valueType === valueTypes.SINGLE) ? null : Map(); this.setState({ collapsed: false }); onChange((value || List()).push(parsedValue)); }; /** * In case the `onChangeObject` function is frozen by a child widget implementation, * e.g. when debounced, always get the latest object value instead of using * `this.props.value` directly. */ getObjectValue = idx => this.props.value.get(idx) || Map(); handleChangeFor(index) { return (fieldName, newValue, newMetadata) => { const { value, metadata, onChange, forID } = this.props; const newObjectValue = this.getObjectValue(index).set(fieldName, newValue); const parsedValue = (this.valueType === valueTypes.SINGLE) ? newObjectValue.first() : newObjectValue; const parsedMetadata = { [forID]: Object.assign(metadata ? metadata.toJS() : {}, newMetadata ? newMetadata[forID] : {}), }; onChange(value.set(index, parsedValue), parsedMetadata); }; } handleRemove = (index, event) => { event.preventDefault(); const { value, metadata, onChange, forID } = this.props; const parsedMetadata = metadata && { [forID]: metadata.removeIn(value.get(index).valueSeq()) }; onChange(value.remove(index), parsedMetadata); } handleCollapseToggle = () => { this.setState({ collapsed: !this.state.collapsed }); } handleItemCollapseToggle = (index, event) => { event.preventDefault(); const { itemsCollapsed } = this.state; this.setState({ itemsCollapsed: itemsCollapsed.set(index, !itemsCollapsed.get(index, false)), }); } objectLabel(item) { const { field } = this.props; const multiFields = field.get('fields'); const singleField = field.get('field'); const labelField = (multiFields && multiFields.first()) || singleField; const value = multiFields ? item.get(multiFields.first().get('name')) : singleField.get('label'); return value || `No ${ labelField.get('name') }`; } onSortEnd = ({ oldIndex, newIndex }) => { const { value, onChange } = this.props; // Update value const item = value.get(oldIndex); const newValue = value.delete(oldIndex).insert(newIndex, item); this.props.onChange(newValue); // Update collapsing const { itemsCollapsed } = this.state; const collapsed = itemsCollapsed.get(oldIndex); const newItemsCollapsed = itemsCollapsed.delete(oldIndex).insert(newIndex, collapsed); this.setState({ itemsCollapsed: newItemsCollapsed }); }; renderItem = (item, index) => { const { field, getAsset, mediaPaths, onOpenMediaLibrary, onAddAsset, onRemoveInsertedMedia, classNameWrapper, } = this.props; const { itemsCollapsed } = this.state; const collapsed = itemsCollapsed.get(index); const classNames = ['nc-listControl-item', collapsed ? 'nc-listControl-collapsed' : '']; return (
{this.objectLabel(item)}
); }; renderListControl() { const { value, forID, field, classNameWrapper } = this.props; const { collapsed } = this.state; const items = value || List(); const className = c(classNameWrapper, 'nc-listControl', { 'nc-listControl-collapsed' : collapsed, }); return (
{ collapsed ? null : }
); } render() { const { field, forID, classNameWrapper } = this.props; const { value } = this.state; if (field.get('field') || field.get('fields')) { return this.renderListControl(); } return (); } };