简体   繁体   English

如何获取 Material UI 'Autocomplete' 组件的名称?

[英]How to get the name of Material UI 'Autocomplete' component?

I'm working with multiple Autocomplete MUI components and currently trying to write a "generic" event handler that will call useReducer hook to store the state.我正在使用多个自动完成MUI 组件,目前正在尝试编写一个“通用”事件处理程序,该处理程序将调用useReducer挂钩来存储 state。

The problem is that in Autocomplete docs, onChange function looks like the following:问题是在Autocomplete文档中,onChange function 如下所示:

function(event: object, value: T | T[], reason: string) => void

I'm trying to get the name of a field from the event object (to determine what Autocomplete is being changed), but event.target.name and event.currentTarget.name are not working.我正在尝试从事件 object 中获取字段的名称(以确定正在更改的自动完成),但event.target.nameevent.currentTarget.name不起作用。

Are there are any ways to retrieve the name of a component that was changed?是否有任何方法可以检索已更改组件的名称?

The reason you get undefined is that the event.target in the onChange points to the li but the MaterialUi Autocomplete will add the name to the outer div .您未定义的原因是onChange中的event.target指向li但 MaterialUi Autocomplete 会将name添加到外部div So, there is no straight forward way.所以,没有直接的方法。 You can use a ref and use getAttribute to get the name.您可以使用ref并使用getAttribute来获取名称。

编辑材质自动选择获取名称属性

Code snippet代码片段

export default function ComboBox() {
  const ref0 = useRef();

  return (
    <Autocomplete
      id="combo-box-demo"
      ref={ref0}
      name="movies"
      options={top100Films}
      getOptionLabel={option => option.title}
      onInputChange={(e, v, r) => {
        const ev = e.target;
        if (r === "reset") console.log(ev, v, r);
      }}
      onChange={(e, v, r) => {
        console.log(ref0.current.getAttribute("name"));
      }}
      style={{ width: 300 }}
      renderInput={params => (
        <TextField
          name="auto-input"
          {...params}
          label="Combo box"
          variant="outlined"
        />
      )}
    />
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 Material-ui 自动补全组件中新增的 object - Get newly added object in Material-ui Autocomplete component 当为 Material UI Autocomplete React 组件选择任何选项时,如何获得事件? - How do I get an event when any option is selected for a Material UI Autocomplete React component? Material UI -Autocomplete如何获取event.type或额外的属性名称? - Material UI -Autocomplete how to get event.type or an extra attribute name? 如何在 Material UI 自动完成组件中捕获选项选择事件? - How to capture option selected event in Material UI autocomplete component? Reactjs:如何将名称、值传递给 Material ui 自动完成? - Reactjs: How to pass name, value to Material ui autocomplete? 如何在组件中获取材料 ui 选择器的值? - How to get material ui pickers value in component? 如何使用 Material-UI 的自动完成功能获得“以选项为中心”的事件? - How to get an "option focused" event with Material-UI's Autocomplete? 如何获取 material-ui 的自动完成 Select 的值? - How can I get the value of the Autocomplete Select of material-ui? 以编程方式使 Material UI 自动完成获得焦点 - Make Material UI Autocomplete get focus programatically 如何使用 Material-UI 自动完成组件到 select 多个选项并添加新选项? - How to use Material-UI Autocomplete component to select multiple options and add new options?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM