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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextField] Fix focused prop so it triggers focus state #990

Merged
merged 6 commits into from Feb 21, 2019

Conversation

amireynoso
Copy link
Contributor

WHY are these changes introduced?

Resolves #985

The component accepts a focused prop which should force the field to be focused. However, it is not currently working (take a look at the issue for a reduced test case).

WHAT is this pull request doing?

I based my changes in the <SearchField /> implementation (component and tests), which also accepts a focused prop and it works as expected. This is a breakdown of the changes:

  • It now takes the focused prop into consideration when setting the value for state.focus (used to be false by default)
  • Forces the input's focus state on componentDidMount() if the prop is true.
  • Forces the input's focus state (or blur state) on componentDidUpdate() in case the prop is toggled
  • Scoped the tests related to this prop into their own describe block
  • The current test claimed to be testing something it was not testing (nor doing): "focuses input and calls onFocus() when focused prop has been updated to true"
    • I refactored this test a little bit to test that the element is in focus when the prop is set to true
  • Added two new tests to check the prop can be toggled

How to 馃帺

馃枼 Local development instructions
馃棐 General tophatting guidelines
馃搫 Changelog guidelines

Copy-paste this code in playground/Playground.tsx:
import * as React from 'react';
import {Page, TextField} from '../src';

interface State {
  value: string;
}

export default class Playground extends React.Component<{}, State> {
  state = {
    value: 'Lorem ipsum',
  };

  handleChange = (value: string) => {
    this.setState({value});
  };

  render() {
    return (
      <Page title="Playground">
        <TextField
          label="Focused text field"
          value={this.state.value}
          onChange={this.handleChange}
          multiline
          focused
        />
      </Page>
    );
  }
}

馃帺 checklist

@BPScott BPScott temporarily deployed to polaris-react-pr-990 February 8, 2019 15:51 Inactive
@BPScott BPScott temporarily deployed to polaris-react-pr-990 February 12, 2019 15:07 Inactive
src/components/TextField/TextField.tsx Show resolved Hide resolved
this.props.focused === true
) {
componentDidMount() {
if (this.input && this.props.focused) {
Copy link
Member

Choose a reason for hiding this comment

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

The ref should always be available in componentDidMount? We should be able to omit that check. The JS styleguide also prefers early returns so we can probably change this to

if (!this.props.focused) return;
this.input.focus();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

componentDidUpdate({focused: wasFocused}: CombinedProps) {
const {focused} = this.props;

if (this.input) {
Copy link
Member

Choose a reason for hiding this comment

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

Same comment about the early return here as well

if (!wasFocused && focused) {
this.input.focus();
} else if (wasFocused && !focused) {
this.input.blur();
Copy link
Member

Choose a reason for hiding this comment

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

Should we blur when the props change? Using the focused prop forces focus on the textfield but omitting the prop doesn't necessarily mean they want the field to be blurred. What are your thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, the blur event would only trigger if the focused prop changed values and it wouldn't be triggered on mount if focused is false or not defined.

One use case is if focused is being set programatically based on an interaction with another component. If the focus is removed (programatically), I'd expect it to trigger the blur event.

I saw this on the <SearchField /> component and thought it was a nice addition.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me, how about you @solonaarmstrong ?

@BPScott BPScott temporarily deployed to polaris-react-pr-990 February 14, 2019 21:49 Inactive
@amireynoso
Copy link
Contributor Author

Hey @AndrewMusgrave, thanks for your review! I addressed the comments, would you mind taking another look?

@BPScott BPScott temporarily deployed to polaris-react-pr-990 February 20, 2019 14:39 Inactive
@amireynoso
Copy link
Contributor Author

@solonaarmstrong Hey Solona! Would you help me with a review of this PR? I wasn't sure who else to tag, feel free to tag someone else. Thank you!

@AndrewMusgrave
Copy link
Member

Sorry I already gave feedback so this doesn't show up on my review list anymore, I'll get to it later today 馃檹

@BPScott BPScott temporarily deployed to polaris-react-pr-990 February 20, 2019 17:46 Inactive
src/components/TextField/TextField.tsx Show resolved Hide resolved
UNRELEASED.md Outdated Show resolved Hide resolved
if (!wasFocused && focused) {
this.input.focus();
} else if (wasFocused && !focused) {
this.input.blur();
Copy link
Member

Choose a reason for hiding this comment

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

Works for me, how about you @solonaarmstrong ?

Copy link
Member

@AndrewMusgrave AndrewMusgrave left a comment

Choose a reason for hiding this comment

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

馃帺 馃挴 One comment about the changelog, looks good other than that. Thanks for fixing this 鉂わ笍

@solonaarmstrong-zz
Copy link

solonaarmstrong-zz commented Feb 20, 2019

What's the difference between focused and autoFocus? It looks as though autoFocus is the one that actually places the cursor in the field?

@AndrewMusgrave
Copy link
Member

AndrewMusgrave commented Feb 20, 2019

Autofocus will focus the textfield when it renders, focused maintains focus on the textfield

Co-Authored-By: amireynoso <amireynoso@users.noreply.github.com>
@BPScott BPScott temporarily deployed to polaris-react-pr-990 February 20, 2019 20:51 Inactive
@amireynoso amireynoso merged commit 53d003b into master Feb 21, 2019
@danrosenthal danrosenthal temporarily deployed to production February 21, 2019 18:40 Inactive
@kaelig kaelig deleted the 985-textfield-focus branch June 27, 2019 01:08
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.

None yet

5 participants