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

fix(kinput): prevent focus loss on icon/button click #2453

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/KInput/KInput.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ describe('KInput', () => {

cy.get('.k-input .mask-value-toggle-button').should('be.visible')
cy.get('.k-input input').should('have.attr', 'type', 'password')
cy.get('.k-input input').focus()
cy.get('.k-input .mask-value-toggle-button').click()
cy.get('.k-input input').should('have.attr', 'type', 'text')
cy.get('.k-input .mask-value-toggle-button').click()
cy.get('.k-input input').should('have.attr', 'type', 'password')

cy.get('.k-input input').should('have.attr', 'type', 'password').should('be.focused')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: in real user interactions, mouseup moves the input cursor to the start position, but I couldn't reproduce this behavior in test cases, so this part of the test is not included.

// user-provided after slot should be rendered
cy.get('.k-input').find(`[data-testid="${afterSlot}"]`).should('not.exist')
})
Expand Down
8 changes: 8 additions & 0 deletions src/components/KInput/KInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
:aria-label="`${maskValue ? 'Hide' : 'Show'} value`"
class="mask-value-toggle-button"
@click.stop="maskValue = !maskValue"
@mousedown.prevent
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes the input stay focusd.

@mouseup.prevent
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This keeps the cursor position.

>
<VisibilityOffIcon
v-if="maskValue"
Expand Down Expand Up @@ -384,6 +386,12 @@ $kInputSlotSpacing: var(--kui-space-40, $kui-space-40); // $kSelectInputSlotSpac
width: $kInputIconSize !important;
}

// enhance the experience for most common cases that icon only slots should not
// prevent the input from being focused by click on the icon
&:has(> #{$kongponentsKongIconSelector}:not(button):not([role="button"]):only-child) {
pointer-events: none;
}
Justineo marked this conversation as resolved.
Show resolved Hide resolved

:deep([role="button"]:not(.k-button)), :deep(button:not(.k-button)),
.mask-value-toggle-button {
@include defaultButtonReset;
Expand Down