Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserving selection on focused inputs #1

Closed
wants to merge 3 commits into from
Closed

Preserving selection on focused inputs #1

wants to merge 3 commits into from

Conversation

aishikaty
Copy link

This will allow you to dispatch actions while inputing text and not writing at the end of the input.

BTW: I love this library!

@danculley
Copy link
Owner

danculley commented Jan 4, 2017

Thanks! And doubly thanks for including a pull request!

I'd like to better understand the use case. There may be other form state that should be preserved that we haven't thought of, so it would be good to do this in an extensible way.

Could you put together a plunker / fiddle / etc. with the use case you are thinking of?

@aishikaty
Copy link
Author

Here is the use case (try to write Hello before World):
https://plnkr.co/edit/8VmhyDHXfMR3vyPa2DVs?p=preview

(function main(h, component) {
  var initialState = { value: "World" };
  var view = function view(state, dispatch) { 
    return h('div', {}, 
      h('input', {
        id: 'input1',
        oninput: function() {
          dispatch({ type: 'SET_VALUE', value: this.value })
        },
        value: state.value
      }),
      state.value
    )
  };
  
  var reducer = function reducer(state, action) {
    switch (action.type) {
      case 'SET_VALUE': return { value: action.value }
    }
  }

  var myComponent = component(initialState, view, reducer);
  
  document.addEventListener('DOMContentLoaded', function() {
    myComponent(document.getElementById('root'))
  })
})(realDOM.h, realDOM.component);

aishikaty added 2 commits January 7, 2017 02:29
Saving 2 lines with ES2015 syntax
Added missing semicolons
@danculley
Copy link
Owner

Thanks! That was super helpful and got me thinking more about what else should get preserved. Ultimately, I decided it would make sense to preserve selectionStart, selectionEnd, selectionDirection, scrollTop, and scrollLeft for all elements with an id property. Obviously some of these won't apply sometimes, but copying them as undefined doesn't really seem to hurt anything. This resolves your bug as well.

https://plnkr.co/edit/J9WMGYQrvpGtqPAUmRdG

@danculley danculley closed this Jan 7, 2017
@aishikaty
Copy link
Author

Nice, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants