mcfly icon indicating copy to clipboard operation
mcfly copied to clipboard

Textarea's cursor jumps when changing its value through McFly

Open ferrannp opened this issue 10 years ago • 5 comments

So I came into the same exact issue as this one for reflux: https://github.com/reflux/refluxjs/issues/43

Using Flux flow Action → Store → View Like:

render: function() {
  return (
    <textarea
      value={this.props.value}
      onChange={this.handleChange}
    />
  );
},

handleChange: function(ev) {
  Actions.updateMessage(ev.target.value);
}

The problem is that typing anything when the cursor is not at the end of the box, will make the cursor to jump to the end of that box.

In the link I posted above, looks like that framework has a sync flag to make actions sync and the issue does not appear. I suppose here is happening because our actions are promises?

Any idea on how to solve this? Could we add to McFly a similar flag?

@tomatau did you run into similar issues in your projects?

ferrannp avatar Sep 29 '15 15:09 ferrannp

@kenwheeler I tested it changing the promise for normal callback (like in versions previous to 0.0.8) and it works fine. Could we add a flag to decide when we want an action to be a promise or when we want to be sync?

ferrannp avatar Sep 30 '15 13:09 ferrannp

+1, it happens the same to me

rgommezz avatar Sep 30 '15 14:09 rgommezz

good call, adding a sync flag

kenwheeler avatar Sep 30 '15 15:09 kenwheeler

Hey man, thank you! I was playing with it myself but I'm pretty sure you'll find a better approach. Just for fun I'll leave it here:

So I make that you can define an action as an array too, like:

  myAction: [
    function(someParam){
      return {
        actionType: 'CHANGE_TEXT',
        text: someParam
      }
    },
    true //Sync! to avoid: https://github.com/kenwheeler/mcfly/issues/51
  ],

Then I change ActionFactory.js:

if(actions[a].constructor === Array){
  action = new Action(actions[a][0], actions[a][1]);
}else {
  action = new Action(actions[a]);
}

And well, in the Action.js:

  function Action(callback, sync) {
    this.callback = callback;
    this.sync = sync;
  }

  function dispatchHelper(payload) {
    if (!payload) return reject();
    if (!payload.actionType) return reThrow(reject,
      "Payload object requires an actionType property"
    );
    try {
      Dispatcher.dispatch(payload);
    } catch (error) {
      reThrow(reject, error);
    }
  }

  Action.prototype.dispatch=function() {"use strict";

    if(this.sync !== true) {
      return Promise.resolve(this.callback.apply(this, arguments))
        .then(function(payload){
          return new Promise(function(resolve, reject){
            dispatchHelper(payload);
            resolve();
          });
        });
    }else {
      var payload = this.callback.apply(this, arguments);
      dispatchHelper(payload);
    }
  };

Does this make sense?

ferrannp avatar Sep 30 '15 15:09 ferrannp

hey @kenwheeler, any news on this?

ferrannp avatar Oct 26 '15 12:10 ferrannp