gpt4 book ai didi

reactjs - 带有图标的 Material ui自动完成

转载 作者:行者123 更新时间:2023-12-03 14:54:59 28 4
gpt4 key购买 nike

嗨,我正在尝试使用显示的文本旁边的图标来实现实质性的UI自动完成下拉框。
我的实现工作正常,但是当我选择其中一个选项时,它没有显示。
问题出在这部分代码上:

renderInput={params => (
<Fragment>
<TextField
{...params}
variant="outlined"
label="Select Account"
placeholder="Favorites"
margin="normal"
fullWidth
/>
</Fragment>

)}

如果我从 getOptionLabel中删除他的图标渲染,那么在选择所选文本时显示就很好了。
任何帮助将不胜感激。
现在,此代码的结果如下所示:
enter image description here
import React, { Fragment, useState } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import {makeStyles} from "@material-ui/core";
import Autocomplete from "@material-ui/lab/Autocomplete/Autocomplete";
import TextField from "@material-ui/core/TextField";
import FacebookIcon from '@material-ui/icons/Facebook';
import AppleIcon from '@material-ui/icons/Apple';
import IconButton from "@material-ui/core/IconButton";


const useStyles = makeStyles(theme => ({
Select: {
width: 425,
},
icon: {
color: '#0095e2'
},
}));

const SelectAccount = ({ clientAccountsData, accountSelected }) => {
const accountSelectedHandler = async (event, values) => {
if ( values !== null )
{
accountSelected(values);
}
else {
accountSelected('');
}
};

const renderCorrectAccountChannelIcon = (network_id) => {
if ( network_id=== '1' )
{
return (
<FacebookIcon/>
);
}
else if ( network_id=== '2' )
{
return (
<img
src={'/Icons/snapchat.png'}
height={30}
width={30}
/>
);
}
else if ( network_id=== '3' )
{
return (
<img
src={'/Icons/google.png'}
height={30}
width={30}
/>
);
}
else if ( network_id=== '4' )
{
return (
<AppleIcon/>
);
}
};

const classes = useStyles();
return (
<div className='material-ui'>
<Autocomplete
className={classes.Select}
id="account_select"
options={clientAccountsData}
onChange={accountSelectedHandler}
getOptionLabel={option =>
{
return(
<Fragment>
<Icon className={classes.icon}>
{
renderCorrectAccountChannelIcon(option.network_id)
}
</Icon>
{option.accountName + ' (' + option.accountId + ')'}
</Fragment>
);
}
}
filterSelectedOptions
renderInput={params => (
<Fragment>
<TextField
{...params}
variant="outlined"
label="Select Account"
placeholder="Favorites"
margin="normal"
fullWidth
/>
</Fragment>

)}
/>
</div>
);
};

SelectAccount.prototypes = {
accountSelected: PropTypes.func.isRequired,
clientAccountsData: PropTypes.array.isRequired,
};

const mapStateToProps = state => ({
clientAccountsData: state.client.clientAccountsData,
});

export default connect(mapStateToProps, {})(SelectAccount);

编辑!:
找到一个修复程序,我们需要使用renderOption来渲染图标+文本
并仅将getOptionLabel用于标签文本
它看起来像这样:
<Autocomplete
className={classes.Select}
id="account_select"
options={clientAccountsData}
onChange={accountSelectedHandler}
getOptionLabel={option => option.accountName + ' (' + option.accountNumber + ')'}
renderOption={option => {
return (
<Fragment>
<Icon className={classes.icon}>
{
renderCorrectAccountChannelIcon(option.network_id)
}
</Icon>
{option.accountName + ' (' + option.accountNumber + ')'}
</Fragment>
);
}}
filterSelectedOptions
renderInput={params => (
<Fragment>
<TextField
{...params}
variant="outlined"
label="Select Account"
placeholder="Favorites"
margin="normal"
fullWidth
/>
</Fragment>

)}
/>

最佳答案

我想分享该解决方案的新衍生产品,该解决方案基于文档(autocomplete demos)中的自动完成示例。也将图像保留在所选标签中。

<Autocomplete
multiple
limitTags={2}
id="multiple-limit-tags"
options={top100Films}
getOptionLabel={(option) => option.title}
defaultValue={[top100Films[13], top100Films[12], top100Films[11]]}
renderTags={options =>
{
return (
options.map(option =>
<Fragment>
<IconButton color="primary">
<img src={'../src/img/Tables.svg'}/> {/*Mock image, attribute in option*/}
</IconButton>
{option.title}
</Fragment>))

}}
renderOption={option => {
return (
<Fragment>
<IconButton color="primary">
<img src={'../src/img/Tables.svg'}/> {/*Mock image, attribute in option*/}
</IconButton>
{option.title}
</Fragment>
);
}}
renderInput={(params) => (
<TextField
{...params}
variant="outlined"
label="limitTags"
placeholder="Favorites"
/>
)}
/>
enter image description here

关于reactjs - 带有图标的 Material ui自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59406081/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com