-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix: UrlInput autoFocus Issue With Custom Block #6197
Fix: UrlInput autoFocus Issue With Custom Block #6197
Conversation
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.
Thanks @yahilmadakiya ! Can you include unit tests for this change?
Sure @danielbachhuber, I'll try to add unit test. |
@danielbachhuber I have tried a unit test for the changes. On my research, I came to know that as of now My unit test 👇 it( 'should not have autofocus if autofocus is false', () => {
const wrapper = mount( <UrlInputButton autofocus={ false } /> );
wrapper.setState( { expanded: true } );
const elem = wrapper.find( 'input[type="text"]' );
expect( elem.is( ':focus' ) ).toBe( false );
} ); |
@yahilmadakiya Looks fine to me. Can you go ahead and commit the unit test? |
@danielbachhuber Above test throw error Please check my comment here 👉 enzymejs/enzyme#703 (comment) |
blocks/url-input/index.js
Outdated
@@ -180,11 +180,12 @@ class UrlInput extends Component { | |||
render() { | |||
const { value = '', instanceId } = this.props; | |||
const { showSuggestions, posts, selectedSuggestion, loading } = this.state; | |||
const autoFocus = 'undefined' === typeof this.props.autoFocus || this.props.autoFocus; |
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.
This is a good use case for destructuring default assignment, just as we're doing with value
:
const { value = '', autoFocus = true, instanceId } = this.props;
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.
@aduth Thanks.
For your test, could this work? it( 'should not have autofocus if autofocus is false', () => {
const wrapper = mount( <UrlInputButton autofocus={ false } /> );
wrapper.setState( { expanded: true } );
const elem = wrapper.find( 'input[type="text"]' ).getDOMNode();
expect( document.activeElement ).toBe( elem );
} ); (I've not tried it, and not sure if JSDOM is capable of assigning |
@aduth I have already tried with Also, I have tried with the above test which you have provided. The Unit test passed in both case Thought: |
That MDN link is unfortunately a bit confusing. I think the working draft status is referring to shadow DOM specifically. It is part of the WHATWG standard: https://html.spec.whatwg.org/multipage/interaction.html#dom-document-activeelement
Could it be that this needs to be corrected to the camel-case form |
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.
Given the difficulty of testing this, I think it's fine to land without tests.
Rebased with |
Fix: #6196
Added option to set autofocus true/false. Default can be true.