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

The autoFocus property doesn't work for the Input component inside the Modal #41239

Open
jacky20001 opened this issue Mar 14, 2023 · 6 comments · May be fixed by react-component/dialog#361
Open
Labels
🐛 Bug Ant Design Team had proved that this is a bug. Inactive

Comments

@jacky20001
Copy link

Reproduction link

Edit on CodeSandbox

Steps to reproduce

Open the modal and you see the input field is not focused

What is expected?

The input field should be focused

What is actually happening?

The input field is not focused

Environment Info
antd 4.24.0
React react
System N/A
Browser Google Chrome
@github-actions
Copy link
Contributor

afc163 referenced this issue in react-component/dialog Mar 14, 2023
* chore: father4 & rc-test

* chore: add husky

* chore: update test

---------

Co-authored-by: MadCcc <1075746765@qq.com>
@zombieJ zombieJ added 🐛 Bug Ant Design Team had proved that this is a bug. and removed unconfirmed labels Mar 14, 2023
@acyza acyza linked a pull request Mar 20, 2023 that will close this issue
@arifcakiroglu
Copy link

arifcakiroglu commented Mar 24, 2023

temporary solution.

const textInput = useRef(null);

setTimeout(() => {
    textInput.current.focus()
}, 0);

return <Input ref={textInput} />;

@MichaelJHaywood
Copy link

temporary solution.

const textInput = useRef(null);

setTimeout(() => {
    textInput.current.focus()
}, 0);

return <Input ref={textInput} />;

I also have a problem with this in v5 but when using Forms with rules. I haven't managed to find a method that of setting focus without using setTimeout with 1s delay which is too long. Any less and it can sometimes not work as the modal can take a longer time to display.

Another solution I found is adding a ref and setting focus using useEffect. This causes the focus to be added, removed, and added again which triggers the form rule and displays an error message.
E.g.

  const myRef = useRef();

  useEffect(()=>{
    if (myRef && myRef.current) {
      const { input } = myRef.current
      input.focus()
    }
  })

Here it is mentioned that destroyOnClose can be used. I can see it apply the focus but it is then removed again to display the form rule error message.

Overall setTimeout is the only solution I can find but it requires a very long delay. I would expect the framework to handle this for us with the autofocus property in a similar fashion to other frameworks. (E.g. PrimeVue) so it it minimal work for the developer.

@zhenzhenChange
Copy link

zhenzhenChange commented Mar 19, 2024

How to handle this elegantly, now that there is a focus on management needs. Can you only call foucs methods with a ref?

@AlyevElvin
Copy link

AlyevElvin commented Apr 19, 2024

1 case :

const emailInput = useCallback(
(inputElement: InputRef) => {
        if (inputElement) {
          inputElement.focus();
        }
  }, []);

  <Input
  ref={emailInput}
  value={value}
  onChange={onChange}
/>      

2 case


  const emailInput = useRef<InputRef>(null);
  useEffect(() => {
    if (emailInput.current) {
      emailInput.current.focus();
    }
  }, [emailInput]);

    <Input
  ref={emailInput}
  value={value}
  onChange={onChange}
/>      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Ant Design Team had proved that this is a bug. Inactive
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants