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

88 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-08-13 16:54:29 +01:00
import classNames from 'classnames';
import Overlay from 'react-overlays/lib/Overlay';
import Icon from '../icon';
import SearchPopout from '../search_popout';
2019-08-13 16:54:29 +01:00
import './search.scss';
export default class Search extends PureComponent {
static contextTypes = {
router: PropTypes.object.isRequired,
};
static propTypes = {
value: PropTypes.string.isRequired,
submitted: PropTypes.bool,
onShow: PropTypes.func.isRequired,
openInRoute: PropTypes.bool,
2019-08-13 16:54:29 +01:00
placeholder: PropTypes.string.isRequired,
className: PropTypes.string,
searchTitle: PropTypes.string,
onChange: PropTypes.func.isRequired,
onKeyUp: PropTypes.func.isRequired,
handleSubmit: PropTypes.func,
withOverlay: PropTypes.bool,
handleClear: PropTypes.func.isRequired,
};
state = {
expanded: false,
};
handleFocus = () => {
this.setState({ expanded: true });
this.props.onShow();
}
handleBlur = () => {
this.setState({ expanded: false });
}
render() {
2019-08-13 16:54:29 +01:00
const { value, submitted, placeholder, className, searchTitle, onKeyUp, handleClear, handleSubmit, withOverlay, onChange } = this.props;
const { expanded } = this.state;
2019-08-13 16:54:29 +01:00
const hasValue = value.length > 0 || submitted;
2019-08-13 16:54:29 +01:00
const classes = classNames('search', className);
const iconClass = hasValue ? 'active' : '';
return (
2019-08-13 16:54:29 +01:00
<div className={classes}>
<label>
2019-08-13 16:54:29 +01:00
<span className='invisible'>{placeholder}</span>
<input
className='search__input'
type='text'
2019-08-13 16:54:29 +01:00
placeholder={placeholder}
value={value}
2019-08-13 16:54:29 +01:00
onChange={onChange}
onKeyUp={onKeyUp}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
/>
</label>
2019-08-13 16:54:29 +01:00
<div role='button' tabIndex='0' className='search__icon' onClick={handleClear}>
<Icon id='search' className={iconClass} />
<Icon id='times-circle' className={iconClass} aria-label={placeholder} />
</div>
2019-08-13 16:54:29 +01:00
{
withOverlay &&
<Overlay show={expanded && !hasValue} placement='bottom' target={this}>
<SearchPopout />
</Overlay>
}
{
(searchTitle && handleSubmit) &&
<Button onClick={handleSubmit}>{intl.formatMessage(messages.searchTitle)}</Button>
}
</div>
);
}
}