如何连接Algolia和@material-ui
How to connect Algolia and @material-ui
This is the documentation 了解如何仅从 Algolia 中的输入字段创建基本 SearchBox
元素。问题是,Algolia 最终看起来很丑
这就是 material-ui 的用武之地。我之前使用 AppBar
包含搜索元素,所以我的想法是在我的 [ 中实例化 SearchBox
=16=] 组件,但带有 material-ui 的专有 InputBase
(而不是无聊的 html input
)。
我将在下方粘贴目前已有的代码,但它拒绝使用 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);
(您可能会说,关于文档,我已经完全照本宣科了——我没有尝试任何特别的东西)
如果您想使用 material ui 搜索框组件而不是 Algolia 的搜索框组件,您可以使用 connectSearchBox();
同步 material ui 搜索框和 Algolia 搜索框 API.
import { InstantSearch, connectSearchBox } from "react-instantsearch-dom";
const CustomSearchBox = connectSearchBox(MaterialUISearchBox);
在 MaterialUISearchBox 组件中,您将使用 Algolia 提供的道具:currentRefinement
和 refine()
.
<InputBase
placeholder="Search…"
inputProps={{ "aria-label": "search" }}
value={this.props.currentRefinement}
onChange={(e) => this.props.refine(this.currentTarget.value)}
searchAsYouType={false}
/>
请查看 url 以了解有关 Algolia 自定义组件的更多详细信息。
https://www.algolia.com/doc/api-reference/widgets/search-box/react/
This is the documentation 了解如何仅从 Algolia 中的输入字段创建基本 SearchBox
元素。问题是,Algolia 最终看起来很丑
这就是 material-ui 的用武之地。我之前使用 AppBar
包含搜索元素,所以我的想法是在我的 [ 中实例化 SearchBox
=16=] 组件,但带有 material-ui 的专有 InputBase
(而不是无聊的 html input
)。
我将在下方粘贴目前已有的代码,但它拒绝使用 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);
(您可能会说,关于文档,我已经完全照本宣科了——我没有尝试任何特别的东西)
如果您想使用 material ui 搜索框组件而不是 Algolia 的搜索框组件,您可以使用 connectSearchBox();
同步 material ui 搜索框和 Algolia 搜索框 API.
import { InstantSearch, connectSearchBox } from "react-instantsearch-dom";
const CustomSearchBox = connectSearchBox(MaterialUISearchBox);
在 MaterialUISearchBox 组件中,您将使用 Algolia 提供的道具:currentRefinement
和 refine()
.
<InputBase
placeholder="Search…"
inputProps={{ "aria-label": "search" }}
value={this.props.currentRefinement}
onChange={(e) => this.props.refine(this.currentTarget.value)}
searchAsYouType={false}
/>
请查看 url 以了解有关 Algolia 自定义组件的更多详细信息。
https://www.algolia.com/doc/api-reference/widgets/search-box/react/