【发布时间】:2021-06-05 17:09:41
【问题描述】:
我正在尝试使用 Material-UI 使用 Autocomplete 组件,由于某种原因,当我更新某些文本时,关闭图标不显示。
我通过显示拆分自动完成组件,并使用一个实际在文本更改时发出 API 请求的包装器。我已尝试指定 closeIcon 属性,但无法显示。
显示组件:
import React from 'react';
import { faPodcast, faSearch, faUser } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Grid } from '@material-ui/core';
import Box from '@material-ui/core/Box';
import InputAdornment from '@material-ui/core/InputAdornment';
import TextField from '@material-ui/core/TextField';
import Typography from '@material-ui/core/Typography';
import { Autocomplete } from '@material-ui/lab';
function SearchBarDisplay({ options = [], onChange, onSelectValue, text }) {
function getOptionLabel(option) {
if (option.name) {
return option.name;
} else if (option.username) {
return option.username;
} else if (option.type === 'advanced') {
return option.text;
} else {
return null;
}
}
function renderOption(name, username, type) {
if (name) {
return (
<Grid container alignItems="center" wrap="nowrap">
<Box mr={1}>
<FontAwesomeIcon icon={faPodcast} />
</Box>
<Typography noWrap>{name}</Typography>
</Grid>
);
} else if (username) {
return (
<Grid container alignItems="center" wrap="nowrap">
<Box mr={1}>
<FontAwesomeIcon icon={faUser} />
</Box>
<Typography noWrap>{username}</Typography>
</Grid>
);
} else if (type === 'advanced') {
return (
<Grid container alignItems="center" wrap="nowrap">
<Box mr={1}>
<FontAwesomeIcon icon={faSearch} />
</Box>
<Typography
noWrap={true}
color="textSecondary">{`See more results for "${text}"`}</Typography>
</Grid>
);
} else {
return null;
}
}
return (
<Autocomplete
id="autocomplete"
options={text.replace(/\s/g, '').length ? options : []}
popupIcon={null}
getOptionSelected={(option, value) => option._id === value._id}
getOptionLabel={(option) => getOptionLabel(option)}
onChange={(event, value) => onSelectValue(value)}
onInputChange={(event, value) => onChange(value)}
renderOption={({ name, username, type }) => renderOption(name, username, type)}
renderInput={(params) => (
<TextField
{...params}
placeholder="Search for podcasts or TapTapers"
margin="normal"
variant="outlined"
InputProps={{
...params.InputProps,
startAdornment: (
<InputAdornment position="start">
<FontAwesomeIcon icon={faSearch} />
</InputAdornment>
)
}}
/>
)}
/>
);
}
export default SearchBarDisplay;
发出 API 请求并传入选项/当前文本值的包装器:
import React, { useEffect, useMemo, useState } from 'react';
import axios from 'axios';
import { debounce } from 'lodash';
import { useRouter } from 'next/router';
import SearchBarDisplay from './display';
function SearchBar() {
const router = useRouter();
const [options, setOptions] = useState([]);
const [text, setText] = useState('');
async function getSearch(text) {
await axios.get(`/api/search/${text}`).then(({ data }) => {
if (Object.keys(data).length === 0 || text.trim().length === 0) {
setOptions([]);
} else {
setOptions([...data, ...[{ type: 'advanced', text: text }]]);
}
});
}
function routeOnSubmit(value) {
const { username, _id, name, type, text } = value || {};
if (username) {
router.push({
pathname: '/users/[id]',
query: { id: _id }
});
} else if (name) {
router.push({
pathname: '/podcasts/[id]',
query: { id: _id }
});
} else if (type === 'advanced') {
router.push({
pathname: 'search/advanced/[text]',
query: { text }
});
}
}
const debouncedGetSearch = useMemo(() => debounce(getSearch, 500), []);
useEffect(() => {
if (text.replace(/\s/g, '').length) {
debouncedGetSearch(text);
} else {
setOptions([]);
}
}, [text, debouncedGetSearch]);
return (
<SearchBarDisplay
options={options}
onSelectValue={(value) => routeOnSubmit(value)}
text={text}
onChange={(value) => setText(value)}
/>
);
}
export default SearchBar;
【问题讨论】:
标签: reactjs material-ui