From a55e1c1d490dc4c71b3a53bc721a71c504f04a10 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Fri, 19 Apr 2019 20:57:43 +0200 Subject: [PATCH] Refactor a bit DrawerSearch to make it closer to upstream --- .../glitch/features/compose/search/index.js | 108 +++++++----------- 1 file changed, 41 insertions(+), 67 deletions(-) diff --git a/app/javascript/flavours/glitch/features/compose/search/index.js b/app/javascript/flavours/glitch/features/compose/search/index.js index 8cbb0906c3..8f9e19b7bc 100644 --- a/app/javascript/flavours/glitch/features/compose/search/index.js +++ b/app/javascript/flavours/glitch/features/compose/search/index.js @@ -2,8 +2,9 @@ import classNames from 'classnames'; import PropTypes from 'prop-types'; import React from 'react'; +import { connect } from 'react-redux'; import { - FormattedMessage, + injectIntl, defineMessages, } from 'react-intl'; import Overlay from 'react-overlays/lib/Overlay'; @@ -14,10 +15,6 @@ 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({ @@ -27,21 +24,32 @@ const messages = defineMessages({ }, }); -// Handlers. -const handlers = { +// The component. +export default @injectIntl +class DrawerSearch extends React.PureComponent { - handleBlur () { - this.setState({ expanded: false }); - }, + 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, + }; - handleChange ({ target: { value } }) { + state = { + expanded: false, + }; + + handleChange = (e) => { const { onChange } = this.props; if (onChange) { - onChange(value); + onChange(e.target.value); } - }, + } - handleClear (e) { + handleClear = (e) => { const { onClear, submitted, @@ -51,17 +59,21 @@ const handlers = { if (onClear && (submitted || value && value.length)) { onClear(); } - }, + } - handleFocus () { + handleBlur () { + this.setState({ expanded: false }); + } + + handleFocus = () => { const { onShow } = this.props; this.setState({ expanded: true }); if (onShow) { onShow(); } - }, + } - handleKeyUp (e) { + handleKeyUp = (e) => { const { onSubmit } = this.props; switch (e.key) { case 'Enter': @@ -72,81 +84,43 @@ const handlers = { case 'Escape': focusRoot(); } - }, -}; - -// The component. -export default class DrawerSearch extends React.PureComponent { - - // Constructor. - constructor (props) { - super(props); - assignHandlers(this, handlers); - this.state = { expanded: false }; } - // Rendering. render () { - const { - handleBlur, - handleChange, - handleClear, - handleFocus, - handleKeyUp, - } = this.handlers; - const { - intl, - submitted, - value, - } = this.props; + const { intl, value, submitted } = this.props; const { expanded } = this.state; - const active = value && value.length || submitted; + const active = value.length > 0 || submitted; const computedClass = classNames('drawer--search', { active }); return (
- + + +
); } } - -// Props. -DrawerSearch.propTypes = { - value: PropTypes.string, - submitted: PropTypes.bool, - onChange: PropTypes.func, - onSubmit: PropTypes.func, - onClear: PropTypes.func, - onShow: PropTypes.func, - intl: PropTypes.object, -};