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
Conversation
this.props.focused === true | ||
) { | ||
componentDidMount() { | ||
if (this.input && this.props.focused) { |
There was a problem hiding this comment.
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();
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
346a667
to
274b39e
Compare
Hey @AndrewMusgrave, thanks for your review! I addressed the comments, would you mind taking another look? |
@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! |
Sorry I already gave feedback so this doesn't show up on my review list anymore, I'll get to it later today 馃檹 |
if (!wasFocused && focused) { | ||
this.input.focus(); | ||
} else if (wasFocused && !focused) { | ||
this.input.blur(); |
There was a problem hiding this comment.
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 ?
There was a problem hiding this 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 鉂わ笍
What's the difference between |
|
Co-Authored-By: amireynoso <amireynoso@users.noreply.github.com>
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 afocused
prop and it works as expected. This is a breakdown of the changes:focused
prop into consideration when setting the value forstate.focus
(used to befalse
by default)componentDidMount()
if the prop istrue
.componentDidUpdate()
in case the prop is toggleddescribe
blocktrue
How to 馃帺
馃枼 Local development instructions
馃棐 General tophatting guidelines
馃搫 Changelog guidelines
Copy-paste this code in
playground/Playground.tsx
:馃帺 checklist