2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-04-24 04:17:27 +01:00
|
|
|
import { withRouter } from 'react-router-dom'
|
|
|
|
import queryString from 'query-string'
|
2020-05-09 03:17:19 +01:00
|
|
|
import { me } from '../initial_state'
|
2020-04-29 23:32:49 +01:00
|
|
|
import { CX } from '../constants'
|
2020-02-21 00:57:29 +00:00
|
|
|
import {
|
|
|
|
changeSearch,
|
|
|
|
clearSearch,
|
|
|
|
submitSearch,
|
|
|
|
showSearch,
|
|
|
|
} from '../actions/search'
|
2020-04-29 23:32:49 +01:00
|
|
|
import Button from './button'
|
2020-02-21 00:57:29 +00:00
|
|
|
|
2020-08-17 21:07:16 +01:00
|
|
|
class Search extends React.PureComponent {
|
2020-02-21 00:57:29 +00:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
2020-04-29 23:32:49 +01:00
|
|
|
focused: false,
|
2020-02-21 00:57:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:31:29 +00:00
|
|
|
textbox = React.createRef()
|
|
|
|
|
2020-04-24 04:17:27 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
// If user navigates to different page, ensure tab bar item
|
|
|
|
// with this.props.to that is on location is set to active.
|
|
|
|
if (this.props.location !== prevProps.location) {
|
|
|
|
const isCurrent = this.props.to === this.props.location.pathname
|
|
|
|
|
|
|
|
if (this.state.isCurrent !== isCurrent) {
|
|
|
|
this.setState({ isCurrent })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
handleOnChange = (e) => {
|
|
|
|
this.props.onChange(e.target.value)
|
2020-02-21 00:57:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
handleOnFocus = () => {
|
|
|
|
this.setState({ focused: true })
|
2020-02-21 00:57:29 +00:00
|
|
|
this.props.onShow()
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
handleOnBlur = () => {
|
|
|
|
this.setState({ focused: false })
|
2020-02-21 00:57:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:31:29 +00:00
|
|
|
handleKeyUp = (e) => {
|
|
|
|
const { value } = this.props
|
|
|
|
|
|
|
|
if (e.key === 'Enter') {
|
2020-05-01 06:50:27 +01:00
|
|
|
e.preventDefault()
|
2020-03-14 17:31:29 +00:00
|
|
|
|
2020-05-01 06:50:27 +01:00
|
|
|
this.props.onSubmit()
|
|
|
|
this.context.router.history.push(`/search?q=${value}`)
|
2020-03-14 17:31:29 +00:00
|
|
|
|
|
|
|
} else if (e.key === 'Escape') {
|
|
|
|
this.textbox.blur()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTextbox = n => {
|
|
|
|
this.textbox = n
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
handleSubmit = () => {
|
|
|
|
this.props.onSubmit()
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:57:29 +00:00
|
|
|
render() {
|
2020-03-14 17:31:29 +00:00
|
|
|
const {
|
|
|
|
value,
|
|
|
|
submitted,
|
2020-04-23 07:13:29 +01:00
|
|
|
onClear,
|
2020-05-14 07:03:22 +01:00
|
|
|
isInNav,
|
|
|
|
theme,
|
2020-03-14 17:31:29 +00:00
|
|
|
} = this.props
|
2020-04-29 23:32:49 +01:00
|
|
|
const { focused } = this.state
|
|
|
|
const highlighted = focused || `${value}`.length > 0
|
|
|
|
|
|
|
|
const inputClasses = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-04-29 23:32:49 +01:00
|
|
|
text: 1,
|
|
|
|
outlineNone: 1,
|
|
|
|
lineHeight125: 1,
|
|
|
|
displayBlock: 1,
|
|
|
|
py7: 1,
|
2020-05-14 07:03:22 +01:00
|
|
|
bgTransparent: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
cPrimary: 1,
|
2020-04-29 23:32:49 +01:00
|
|
|
fs14PX: 1,
|
|
|
|
flexGrow1: 1,
|
|
|
|
radiusSmall: 1,
|
|
|
|
pl15: 1,
|
|
|
|
searchInput: 1,
|
|
|
|
})
|
|
|
|
|
2020-11-03 21:12:24 +00:00
|
|
|
const isLight = ['light', 'white'].indexOf(theme) > -1
|
|
|
|
|
2020-04-29 23:32:49 +01:00
|
|
|
const containerClasses = CX({
|
2020-08-18 21:49:11 +01:00
|
|
|
d: 1,
|
2020-11-03 21:12:24 +00:00
|
|
|
searchNavigation: (!highlighted && isInNav && isLight) || (isInNav && theme !== 'light'),
|
2020-05-14 07:03:22 +01:00
|
|
|
bgWhite: (highlighted && isInNav && theme === 'light'),
|
2020-05-14 21:45:39 +01:00
|
|
|
bgPrimary: !isInNav,
|
2020-04-29 23:32:49 +01:00
|
|
|
flexRow: 1,
|
|
|
|
radiusSmall: 1,
|
2020-08-18 21:43:06 +01:00
|
|
|
aiCenter: 1,
|
2020-04-29 23:32:49 +01:00
|
|
|
})
|
|
|
|
|
2020-11-03 21:12:24 +00:00
|
|
|
const prependIconColor = (highlighted || !isInNav || theme === 'white') ? 'brand' : 'white'
|
2020-10-30 04:31:01 +00:00
|
|
|
const placeholder = !me ? 'Search Gab' : 'Search for people, groups or gabs'
|
2020-04-29 23:32:49 +01:00
|
|
|
const id = 'nav-search'
|
|
|
|
|
2020-02-21 00:57:29 +00:00
|
|
|
return (
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.jcCenter, _s.h53PX].join(' ')}>
|
2020-04-29 23:32:49 +01:00
|
|
|
<div className={containerClasses}>
|
|
|
|
<label className={_s.visiblyHidden} htmlFor={id}>Search</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
id={id}
|
|
|
|
className={inputClasses}
|
2020-10-29 04:22:51 +00:00
|
|
|
type='search'
|
2020-05-09 03:17:19 +01:00
|
|
|
placeholder={placeholder}
|
2020-04-29 23:32:49 +01:00
|
|
|
ref={this.setTextbox}
|
|
|
|
value={value}
|
|
|
|
onKeyUp={this.handleKeyUp}
|
|
|
|
onChange={this.handleOnChange}
|
|
|
|
onFocus={this.handleOnFocus}
|
|
|
|
onBlur={this.handleOnBlur}
|
2020-05-03 06:22:49 +01:00
|
|
|
autoComplete='off'
|
2020-04-29 23:32:49 +01:00
|
|
|
/>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
className={[_s.px10, _s.mr5].join(' ')}
|
|
|
|
tabIndex='0'
|
|
|
|
title='...'
|
|
|
|
backgroundColor='none'
|
|
|
|
color={prependIconColor}
|
|
|
|
onClick={this.handleSubmit}
|
|
|
|
icon='search'
|
2020-05-14 07:03:22 +01:00
|
|
|
iconClassName={_s.fillInherit}
|
2020-04-29 23:32:49 +01:00
|
|
|
iconSize='16px'
|
|
|
|
/>
|
|
|
|
</div>
|
2020-02-21 00:57:29 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-18 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
value: state.getIn(['search', 'value']),
|
|
|
|
submitted: state.getIn(['search', 'submitted']),
|
|
|
|
theme: state.getIn(['settings', 'displayOptions', 'theme']),
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onChange: (value) => dispatch(changeSearch(value)),
|
|
|
|
onClear: () => dispatch(clearSearch()),
|
|
|
|
onSubmit: () => dispatch(submitSearch()),
|
|
|
|
onShow: () => dispatch(showSearch()),
|
|
|
|
})
|
|
|
|
|
|
|
|
Search.propTypes = {
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
submitted: PropTypes.bool,
|
|
|
|
onShow: PropTypes.func.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
withOverlay: PropTypes.bool,
|
|
|
|
onClear: PropTypes.func.isRequired,
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
|
|
|
isInNav: PropTypes.bool.isRequired,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Search))
|