Buk*_*Lau 35 reactjs material-ui
我想将Autocomplete组件用于输入标签。我正在尝试获取标签并将它们保存在状态中,以便稍后将它们保存在数据库中。我在反应中使用函数而不是类。我确实尝试过onChange,但没有得到任何结果。
<div style={{ width: 500 }}>
<Autocomplete
multiple
options={autoComplete}
filterSelectedOptions
getOptionLabel={(option) => option.tags}
renderInput={(params) => (
<TextField
className={classes.input}
{...params}
variant="outlined"
placeholder="Favorites"
margin="normal"
fullWidth
/>
)}
/>
</div>;
Run Code Online (Sandbox Code Playgroud)
小智 61
正如 Yuki 已经提到的,请确保您确实onChange正确使用了该功能。它接收两个参数。根据文档:
签名:
function(event: object, value: any) => void。
event: 回调的事件源
value: null(自动完成组件中的一个/多个值)。
下面是一个例子:
import React from 'react';
import Chip from '@material-ui/core/Chip';
import Autocomplete from '@material-ui/lab/Autocomplete';
import TextField from '@material-ui/core/TextField';
export default class Tags extends React.Component {
constructor(props) {
super(props);
this.state = {
tags: []
};
this.onTagsChange = this.onTagsChange.bind(this);
}
onTagsChange = (event, values) => {
this.setState({
tags: values
}, () => {
// This will output an array of objects
// given by Autocompelte options property.
console.log(this.state.tags);
});
}
render() {
return (
<div style={{ width: 500 }}>
<Autocomplete
multiple
options={top100Films}
getOptionLabel={option => option.title}
defaultValue={[top100Films[13]]}
onChange={this.onTagsChange}
renderInput={params => (
<TextField
{...params}
variant="standard"
label="Multiple values"
placeholder="Favorites"
margin="normal"
fullWidth
/>
)}
/>
</div>
);
}
}
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 },
{ title: '12 Angry Men', year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: 'Pulp Fiction', year: 1994 },
{ title: 'The Lord of the Rings: The Return of the King', year: 2003 },
{ title: 'The Good, the Bad and the Ugly', year: 1966 },
{ title: 'Fight Club', year: 1999 },
{ title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
{ title: 'Star Wars: Episode V - The Empire Strikes Back', year: 1980 },
{ title: 'Forrest Gump', year: 1994 },
{ title: 'Inception', year: 2010 },
];
Run Code Online (Sandbox Code Playgroud)
oye*_*aba 10
我需要在每次输入更改时点击我的 api 才能从后端获取我的标签!
如果您想在每次输入更改时获得建议的标签,请使用 Material-ui onInputChange!
this.state = {
// labels are temp, will change every time on auto complete
labels: [],
// these are the ones which will be send with content
selectedTags: [],
}
}
//to get the value on every input change
onInputChange(event,value){
console.log(value)
//response from api
.then((res) => {
this.setState({
labels: res
})
})
}
//to select input tags
onSelectTag(e, value) {
this.setState({
selectedTags: value
})
}
<Autocomplete
multiple
options={top100Films}
getOptionLabel={option => option.title}
onChange={this.onSelectTag} // click on the show tags
onInputChange={this.onInputChange} //** on every input change hitting my api**
filterSelectedOptions
renderInput={(params) => (
<TextField
{...params}
variant="standard"
label="Multiple values"
placeholder="Favorites"
margin="normal"
fullWidth
/>
Run Code Online (Sandbox Code Playgroud)
@德沃罗
如果有人在显示输入字段下拉列表中的选定项目时遇到问题,
我找到了一个解决方法,基本上,你必须为和绑定一个inputValueat 。onChageAutocompleteTextField
const [input, setInput] = useState('');
<Autocomplete
options={suggestions}
getOptionLabel={(option) => option}
inputValue={input}
onChange={(e,v) => setInput(v)}
style={{ width: 300 }}
renderInput={(params) => (
<TextField {...params} label="Combo box" onChange={({ target }) => setInput(target.value)} variant="outlined" fullWidth />
)}
/>
Run Code Online (Sandbox Code Playgroud)
小智 5
当我从自动完成中选择一个选项时,我想更新我的状态。我有一个管理所有输入的全局 onChange 处理程序
const {name, value } = event.target;
setTukio({
...tukio,
[name]: value,
});
Run Code Online (Sandbox Code Playgroud)
这会根据字段的名称动态更新对象。但是在自动完成上,名称返回空白。所以我将处理程序从 更改onChange为onSelect。然后创建一个单独的函数来处理更改,或者在我的情况下添加一个 if 语句来检查是否未传递名称。
// This one will set state for my onSelect handler of the autocomplete
if (!name) {
setTukio({
...tukio,
tags: value,
});
} else {
setTukio({
...tukio,
[name]: value,
});
}
Run Code Online (Sandbox Code Playgroud)
如果您只有一个自动完成功能,则上述方法有效。如果你有多个你可以传递一个像下面这样的自定义函数
<Autocomplete
options={tags}
getOptionLabel={option => option.tagName}
id="tags"
name="tags"
autoComplete
includeInputInList
onSelect={(event) => handleTag(event, 'tags')}
renderInput={(params) => <TextField {...params} hint="koo, ndama nyonya" label="Tags" margin="normal" />}
/>
// The handler
const handleTag = ({ target }, fieldName) => {
const { value } = target;
switch (fieldName) {
case 'tags':
console.log('Value ', value)
// Do your stuff here
break;
default:
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82086 次 |
| 最近记录: |