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 · 11 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 <[email protected]>
@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}
/>      

@vasile-encord
Copy link

It is working with the ref, but only on initial open, when you close and open again it won't work

@SamirKhan21860
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.

I think doing it this way like it mentioned above, works well. (Don't use destroyOnClose in Modal)
Try The exact way I'm doing it.(Which works the best):

const [modalVisible, setModalVisible] = useState(false);

// Using useRef to autoFucs the input filed in the model
  const inputRef = useRef(null);

// Here is the inputRef foucs, using setTimeout(()=>{}, 0); with zero on purpose
useEffect(() => {
  if (modalVisible && inputRef.current) {
    setTimeout(() => {
      inputRef.current.focus();
    }, 0);
  }
}, [modalVisible, inputRef]);


<Modal> // don't user destoryOnClose in opening tag of Modal
    <Input autoFocus autoComplete="off" ref={inputRef} />
</Modal>

@SamirKhan21860
Copy link

I think doing it this way like it mentioned below, works well. (Don't use destroyOnClose in Modal)
Try The exact way I'm doing it.(Which works the best):

const [modalVisible, setModalVisible] = useState(false);

// Using useRef to autoFucs the input filed in the model
  const inputRef = useRef(null);

// Here is the inputRef foucs, using setTimeout(()=>{}, 0); with zero on purpose
useEffect(() => {
  if (modalVisible && inputRef.current) {
    setTimeout(() => {
      inputRef.current.focus();
    }, 0);
  }
}, [modalVisible, inputRef]);


<Modal> // don't user destoryOnClose in opening tag of Modal
    <Input autoFocus autoComplete="off" ref={inputRef} />
</Modal>

@tejastn10
Copy link

It appears that the PR has been in a draft state for over a year.
Can anyone please provide an update on this issue?

@zhenzhenChange
Copy link

zhenzhenChange commented Nov 11, 2024

Wait until the modal is open and then focus.

// antd v5.21.6
export function Overlay() {
  const autoFocusRef = useRef<Nullable<InputRef>>(null)

  return (
    <Modal afterOpenChange={(open) => open && autoFocusRef.current?.focus()}>
      <Input ref={autoFocusRef} />
    </Modal>
  )
}

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.

10 participants