2

我一直试图让语义 ui 搜索结果出现,但对于我的一生,我在搜索时得到的只是:

如您所见,搜索结果不可见

我已经在我的 index.js 文件中导入了 semantic-ui-css:

import "semantic-ui-css/semantic.min.css";

我正在使用一个超级基础的数据集来测试:

const cities = [
    {
        key: 0,
        city: "New York City",
        usState: "New York"
    },
    {
        key: 1,
        city: "San Francisco",
        usState: "California"
    },
    {
        key: 2,
        city: "Chicago",
        usState: "Illinois"
    }
];

(这个数组出现在导入之后,但包含在上面以使代码部分更短)

并且正在使用文档中的标准搜索

import React from "react";
import { Search } from "semantic-ui-react";
import _ from "lodash";

class SearchBox extends React.Component {
    state = {
        isLoading: false,
        results: [],
        value: ""
    };

    componentWillMount() {
        this.resetComponent();
    }

    resetComponent = () =>
        this.setState({ isLoading: false, results: [], value: "" });

    handleSearchChange = (e, { value }) => {
        this.setState({ isLoading: true, value });

        setTimeout(() => {
            if (this.state.value.length < 1) this.resetComponent();

            const re = new RegExp(_.escapeRegExp(this.state.value), "i");
            const isMatch = result => re.test(result.city);

            this.setState({
                isLoading: false,
                results: _.filter(cities, isMatch)
            });
        }, 500);
    };

    handleResultSelect = (e, { result }) =>
        this.setState({ value: result.city });

    render() {
        const { isLoading, results, value } = this.state;
        return (
            <div>
                <Search
                    type="text"
                    size="big"
                    loading={isLoading}
                    results={results}
                    value={value}
                    onSearchChange={this.handleSearchChange}
                    onResultSelect={this.handleResultSelect}
                />
            </div>
        );
    }
}

export default SearchBox;

在编辑语义用户界面反应搜索示例时,我什至尝试使用我的数组作为数据,但由于某种原因,结果在那里也不可见。如何使搜索结果可见?

提前感谢您的任何评论或答案!

4

1 回答 1

3

尝试检查 SearchResult 的正确道具,因为默认 SearchResult 组件应使用具有以下键之一的对象:标题、描述、价格、图像

见 [ https://react.semantic-ui.com/modules/search#search-example-standard][1]

于 2017-11-23T16:54:58.023 回答