2020-03-27 13:59:38 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-10-31 20:59:29 -07:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2020-05-28 15:52:07 -07:00
|
|
|
import Icon from 'soapbox/components/icon';
|
2021-11-01 10:25:18 -07:00
|
|
|
import AutosuggestAccountInput from 'soapbox/components/autosuggest_account_input';
|
2021-09-27 18:47:44 -07:00
|
|
|
import classNames from 'classnames';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: { id: 'search.placeholder', defaultMessage: 'Search' },
|
|
|
|
});
|
|
|
|
|
|
|
|
export default @injectIntl
|
|
|
|
class Search extends React.PureComponent {
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
submitted: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
|
|
|
onClear: PropTypes.func.isRequired,
|
|
|
|
onShow: PropTypes.func.isRequired,
|
2021-11-01 10:25:18 -07:00
|
|
|
onSelected: PropTypes.func,
|
2020-03-27 13:59:38 -07:00
|
|
|
openInRoute: PropTypes.bool,
|
2021-10-14 10:38:52 -07:00
|
|
|
autoFocus: PropTypes.bool,
|
2021-11-01 10:25:18 -07:00
|
|
|
autosuggest: PropTypes.bool,
|
2020-03-27 13:59:38 -07:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2021-10-14 10:38:52 -07:00
|
|
|
static defaultProps = {
|
|
|
|
autoFocus: false,
|
2021-11-01 10:25:18 -07:00
|
|
|
ausosuggest: false,
|
2021-10-14 10:38:52 -07:00
|
|
|
}
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
state = {
|
|
|
|
expanded: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
this.props.onChange(e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClear = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (this.props.value.length > 0 || this.props.submitted) {
|
|
|
|
this.props.onClear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:25:18 -07:00
|
|
|
handleKeyDown = (e) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
if (e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.props.onSubmit();
|
|
|
|
|
|
|
|
if (this.props.openInRoute) {
|
|
|
|
this.context.router.history.push('/search');
|
|
|
|
}
|
|
|
|
} else if (e.key === 'Escape') {
|
|
|
|
document.querySelector('.ui').parentElement.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFocus = () => {
|
|
|
|
this.setState({ expanded: true });
|
|
|
|
this.props.onShow();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBlur = () => {
|
|
|
|
this.setState({ expanded: false });
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:25:18 -07:00
|
|
|
handleSelected = accountId => {
|
|
|
|
const { onSelected } = this.props;
|
|
|
|
|
|
|
|
if (onSelected) {
|
|
|
|
onSelected(accountId, this.context.router.history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
render() {
|
2021-11-01 10:25:18 -07:00
|
|
|
const { intl, value, autoFocus, autosuggest, submitted } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
const hasValue = value.length > 0 || submitted;
|
|
|
|
|
2021-11-01 10:25:18 -07:00
|
|
|
const Component = autosuggest ? AutosuggestAccountInput : 'input';
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
return (
|
|
|
|
<div className='search'>
|
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
|
2021-11-01 10:25:18 -07:00
|
|
|
<Component
|
2020-03-27 13:59:38 -07:00
|
|
|
className='search__input'
|
|
|
|
type='text'
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
|
|
|
value={value}
|
|
|
|
onChange={this.handleChange}
|
2021-11-01 10:25:18 -07:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2020-03-27 13:59:38 -07:00
|
|
|
onFocus={this.handleFocus}
|
|
|
|
onBlur={this.handleBlur}
|
2021-11-01 10:25:18 -07:00
|
|
|
onSelected={this.handleSelected}
|
2021-10-14 10:38:52 -07:00
|
|
|
autoFocus={autoFocus}
|
2021-11-01 10:25:18 -07:00
|
|
|
autoSelect={false}
|
2020-03-27 13:59:38 -07:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
|
2021-09-27 18:47:44 -07:00
|
|
|
<Icon src={require('@tabler/icons/icons/search.svg')} className={classNames('svg-icon--search', { active: !hasValue })} />
|
|
|
|
<Icon src={require('@tabler/icons/icons/backspace.svg')} className={classNames('svg-icon--backspace', { active: hasValue })} aria-label={intl.formatMessage(messages.placeholder)} />
|
2020-03-27 13:59:38 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|