2022-01-10 14:17:52 -08:00
|
|
|
import classNames from 'classnames';
|
2021-10-14 09:47:10 -07:00
|
|
|
import PropTypes from 'prop-types';
|
2022-01-10 14:17:52 -08:00
|
|
|
import React from 'react';
|
2021-10-14 09:47:10 -07:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2021-10-14 09:47:10 -07:00
|
|
|
import AutosuggestAccountInput from 'soapbox/components/autosuggest_account_input';
|
2022-01-10 14:17:52 -08:00
|
|
|
import Icon from 'soapbox/components/icon';
|
2021-10-14 09:47:10 -07:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: { id: 'account_search.placeholder', defaultMessage: 'Search for an account' },
|
|
|
|
});
|
|
|
|
|
|
|
|
export default @injectIntl
|
|
|
|
class AccountSearch extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
onSelected: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
value: '',
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty = () => {
|
|
|
|
const { value } = this.state;
|
|
|
|
return !(value.length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearState = () => {
|
|
|
|
this.setState({ value: '' });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = ({ target }) => {
|
|
|
|
this.setState({ value: target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelected = accountId => {
|
|
|
|
this.clearState();
|
|
|
|
this.props.onSelected(accountId);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClear = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (!this.isEmpty()) {
|
|
|
|
this.setState({ value: '' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = e => {
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
document.querySelector('.ui').parentElement.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { intl, onSelected, ...rest } = this.props;
|
|
|
|
const { value } = this.state;
|
|
|
|
const isEmpty = this.isEmpty();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='search search--account'>
|
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
|
|
|
|
<AutosuggestAccountInput
|
|
|
|
className='search__input'
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
|
|
|
value={value}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onSelected={this.handleSelected}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
{...rest}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
|
|
|
|
<Icon src={require('@tabler/icons/icons/search.svg')} className={classNames('svg-icon--search', { active: isEmpty })} />
|
|
|
|
<Icon src={require('@tabler/icons/icons/backspace.svg')} className={classNames('svg-icon--backspace', { active: !isEmpty })} aria-label={intl.formatMessage(messages.placeholder)} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|