2017-12-27 00:54:28 +00:00
|
|
|
// Package imports.
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import {
|
|
|
|
FormattedMessage,
|
|
|
|
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';
|
|
|
|
import {
|
|
|
|
assignHandlers,
|
|
|
|
hiddenComponent,
|
|
|
|
} from 'flavours/glitch/util/react_helpers';
|
|
|
|
|
|
|
|
// Messages.
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: {
|
|
|
|
defaultMessage: 'Search',
|
|
|
|
id: 'search.placeholder',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handlers.
|
|
|
|
const handlers = {
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
handleBlur () {
|
2017-12-27 00:54:28 +00:00
|
|
|
this.setState({ expanded: false });
|
|
|
|
},
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
handleChange ({ target: { value } }) {
|
2017-12-27 00:54:28 +00:00
|
|
|
const { onChange } = this.props;
|
|
|
|
if (onChange) {
|
|
|
|
onChange(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
handleClear (e) {
|
2017-12-27 00:54:28 +00:00
|
|
|
const {
|
|
|
|
onClear,
|
|
|
|
submitted,
|
2018-01-06 23:34:01 +00:00
|
|
|
value,
|
2017-12-27 00:54:28 +00:00
|
|
|
} = this.props;
|
|
|
|
e.preventDefault(); // Prevents focus change ??
|
2018-01-06 23:34:01 +00:00
|
|
|
if (onClear && (submitted || value && value.length)) {
|
2017-12-27 00:54:28 +00:00
|
|
|
onClear();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
handleFocus () {
|
2017-12-27 00:54:28 +00:00
|
|
|
const { onShow } = this.props;
|
|
|
|
this.setState({ expanded: true });
|
|
|
|
if (onShow) {
|
|
|
|
onShow();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
handleKeyUp (e) {
|
2017-12-27 00:54:28 +00:00
|
|
|
const { onSubmit } = this.props;
|
|
|
|
switch (e.key) {
|
|
|
|
case 'Enter':
|
|
|
|
if (onSubmit) {
|
|
|
|
onSubmit();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
focusRoot();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// The component.
|
|
|
|
export default class DrawerSearch extends React.PureComponent {
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
// Constructor.
|
2017-12-27 00:54:28 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
assignHandlers(this, handlers);
|
|
|
|
this.state = { expanded: false };
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
// Rendering.
|
2017-12-27 00:54:28 +00:00
|
|
|
render () {
|
|
|
|
const {
|
2018-01-03 20:36:21 +00:00
|
|
|
handleBlur,
|
|
|
|
handleChange,
|
|
|
|
handleClear,
|
|
|
|
handleFocus,
|
|
|
|
handleKeyUp,
|
2017-12-27 00:54:28 +00:00
|
|
|
} = this.handlers;
|
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
submitted,
|
|
|
|
value,
|
|
|
|
} = this.props;
|
|
|
|
const { expanded } = this.state;
|
2018-01-06 23:34:01 +00:00
|
|
|
const active = value && value.length || submitted;
|
|
|
|
const computedClass = classNames('drawer--search', { active });
|
2017-12-27 00:54:28 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={computedClass}>
|
|
|
|
<label>
|
|
|
|
<span {...hiddenComponent}>
|
|
|
|
<FormattedMessage {...messages.placeholder} />
|
|
|
|
</span>
|
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
2017-12-27 22:28:41 +00:00
|
|
|
value={value || ''}
|
2018-01-03 20:36:21 +00:00
|
|
|
onChange={handleChange}
|
|
|
|
onKeyUp={handleKeyUp}
|
|
|
|
onFocus={handleFocus}
|
|
|
|
onBlur={handleBlur}
|
2017-12-27 00:54:28 +00:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<div
|
|
|
|
aria-label={intl.formatMessage(messages.placeholder)}
|
|
|
|
className='icon'
|
2018-01-03 20:36:21 +00:00
|
|
|
onClick={handleClear}
|
2017-12-27 00:54:28 +00:00
|
|
|
role='button'
|
|
|
|
tabIndex='0'
|
|
|
|
>
|
|
|
|
<Icon icon='search' />
|
2018-01-06 23:34:01 +00:00
|
|
|
<Icon icon='times-circle' />
|
2017-12-27 00:54:28 +00:00
|
|
|
</div>
|
|
|
|
<Overlay
|
|
|
|
placement='bottom'
|
2018-01-06 23:34:01 +00:00
|
|
|
show={expanded && !active}
|
2017-12-27 00:54:28 +00:00
|
|
|
target={this}
|
|
|
|
><DrawerSearchPopout /></Overlay>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:36:21 +00:00
|
|
|
// Props.
|
2017-12-27 00:54:28 +00:00
|
|
|
DrawerSearch.propTypes = {
|
|
|
|
value: PropTypes.string,
|
|
|
|
submitted: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onSubmit: PropTypes.func,
|
|
|
|
onClear: PropTypes.func,
|
|
|
|
onShow: PropTypes.func,
|
|
|
|
intl: PropTypes.object,
|
|
|
|
};
|