rat*_*kin 7 autocomplete reactjs material-ui
我正在遵循https://material-ui.com/components/autocomplete/ 中的各种示例来创建自定义自动完成。我正在尝试使用该renderInput属性来使用自定义输入组件。我发现的所有示例都使用该TextField组件,但我想使用常规input组件。
问题是,这些选项永远不会显示。我已经在这里创造一个示范(互换renderInput使用renderInputWORKING,看看工作版本):
https://codesandbox.io/s/epic-johnson-oxy7b?file=/src/App.tsx
使用以下代码renderInput:
const renderInput = (params: AutocompleteRenderInputParams) => {
console.log(params);
const { InputLabelProps, inputProps, InputProps } = params;
return (
<div>
<label {...InputLabelProps}>foo</label>
<input {...InputProps} {...inputProps} />
</div>
);
};
Run Code Online (Sandbox Code Playgroud)
如何将<input />组件用于renderInputprop on <Autocomplete />?
Rya*_*ell 13
更新
Material-UI 的 4.10.1 版本(2020 年 6 月 1 日)在文档中包含了一个新示例,用于此确切案例:https : //material-ui.com/components/autocomplete/#custom-input。
拉取请求:https : //github.com/mui-org/material-ui/pull/21257
看最有用的例子在文档中的自定义自动完成例子,它使用InputBase的不是TextField。此示例包含以下代码renderInput:
renderInput={(params) => (
<InputBase
ref={params.InputProps.ref}
inputProps={params.inputProps}
autoFocus
className={classes.inputBase}
/>
)}
Run Code Online (Sandbox Code Playgroud)
将InputProps传递给TextField被放置在包裹一个div <input>,所以大部分的道具是不恰当的,直接把上<input>为你的元素。在上面来自文档示例的代码中,您可以看到它只使用了一个东西,params.InputProps即ref. 此 ref用于控制选项列表框的锚元素。一个 ref 也被放置在它<input>自己上,但它ref用于非常不同的目的。使用您的代码,只有其中一个被使用。
下面是一个工作示例(基于组合框示例,因为您的沙箱有许多其他与此问题没有直接关系的自定义设置),它使用<input>代替TextField:
import React from "react";
import Autocomplete from "@material-ui/lab/Autocomplete";
export default function ComboBox() {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={option => option.title}
style={{ width: 300 }}
renderInput={params => (
<div ref={params.InputProps.ref}>
<label {...params.InputLabelProps}>My Label </label>
<input {...params.inputProps} autoFocus />
</div>
)}
/>
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9603 次 |
| 最近记录: |