tootlab-mastodon/app/javascript/flavours/glitch/features/compose/search/index.js

127 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-12-26 19:54:28 -05:00
// Package imports.
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import { connect } from 'react-redux';
2017-12-26 19:54:28 -05:00
import {
injectIntl,
2017-12-26 19:54:28 -05:00
defineMessages,
} from 'react-intl';
import Overlay from 'react-overlays/lib/Overlay';
// Components.
import Icon from 'flavours/glitch/components/icon';
import DrawerSearchPopout from './popout';
// Utils.
import { focusRoot } from 'flavours/glitch/util/dom_helpers';
// Messages.
const messages = defineMessages({
placeholder: {
defaultMessage: 'Search',
id: 'search.placeholder',
},
});
// The component.
export default @injectIntl
class DrawerSearch extends React.PureComponent {
2017-12-26 19:54:28 -05:00
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,
intl: PropTypes.object.isRequired,
};
state = {
expanded: false,
};
2017-12-26 19:54:28 -05:00
handleChange = (e) => {
2017-12-26 19:54:28 -05:00
const { onChange } = this.props;
if (onChange) {
onChange(e.target.value);
2017-12-26 19:54:28 -05:00
}
}
2017-12-26 19:54:28 -05:00
handleClear = (e) => {
2017-12-26 19:54:28 -05:00
const {
onClear,
submitted,
2018-01-06 18:34:01 -05:00
value,
2017-12-26 19:54:28 -05:00
} = this.props;
e.preventDefault(); // Prevents focus change ??
2018-01-06 18:34:01 -05:00
if (onClear && (submitted || value && value.length)) {
2017-12-26 19:54:28 -05:00
onClear();
}
}
handleBlur () {
this.setState({ expanded: false });
}
2017-12-26 19:54:28 -05:00
handleFocus = () => {
2017-12-26 19:54:28 -05:00
const { onShow } = this.props;
this.setState({ expanded: true });
if (onShow) {
onShow();
}
}
2017-12-26 19:54:28 -05:00
handleKeyUp = (e) => {
2017-12-26 19:54:28 -05:00
const { onSubmit } = this.props;
switch (e.key) {
case 'Enter':
if (onSubmit) {
onSubmit();
}
break;
case 'Escape':
focusRoot();
}
}
render () {
const { intl, value, submitted } = this.props;
2017-12-26 19:54:28 -05:00
const { expanded } = this.state;
const active = value.length > 0 || submitted;
2018-01-06 18:34:01 -05:00
const computedClass = classNames('drawer--search', { active });
2017-12-26 19:54:28 -05:00
return (
<div className={computedClass}>
<label>
<span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
2017-12-26 19:54:28 -05:00
<input
type='text'
placeholder={intl.formatMessage(messages.placeholder)}
value={value || ''}
onChange={this.handleChange}
onKeyUp={this.handleKeyUp}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
2017-12-26 19:54:28 -05:00
/>
</label>
<div
aria-label={intl.formatMessage(messages.placeholder)}
className='icon'
onClick={this.handleClear}
2017-12-26 19:54:28 -05:00
role='button'
tabIndex='0'
>
<Icon icon='search' />
2018-01-06 18:34:01 -05:00
<Icon icon='times-circle' />
2017-12-26 19:54:28 -05:00
</div>
<Overlay show={expanded && !active} placement='bottom' target={this}>
<DrawerSearchPopout />
</Overlay>
2017-12-26 19:54:28 -05:00
</div>
);
}
}