jcmcneal / react-step-wizard

A modern flexible step wizard component built for React.
MIT License
583 stars 126 forks source link

Set input field focus on each step #79

Closed DiegoMcDipster closed 3 years ago

DiegoMcDipster commented 3 years ago

Hi,

I'm using the step wizard to create a quiz. Each step has a question, a formik field for the answer and a button that moves to the next step if the validation is passed.

I want the focus to be set on the input field on each step. However:

So, my question is what's the best way of implementing input field focus on each step?

Thanks in advance for you assistance.

resolritter commented 3 years ago

Try focusing the element manually

const [element, setElement] = React.useState()
React.useLayoutEffect(() => {
  if (element) {
    element.focus()
  }
}, [element])

<Component ref={setElement} />
DiegoMcDipster commented 3 years ago

Thanks @resolritter for the quick response.

Unfortunately, that didn't work. I'm still getting the same behaviour.

On the README.md you have a demo 'Create Your Account to Apply' wizard. Would you be kind enough to put that code in a sandbox so that i can play around with it. That will help me to get insight into what i've done wrong in my code. (I don't want to post my code here because it's too complex (for example, I'm using Formik and a reusable component that renders by looping through an array of questions/answers) and I don't want to waste your time with you trying to figure it out)

I'd really appreciate it if you would do that.

Again thanks!

resolritter commented 3 years ago

There's already a demo application at https://github.com/jcmcneal/react-step-wizard/tree/master/app

and some NPM commands you can try using for running it https://github.com/jcmcneal/react-step-wizard/blob/76723d6b8105a720f1968fa0c60128c780105337/package.json#L11

resolritter commented 3 years ago

Closing since I don't see an action to be taken here. Events and lifecycle-related quirks are not a concern for this library, so this question seems more like a userland struggle which would be better suited on e.g. Stack Overflow.