2017-12-27 09:54:28 +09:00
|
|
|
// Package imports.
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2019-04-20 03:57:43 +09:00
|
|
|
import { connect } from 'react-redux';
|
2017-12-27 09:54:28 +09:00
|
|
|
import {
|
2019-04-20 03:57:43 +09:00
|
|
|
injectIntl,
|
2017-12-27 09:54:28 +09: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',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-04-20 03:57:43 +09:00
|
|
|
// The component.
|
|
|
|
export default @injectIntl
|
|
|
|
class DrawerSearch extends React.PureComponent {
|
2017-12-27 09:54:28 +09:00
|
|
|
|
2019-04-20 03:57:43 +09: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-27 09:54:28 +09:00
|
|
|
|
2019-04-20 03:57:43 +09:00
|
|
|
handleChange = (e) => {
|
2017-12-27 09:54:28 +09:00
|
|
|
const { onChange } = this.props;
|
|
|
|
if (onChange) {
|
2019-04-20 03:57:43 +09:00
|
|
|
onChange(e.target.value);
|
2017-12-27 09:54:28 +09:00
|
|
|
}
|
2019-04-20 03:57:43 +09:00
|
|
|
}
|
2017-12-27 09:54:28 +09:00
|
|
|
|
2019-04-20 03:57:43 +09:00
|
|
|
handleClear = (e) => {
|
2017-12-27 09:54:28 +09:00
|
|
|
const {
|
|
|
|
onClear,
|
|
|
|
submitted,
|
2018-01-07 08:34:01 +09:00
|
|
|
value,
|
2017-12-27 09:54:28 +09:00
|
|
|
} = this.props;
|
|
|
|
e.preventDefault(); // Prevents focus change ??
|
2018-01-07 08:34:01 +09:00
|
|
|
if (onClear && (submitted || value && value.length)) {
|
2017-12-27 09:54:28 +09:00
|
|
|
onClear();
|
|
|
|
}
|
2019-04-20 03:57:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
handleBlur () {
|
|
|
|
this.setState({ expanded: false });
|
|
|
|
}
|
2017-12-27 09:54:28 +09:00
|
|
|
|
2019-04-20 03:57:43 +09:00
|
|
|
handleFocus = () => {
|
2017-12-27 09:54:28 +09:00
|
|
|
const { onShow } = this.props;
|
|
|
|
this.setState({ expanded: true });
|
|
|
|
if (onShow) {
|
|
|
|
onShow();
|
|
|
|
}
|
2019-04-20 03:57:43 +09:00
|
|
|
}
|
2017-12-27 09:54:28 +09:00
|
|
|
|
2019-04-20 03:57:43 +09:00
|
|
|
handleKeyUp = (e) => {
|
2017-12-27 09:54:28 +09:00
|
|
|
const { onSubmit } = this.props;
|
|
|
|
switch (e.key) {
|
|
|
|
case 'Enter':
|
|
|
|
if (onSubmit) {
|
|
|
|
onSubmit();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
focusRoot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2019-04-20 03:57:43 +09:00
|
|
|
const { intl, value, submitted } = this.props;
|
2017-12-27 09:54:28 +09:00
|
|
|
const { expanded } = this.state;
|
2019-04-20 03:57:43 +09:00
|
|
|
const active = value.length > 0 || submitted;
|
2018-01-07 08:34:01 +09:00
|
|
|
const computedClass = classNames('drawer--search', { active });
|
2017-12-27 09:54:28 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={computedClass}>
|
|
|
|
<label>
|
2019-04-20 03:57:43 +09:00
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
|
2017-12-27 09:54:28 +09:00
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
2017-12-28 07:28:41 +09:00
|
|
|
value={value || ''}
|
2019-04-20 03:57:43 +09:00
|
|
|
onChange={this.handleChange}
|
|
|
|
onKeyUp={this.handleKeyUp}
|
|
|
|
onFocus={this.handleFocus}
|
|
|
|
onBlur={this.handleBlur}
|
2017-12-27 09:54:28 +09:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<div
|
|
|
|
aria-label={intl.formatMessage(messages.placeholder)}
|
|
|
|
className='icon'
|
2019-04-20 03:57:43 +09:00
|
|
|
onClick={this.handleClear}
|
2017-12-27 09:54:28 +09:00
|
|
|
role='button'
|
|
|
|
tabIndex='0'
|
|
|
|
>
|
|
|
|
<Icon icon='search' />
|
2018-01-07 08:34:01 +09:00
|
|
|
<Icon icon='times-circle' />
|
2017-12-27 09:54:28 +09:00
|
|
|
</div>
|
2019-04-20 03:57:43 +09:00
|
|
|
<Overlay show={expanded && !active} placement='bottom' target={this}>
|
|
|
|
<DrawerSearchPopout />
|
|
|
|
</Overlay>
|
2017-12-27 09:54:28 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|