Skip to content

Conversation

@mapitman
Copy link

Description

Remove the hard-coded autoFocus attributes from the auth-related jsx files. Replace with JavaScript to set the focus to the first input element in the auth modal.

Motivation and Context

Fixes #7711

How Has This Been Tested?

I tested it with using the swagger.json linked to in the issue. Also the automated test that was added to test the autoFocus still passes after this change.

Screenshots (if appropriate):

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

There is an existing test for this.

@mapitman mapitman force-pushed the autofocus branch 2 times, most recently from 9f054e4 to 684a6f5 Compare December 17, 2021 16:15
@tim-lai
Copy link
Contributor

tim-lai commented Jan 12, 2022

@mapitman Thanks for the PR (and issue ticket). I do think that moving control of the prop to the top-level Auth component is a good starting point. However, I think there should be a way to provide a priority list of preferred components (keys?) to set an autofocus on first, and then skip for the rest of components if autofocus is set. I know you mentioned that you are not as familiar with React, so perhaps someone else is willing and able to further develop this PR if you are unable to.

@mapitman
Copy link
Author

@tim-lai I'd be willing to take another go at this. Would you be willing to give me some guidance on how this would work? I'm not quite understanding the use case. Is it that someone may want to set focus to the second textbox in the modal for example?

@tim-lai
Copy link
Contributor

tim-lai commented Jan 13, 2022

@mapitman My interpretation of the earlier PR #6483 was that the PR simply added the autofocus to any input elements that could be present (per various individual use cases), without considering if those input elements could appear together. In the case(s) where multiple input elements are present, it seems reasonable that the autofocus should be on the first input element (this PR). So my request is that any proposed solution does NOT hardcode which input element to autofocus, but that it finds the first element rendered to which autofocus should be applied. Let me if know that helps clarify my request. Thanks!

Find the first input element in the div with class name
modal-ux-content and set focus to it.
@mapitman
Copy link
Author

mapitman commented Feb 1, 2022

@tim-lai Thanks for the reply. I've made a slight change because I noticed I was using using a class name specific to my use of Swagger UI. I've also rebased my changes on current master.

The code will now find the first input element in the modal div (found by using the class name modal-ux-content) and set focus to it. I have also removed all the autoFocus attributes from the inputs.

I believe this addresses your point above:

So my request is that any proposed solution does NOT hardcode which input element to autofocus, but that it finds the first element rendered to which autofocus should be applied.

I didn't realize this project does not use semi-colons.
}

componentDidMount() {
var inputs = document.getElementsByClassName("modal-ux-content")[0].getElementsByTagName("input")
Copy link
Contributor

Choose a reason for hiding this comment

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

As a vanilla JS solution, this looks good to me. However, thinking about this more, we really should "React-ify" this to not directly change the DOM with the .focus. E.g. use "refs"

I'm thinking would could try adding a ref to className="auth-container" here that would contain multiple current refs. Most likely then also need to add creation of refs within the two maps that generate React element. e.g. something similar to ref={el => inputRef.current[x] = el} .

More info on refs here, and we'll want to be compatible with the possible future use of React hooks.

Copy link
Author

Choose a reason for hiding this comment

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

"React-ifying" is the part where I'm out of my depth. I'll take a look at the link you provided and see if I can learn just enough to do this. 😸

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.

When there are multiple security schemes, the authorization modal partially scrolls and does not necessarily focus the first text box

2 participants