You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to integrate Sortable module with our code, but the problem is that we are using ES6 in our code.. As far as I know, mixins cannot be used in this situation, but they can be switched to Composed Components. Maybe there is anyone who already tried to convert that?
Okay I've played around the mixin and created High Order Component from that and it works, but, wondering, maybe someone know how to get the internal instance of HOC in proper way, cause now I'm getting the instance of that component by accessing this._reactInternalInstance._renderedComponent._instance
Okay, figured it out. In HOC when I am passing ComposedComponent, in render method I am just adding ref attribute, and then when I want to get that instance I am just calling this.refs.[refName]. If someone would like to take a look at HOC version instead of mixin just reply here, I'll try to make a merge request then.
I managed to get it working by rewriting existing react-sortable-mixin.js in ES6. See my example below:
react-sortable.jsx
import_from'lodash';importReactfrom'react';importReactDOMfrom'react-dom';importSortablefrom'sortablejs';constdefaultOptions={ref: 'list',model: 'items',onStart: 'handleStart',onEnd: 'handleEnd',onAdd: 'handleAdd',onUpdate: 'handleUpdate',onRemove: 'handleRemove',onSort: 'handleSort',onFilter: 'handleFilter',onMove: 'handleMove'};let_nextSibling=null;let_activeComponent=null;constgetModelName=(component)=>{let{ sortableOptions ={}}=component;let{ model }=sortableOptions;returnmodel||defaultOptions.model;};constgetModelItems=(component)=>{letmodel=getModelName(component);let{ state ={}, props ={}}=component;letitems=state[model]||props[model]||[];returnitems.slice();};classReactSortableextendsReact.Component{_sortableInstance=null;componentDidMount(){constoptions=_.merge({},defaultOptions,this.sortableOptions);constemitEvent=(type,evt)=>{constmethod=this[options[type]];method&&method.call(this,evt,this._sortableInstance);};letcopyOptions=_.extend({},options);[// Bind callbacks so that 'this' refers to the component'onStart','onEnd','onAdd','onSort','onUpdate','onRemove','onFilter','onMove'].forEach((name)=>{copyOptions[name]=(evt)=>{if(name==='onStart'){_nextSibling=evt.item.nextElementSibling;_activeComponent=this;}elseif(name==='onAdd'||name==='onUpdate'){evt.from.insertBefore(evt.item,_nextSibling);letnewState={};letremoteState={};letoldIndex=evt.oldIndex;letnewIndex=evt.newIndex;letitems=getModelItems(this);if(name==='onAdd'){letremoteItems=getModelItems(_activeComponent);letitem=remoteItems.splice(oldIndex,1)[0];items.splice(newIndex,0,item);remoteState[getModelName(_activeComponent)]=remoteItems;}else{items.splice(newIndex,0,items.splice(oldIndex,1)[0]);}newState[getModelName(this)]=items;if(copyOptions.stateHandler){this[copyOptions.stateHandler](newState);}else{this.setState(newState);}(this!==_activeComponent)&&_activeComponent.setState(remoteState);}setTimeout(()=>{emitEvent(name,evt);},0);};});letdomNode=ReactDOM.findDOMNode(this.refs[options.ref]||this);this._sortableInstance=Sortable.create(domNode,copyOptions);}componentWillReceiveProps(nextProps){letnewState={};letmodel=getModelName(this);letitems=nextProps[model];if(items){newState[model]=items;this.setState(newState);}}componentWillUnmount(){this._sortableInstance.destroy();this._sortableInstance=null;}}exportdefaultReactSortable;
@falzhobel Yes! It's basically the same but only to adopt changes for React v0.14 (e.g. ReactDOM).
It would be great if a ES6 version of react-sortable.jsx is available for developers to import to their projects. For example:
Dear all! Meteor is moved to the separate repository. If this issue is still actual, please create it there once again and this rojects needs a maintainer:
Activity
kbiedrzycki commentedon Jan 7, 2016
Okay I've played around the mixin and created High Order Component from that and it works, but, wondering, maybe someone know how to get the internal instance of HOC in proper way, cause now I'm getting the instance of that component by accessing
this._reactInternalInstance._renderedComponent._instancekbiedrzycki commentedon Jan 8, 2016
Okay, figured it out. In HOC when I am passing ComposedComponent, in render method I am just adding ref attribute, and then when I want to get that instance I am just calling
this.refs.[refName]. If someone would like to take a look at HOC version instead of mixin just reply here, I'll try to make a merge request then.cheton commentedon Jan 27, 2016
I managed to get it working by rewriting existing react-sortable-mixin.js in ES6. See my example below:
react-sortable.jsx
Sample code:
cheton commentedon Jan 28, 2016
@falzhobel Yes! It's basically the same but only to adopt changes for React v0.14 (e.g. ReactDOM).
It would be great if a ES6 version of
react-sortable.jsxis available for developers to import to their projects. For example:cheton commentedon Mar 10, 2016
A higher order React component (HOC) for Sortable is available at https://github.com/cheton/react-sortable.
You can run
npm install --save react-sortablejsto install the package.RubaXa commentedon Jul 4, 2016
Dear all! Meteor is moved to the separate repository. If this issue is still actual, please create it there once again and this rojects needs a maintainer: