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

How to set field autoFocus? #524

Closed
sneas opened this issue Sep 25, 2019 · 3 comments
Closed

How to set field autoFocus? #524

sneas opened this issue Sep 25, 2019 · 3 comments

Comments

@sneas
Copy link

sneas commented Sep 25, 2019

Hi, thank you for mobx-react-form!

Is it possible to set up autoFocus of a particular field to true through the field configuration without calling focus() method explicitly?

This way obviously isn't working:

const form = new MobxReactForm({
    fields: [
        {
            name: 'firstName',
            label: 'First Name',
            autoFocus: true,
        },
    ],
});

console.log(form.$('firstName').bind().autoFocus); // false
@foxhound87
Copy link
Owner

foxhound87 commented Oct 2, 2019

It is not possible because it should eventually unfocus also all other fields recursively if you use autoFocus: true for more fields. I don't think it is a good idea to allow focus in this way.

You can use hooks like this:

new MobxReactForm({
  fields: [{
    name: 'firstName',
    label: 'First Name',
  }],
}, {
  hooks: {
    onInit(form) {
      form.$('firstName').focus();
    }
  }
});

@foxhound87
Copy link
Owner

If it doesn't work yet use createRef and forwardRef:
https://codesandbox.io/s/form-focus-d4ttb

@github-actions
Copy link

🎉 This issue has been resolved in version 5.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

2 participants