Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace search field focus state with middleware #1914

Merged
merged 2 commits into from
Feb 20, 2020

Conversation

dmsnell
Copy link
Member

@dmsnell dmsnell commented Feb 19, 2020

Alternative idea to #1908

searchFocus isn't quite app state. We have been using it as a
workaround in order to trigger side-effects from Redux in order
to focus the search field.

In this patch we're removing that state altogether and replacing
it with a custom middleware specific to the search field. This too
is somewhat ugly because of the coupling between the middleware
and the search field component; it needs more thought on how best
to register the DOM node containing focus.

Nevertheless by removing the state property we are able to remove
more code and the update loop that was necessary to set focus,
update, an then record that the focus was already set or unset.

Storing a state value for focus doesn't match the reality that the
focus is a property of the DOM node and changes independently from
the Redux actions being dispatched in the app.

Alternative idea to #1908

`searchFocus` isn't quite app state. We have been using it as a
workaround in order to trigger side-effects from Redux in order
to focus the search field.

In this patch we're removing that state altogether and replacing
it with a custom middleware specific to the search field. This too
is somewhat ugly because of the coupling between the middleware
and the search field component; it needs more thought on how best
to register the DOM node containing focus.

Nevertheless by removing the `state` property we are able to remove
more code and the update loop that was necessary to set focus,
update, an then record that the focus was already set or unset.

Storing a state value for focus doesn't match the reality that the
focus is a property of the DOM node and changes independently from
the Redux actions being dispatched in the app.
@codebykat codebykat self-requested a review February 19, 2020 01:09
Copy link
Member

@codebykat codebykat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. It's less of a hack than the existing code (or my PR), works fine, and incidentally fixes a minor interaction bug (after selecting a suggested tag, the cursor is placed in the search field, but the text is now not selected, which is as it should be IMO).

@@ -18,20 +18,33 @@ export class SearchField extends Component<ConnectedProps> {

inputField = createRef<HTMLInputElement>();

componentDidUpdate() {
const { searchFocus, onSearchFocused } = this.props;
componentDidMount() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are making a mistake adding more componentDidMounts to the app, at very least this should be: UNSAFE_componentWillMount

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, let's merge this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad DidMount is fine.

@dmsnell dmsnell mentioned this pull request Feb 19, 2020
41 tasks
@belcherj belcherj merged commit f3218d4 into develop Feb 20, 2020
@belcherj belcherj deleted the refactor/replace-search-focus-state-with-middleware branch February 20, 2020 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants