gab-social/app/javascript/gabsocial/components/search.js

115 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-02-21 00:57:29 +00:00
import {
changeSearch,
clearSearch,
submitSearch,
showSearch,
} from '../actions/search'
import Input from './input'
2020-04-11 23:29:19 +01:00
const mapStateToProps = (state) => ({
2020-02-21 00:57:29 +00:00
value: state.getIn(['search', 'value']),
submitted: state.getIn(['search', 'submitted']),
})
2020-04-11 23:29:19 +01:00
const mapDispatchToProps = (dispatch) => ({
onChange: (value) => dispatch(changeSearch(value)),
onClear: () => dispatch(clearSearch()),
onSubmit: () => dispatch(submitSearch()),
onShow: () => dispatch(showSearch()),
2020-02-21 00:57:29 +00:00
})
export default
@connect(mapStateToProps, mapDispatchToProps)
class Search extends PureComponent {
static contextTypes = {
router: PropTypes.object.isRequired,
}
static propTypes = {
value: PropTypes.string.isRequired,
submitted: PropTypes.bool,
onShow: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onKeyUp: PropTypes.func.isRequired,
withOverlay: PropTypes.bool,
handleClear: PropTypes.func.isRequired,
2020-03-14 17:31:29 +00:00
onSubmit: PropTypes.func.isRequired,
2020-02-21 00:57:29 +00:00
}
state = {
expanded: false,
}
2020-03-14 17:31:29 +00:00
textbox = React.createRef()
2020-02-21 00:57:29 +00:00
handleChange = (e) => {
this.props.onChange(e.target.value)
}
handleFocus = () => {
this.setState({ expanded: true })
this.props.onShow()
}
handleBlur = () => {
this.setState({ expanded: false })
}
2020-03-14 17:31:29 +00:00
handleKeyUp = (e) => {
const { value } = this.props
if (e.key === 'Enter') {
e.preventDefault();
this.props.onSubmit();
this.context.router.history.push(`/search?q=${value}`);
} else if (e.key === 'Escape') {
this.textbox.blur()
}
}
setTextbox = n => {
this.textbox = n
}
2020-02-21 00:57:29 +00:00
render() {
2020-03-14 17:31:29 +00:00
const {
value,
submitted,
handleClear,
withOverlay
} = this.props
2020-02-21 00:57:29 +00:00
const { expanded } = this.state
const hasValue = value ? value.length > 0 || submitted : 0
return (
<div className={[_s.default, _s.justifyContentCenter, _s.height53PX].join(' ')}>
<Input
hasClear
value={value}
2020-03-14 17:31:29 +00:00
inputRef={this.setTextbox}
2020-02-21 00:57:29 +00:00
prependIcon='search'
placeholder='Search on Gab...'
onChange={this.handleChange}
2020-03-14 17:31:29 +00:00
onKeyUp={this.handleKeyUp}
2020-02-21 00:57:29 +00:00
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onClear={handleClear}
/>
{
withOverlay &&
2020-03-06 15:38:22 +00:00
{/*<Overlay show={expanded && !hasValue} placement='bottom' target={this}>
2020-02-21 00:57:29 +00:00
<SearchPopout />
2020-03-06 15:38:22 +00:00
</Overlay>*/}
2020-02-21 00:57:29 +00:00
}
</div>
)
}
}