如何连接 Algolia 和 @material-ui

ark*_*k0n 4 components jsx reactjs algolia material-ui

这是有关如何SearchBox从 Algolia 中的输入字段创建基本元素的文档。问题是,Algolia 最终看起来相当丑陋

这就是material-ui 的用武之地。我之前使用过它包含一个搜索元素,所以我的想法是在我的组件中AppBar实例化,但使用material-ui 的专有(而不是无聊的 html )。SearchBoxAppBar.jsInputBaseinput

我将在下面粘贴到目前为止的代码,但它拒绝编译InputBase(更具体地说,它是关联的道具)用于创建自定义SearchBox元素。

如果有人有像这样整合不同 API 的经验,或者认为您可能知道发生了什么,请随时告诉我!

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/Appbar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import InputBase from '@material-ui/core/InputBase';
import {fade} from '@material-ui/core/styles/colorManipulator';
import {withStyles} from "@material-ui/core/styles";
import SearchIcon from '@material-ui/icons/Search';
import { connectSearchBox } from 'react-instantsearch-dom';

const styles = theme => ({
    root:{
        width: '100%',
    },
    grow:{
        flexGrow: 1,
    },
    menuButton:{
        marginLeft: -12,
        marginRight: 20,
    },
    title:{
        display: 'none',
        [theme.breakpoints.up('sm')]:{
            display: 'block',
        },
    },
    search:{
        position: 'relative',
        borderRadius: theme.shape.borderRadius,
        backgroundColor: fade(theme.palette.common.white, 0.15),
        '&:hover':{
            backgroundColor: fade(theme.palette.common.white, 0.25),
        },
        marginLeft: 0,
        width: '100%',
        [theme.breakpoints.up('sm')]:{
            marginLeft: theme.spacing.unit,
            width: 'auto',
        },
    },
    searchIcon:{
        width: theme.spacing.unit * 9,
        height: '100%',
        position: 'absolute',
        pointerEvents: 'none',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
    },
    inputRoot:{
        color: 'inherit',
        width: '100%',
    },
    inputInput:{
        paddingTop: theme.spacing.unit,
        paddingRight: theme.spacing.unit,
        paddingBottom: theme.spacing.unit,
        paddingLeft: theme.spacing.unit * 10,
        transition: theme.transitions.create('width'),
        width: '100%',
        [theme.breakpoints.up('sm')]:{
            width: 120,
            '&:focus':{
                width: 200,
            },
        },
    },
});

function SearchBox({currentRefinement, refine}, props){
    const {classes} = props;
    return(
        <InputBase
            type='search'
            value={currentRefinement}
            onChange={event => refine(event.currentTarget.value)}
            placeholder="Search for Destination by Name, State, and keywords..."
            classes={{
                root: classes.inputRoot,
                input: classes.inputInput,
            }}
        />
    );
}


const CustomSearchBox = connectSearchBox(SearchBox);

function SearchAppBar(props){
    const {classes} = props;
    return(
        <div className={classes.root}>
            <AppBar position="static">
                <Toolbar>
                    <Typography className={classes.title} variant="h6" color='inherit' noWrap>
                    title
                    </Typography>
                    <div className={classes.grow}/>
                    <div className={classes.search}>
                        <div className={classes.searchIcon}>
                            <SearchIcon/>
                        </div>
                        <CustomSearchBox/>
                    </div>
                </Toolbar>
            </AppBar>
        </div>
    );
}

SearchAppBar.propTypes = {
    classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(SearchAppBar);
Run Code Online (Sandbox Code Playgroud)

(正如你可能知道的那样,我在文档方面非常照章办事 - 我没有尝试任何特别的事情)

在此输入图像描述

小智 5

如果您想使用material ui 搜索框组件而不是Algolia 的,您可以使用connectSearchBox();同步material ui 搜索框和Algolia 搜索框API。

\n
import { InstantSearch, connectSearchBox } from "react-instantsearch-dom";\nconst CustomSearchBox = connectSearchBox(MaterialUISearchBox);\n
Run Code Online (Sandbox Code Playgroud)\n

在 MaterialUISearchBox 组件中,您将使用 Algolia 提供的 props:currentRefinementrefine()

\n
        <InputBase\n          placeholder="Search\xe2\x80\xa6"\n          inputProps={{ "aria-label": "search" }}\n          value={this.props.currentRefinement}\n          onChange={(e) => this.props.refine(this.currentTarget.value)}\n          searchAsYouType={false}\n        />\n
Run Code Online (Sandbox Code Playgroud)\n

请检查 url 以获取有关 Algolia 自定义组件的更多详细信息。

\n

https://www.algolia.com/doc/api-reference/widgets/search-box/react/

\n