Skip to content

Commit

Permalink
fix(kselect): clear filter on blur [KHCP-13444] (#2486)
Browse files Browse the repository at this point in the history
* fix(kselect): clear filter on blur [KHCP-13444]

* fix(kselect, kmultiselect): create item on enter key press [KHCP-13444]
  • Loading branch information
portikM authored Oct 29, 2024
1 parent e53e4f0 commit 83da311
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/components/KMultiselect/KMultiselect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
}
}"
@focus="onInputFocus"
@keydown.enter="onInputEnter"
@keyup="(evt: any) => triggerFocus(evt, isToggled)"
@mouseenter="() => isHovered = true"
@mouseleave="() => isHovered = false"
Expand Down Expand Up @@ -170,6 +171,7 @@
type="text"
@click.stop
@focus="triggerInitialFocus"
@keydown.enter="onInputEnter"
@keyup="onDropdownInputKeyup"
@keyup.enter.stop
@update:model-value="onQueryChange"
Expand Down Expand Up @@ -815,9 +817,15 @@ const handleItemSelect = (item: MultiselectItem, isNew?: boolean) => {
emit('update:modelValue', selectedVals)
}
const onInputEnter = (): void => {
if (!filteredItems.value.length && props.enableItemCreation) {
handleAddItem()
}
}
// add an item with `enter`
const handleAddItem = (): void => {
if (!props.enableItemCreation || !filterString.value || !uniqueFilterStr.value) {
if (!props.enableItemCreation || !filterString.value || !uniqueFilterStr.value || !props.itemCreationValidator(filterString.value)) {
// do nothing if not enabled or no label or label already exists
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/KSelect/KSelect.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('KSelect', () => {
cy.getTestId(`select-item-${vals[0]}`).should('contain.text', labels[0])
cy.getTestId(`select-item-${vals[1]}`).should('not.exist')

cy.getTestId(`select-item-${vals[0]}`).eq(0).click({ force: true })
cy.getTestId(`select-item-${vals[0]}`).click()
cy.getTestId('select-input').should('have.value', labels[0])
})

Expand Down
9 changes: 9 additions & 0 deletions src/components/KSelect/KSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
@blur="onInputBlur"
@click="onInputClick"
@focus="onInputFocus"
@keydown.enter="onInputEnter"
@keypress="onInputKeypress"
@keyup="(evt: any) => triggerFocus(evt, isToggled)"
@keyup.enter.stop
Expand Down Expand Up @@ -477,6 +478,12 @@ const onInputKeypress = (event: Event) => {
}
}
const onInputEnter = (): void => {
if (!filteredItems.value.length && props.enableItemCreation) {
handleAddItem()
}
}
const handleAddItem = (): void => {
if (!props.enableItemCreation || !filterQuery.value || !uniqueFilterQuery.value) {
// do nothing if not enabled or no label or label already exists
Expand Down Expand Up @@ -607,6 +614,8 @@ const onClose = (toggle: () => void, isToggled: boolean) => {
if (selectedItem.value) {
filterQuery.value = selectedItem.value.label
} else {
filterQuery.value = ''
}
if (isToggled) {
Expand Down

0 comments on commit 83da311

Please sign in to comment.