Giter VIP home page Giter VIP logo

Comments (3)

chancyk avatar chancyk commented on May 24, 2024 1

@Geordi7 It seems the first approach causes the entire parent div to refresh, which wipes out the input element, killing the focus. Wrapping it in a span so it has its own node also seems to resolve the issue.

const focus = e => e.focus();
class C extends Component {
  	state = {a: {b: {c: ''}}}
	view = s => <div>
        <span>Hello {s.a.b.c}</span>
      	<input type='text' value={s.a.b.c}
          $oninput={(s,e) => (s.a.b.c=e.target.value, s)}
          />
      </div>
}
    
new C().start(document.body)

Another solution might be to use a ref so it's refocused when the input element is refreshed:

const focus = e => e.focus();
class C extends Component {
  	state = {a: {b: {c: ''}}}
	view = s => <div>
        Hello {s.a.b.c}<br/>
      	<input ref={focus} type='text' value={s.a.b.c}
          $oninput={(s,e) => (s.a.b.c=e.target.value, s)}
          />
      </div>
}
    
new C().start(document.body)

There's an example called Ref - Examples that uses the ref approach for contenteditable also, which I've noticed can be quirky in frameworks also for similar reasons.

from apprun.

Geordi7 avatar Geordi7 commented on May 24, 2024

After further testing it seems the problem is not with the input itself but with the text node before it.

if you replace Hello {s.a.b.c}<br /> with {'Hello ' + s.a.b.c}<br /> it works correctly.

This is probably still a bug.

from apprun.

chancyk avatar chancyk commented on May 24, 2024

I ran into another good usage for ref. MaterializeCSS relies on input elements having a specific type set, but when type="text" will sometimes get stripped from the element because it's the default, causing the label animations not to work. I was able to use a ref to specifically reset the type attribute which fixed the issue.

from apprun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.