[Bug]: DatePicker Compat steals focus on every render
Environment Information
- Package version(s): @fluentui/[email protected]
- Browser and OS versions:
Describe the issue:
Actual behavior:
When you have controlled components that re-renders the app, the datepicker steals the focus. Its probably has a bad useEffect code that focuses the component every time it renders.
A simple controlled Input component will have its focus stolen when typing a character in the input.
Expected behavior:
The DatePicker should not steal the focus on every render.
If applicable, please provide a codepen repro:
Simple codepen example of the bug:
Type into input which causes a re-render, and focus moves out of input to date picker.
https://codesandbox.io/s/sad-wright-t2hby7?file=/example.tsx:617-627
Priorities and help requested (not applicable if asking question):
Are you willing to submit a PR to fix? Yes
Requested priority: Blocking
Products/sites affected:
https://github.com/microsoft/fluentui/blob/c29489c82d4e9dc541135e65f1e2a4a5159a2a05/packages/react-components/react-datepicker-compat/src/components/DatePicker/useDatePicker.tsx#LL29C10-L29C23
The function useFocusLogic returns a focus function that changes every time the component renders.
https://github.com/microsoft/fluentui/blob/c29489c82d4e9dc541135e65f1e2a4a5159a2a05/packages/react-components/react-datepicker-compat/src/components/DatePicker/useDatePicker.tsx#L427
The focus function is used in the useEffect dependencies. This will make the code run at every render.
I don't think you need focus as a dependencies. If you remove it from the dependencies array it should work.
Not sure why this is not being triaged. It's a breaking bug.
We're just back from a long weekend. I'll triage and get the component author involved as soon as I can.
@ParadimeWeb Thanks for reporting the bug and going in depth! this will be closed with the PR linked. Thanks!
:tada:This issue was addressed in #28053, which has now been successfully released as @fluentui/[email protected].:tada:
Handy links:
:tada:This issue was addressed in #28053, which has now been successfully released as @fluentui/[email protected].:tada:
Handy links: