mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.36k stars 32.13k forks source link

[material-ui][Autocomplete] onHighlightChange called with undefined event contrary to type definition #43213

Open sydneyjodon-wk opened 1 month ago

sydneyjodon-wk commented 1 month ago

Steps to reproduce

Link to live example: https://stackblitz.com/edit/react-d4b5ls?file=Demo.tsx

Steps:

  1. Set the onHighlightChange prop
  2. Check what is passed in for the event param

Current behavior

Hello 👋

I'm noticing that the onHighlightChange is typed as:

onHighlightChange?: (
    event: React.SyntheticEvent,
    option: Value | null,
    reason: AutocompleteHighlightChangeReason,
) => void;

with a non-null event param, but undefined seems to be coming through for the param. See demo:

autocomplete-undefined-event

Expected behavior

Would it be possible to change the typing of the prop to reflect the values that are being passed to event?

- event: React.SyntheticEvent,
+ event: React.SyntheticEvent | undefined,

This seems like it would be consistent with some other onHighlightChange props.

Context

We are trying to use strict null safety

Your environment

See codesandbox link

Search keywords: onHighlightChange, undefined event

ZeeshanTamboli commented 1 month ago

The onHighlightChange should only trigger when the highlighted option changes, not when the listbox opens. This seems to be a broader issue, as mentioned in #31383. Instead of changing the type, we should update the overall implementation. The types are correct that the event should always be present.