From 9c1683a887f8af6affaca0dcb5cdcb72300da140 Mon Sep 17 00:00:00 2001 From: Benaiah Mischenko Date: Mon, 16 Oct 2017 18:16:03 -0700 Subject: [PATCH 1/3] Migrate ListControl from react-sortable to react-sortable-hoc --- package.json | 2 +- src/components/Widgets/ListControl.css | 7 ++- src/components/Widgets/ListControl.js | 82 +++++++++++++------------- yarn.lock | 15 +++-- 4 files changed, 57 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 82dc7a44587a..a1fd868cc3dd 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "react-router-dom": "^4.2.2", "react-router-redux": "^5.0.0-alpha.6", "react-sidebar": "^2.2.1", - "react-sortable": "^1.2.0", + "react-sortable-hoc": "^0.6.8", "react-split-pane": "^0.1.66", "react-toolbox": "^2.0.0-beta.12", "react-topbar-progress-indicator": "^1.0.0", diff --git a/src/components/Widgets/ListControl.css b/src/components/Widgets/ListControl.css index 72aaa7dcc01a..e6798ed51351 100644 --- a/src/components/Widgets/ListControl.css +++ b/src/components/Widgets/ListControl.css @@ -52,7 +52,6 @@ .nc-listControl-item { position: relative; padding-left: 24px; - cursor: move; } .nc-listControl-objectLabel { @@ -83,9 +82,11 @@ } .nc-listControl-dragIcon { + cursor: move; + display: block; position: absolute; + text-align: center; top: 2px; - display: block; width: 100%; - text-align: center; + z-index: 1; } diff --git a/src/components/Widgets/ListControl.js b/src/components/Widgets/ListControl.js index ddf6ece6f363..d40e015aea25 100644 --- a/src/components/Widgets/ListControl.js +++ b/src/components/Widgets/ListControl.js @@ -1,12 +1,12 @@ import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { List, Map, fromJS } from 'immutable'; -import { sortable } from 'react-sortable'; +import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc'; import FontIcon from 'react-toolbox/lib/font_icon'; import ObjectControl from './ObjectControl'; function ListItem(props) { - return
{props.children}
; + return
{props.children}
; } ListItem.propTypes = { className: PropTypes.string, @@ -18,7 +18,12 @@ function valueToString(value) { return value ? value.join(',').replace(/,([^\s]|$)/g, ', $1') : ''; } -const SortableListItem = sortable(ListItem); +const SortableListItem = SortableElement(ListItem); +const DragHandle = SortableHandle( + () => +); +const SortableList = SortableContainer(({ items, renderItem }) => + (
{items.map(renderItem)}
)); const valueTypes = { SINGLE: 'SINGLE', @@ -126,55 +131,52 @@ export default class ListControl extends Component { return value || `No ${ labelField.get('name') }`; } - handleSort = (obj) => { - this.setState({ draggingIndex: obj.draggingIndex }); - if ('items' in obj) { - this.props.onChange(fromJS(obj.items)); - } + onSortEnd = ({ oldIndex, newIndex }) => { + const { value, onChange } = this.props; + const item = value.get(oldIndex); + const newValue = value.delete(oldIndex).insert(newIndex, item); + this.props.onChange(newValue); }; - renderItem(item, index) { - const { value, field, getAsset, onAddAsset, onRemoveAsset } = this.props; + renderItem = (item, index) => { + const { field, getAsset, onAddAsset, onRemoveAsset } = this.props; const { itemStates } = this.state; const collapsed = itemStates.getIn([index, 'collapsed']); const classNames = ['nc-listControl-item', collapsed ? 'nc-listControl-collapsed' : 'nc-listControl-expanded']; - return ( -
- - - -
{this.objectLabel(item)}
- -
+ return ( + + + +
{this.objectLabel(item)}
+
); - } + }; renderListControl() { const { value, forID, field } = this.props; const listLabel = field.get('label'); return (
- {value && value.map((item, index) => this.renderItem(item, index))} +