【问题标题】:Material-UI CloseIcon not showing in AutocompleteMaterial-UI CloseIcon 未在自动完成中显示
【发布时间】: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


    【解决方案1】:

    我能够解决这个问题!我希望在您键入时显示关闭图标,并且我需要按照 Material-UI 文档传递 freeSolo 属性。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多