Skip to content

Commit

Permalink
fix(Autocomplete): make clear button work with enter key (#2901)
Browse files Browse the repository at this point in the history
Fixes issue #2185
  • Loading branch information
tujoworker authored Nov 15, 2023
1 parent d650c66 commit 30007c4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 43 deletions.
80 changes: 38 additions & 42 deletions packages/dnb-eufemia/src/components/autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ class AutocompleteInstance extends React.PureComponent {

onInputKeyDownHandler = ({ event: e }) => {
const key = keycode(e)

switch (key) {
case 'page up':
case 'page down':
Expand All @@ -853,6 +854,43 @@ class AutocompleteInstance extends React.PureComponent {
e.preventDefault() // has to be there for VO, one the drawer is closed
break
}

switch (key) {
case 'up':
case 'down':
if (!this.context.drawerList.opened) {
// e.preventDefault()
this.setVisible()
}

break

case 'esc':
this.setState({
showAllNextTime: true,
_listenForPropChanges: false,
})

break

case 'enter':
e.preventDefault()

if (!this.context.drawerList.opened && this.hasFilterActive()) {
this.ignoreEvents()
this.showAll()
}
if (
(!this.hasValidData() || !this.hasSelectedItem()) &&
!this.hasActiveItem()
) {
this.toggleVisible()
} else {
this.setVisible()
}

break
}
}

onInputClickHandler = (e) => {
Expand Down Expand Up @@ -1012,47 +1050,6 @@ class AutocompleteInstance extends React.PureComponent {
}
}

onShellKeyDownHandler = (e) => {
const key = keycode(e)

switch (key) {
case 'up':
case 'down':
if (!this.context.drawerList.opened) {
e.preventDefault()
this.setVisible()
}

break

case 'esc':
this.setState({
showAllNextTime: true,
_listenForPropChanges: false,
})

break

case 'enter':
e.preventDefault()

if (!this.context.drawerList.opened && this.hasFilterActive()) {
this.ignoreEvents()
this.showAll()
}
if (
(!this.hasValidData() || !this.hasSelectedItem()) &&
!this.hasActiveItem()
) {
this.toggleVisible()
} else {
this.setVisible()
}

break
}
}

getEventObjects = (key) => {
const attributes = this.attributes

Expand Down Expand Up @@ -1836,7 +1833,6 @@ class AutocompleteInstance extends React.PureComponent {
const shellParams = {
className: 'dnb-autocomplete__shell dnb-no-focus',
ref: this._refShell,
onKeyDown: this.onShellKeyDownHandler,
}

const inputParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2542,9 +2542,54 @@ describe('Autocomplete component', () => {
expect(inputElement.value).toEqual('CH (+41)')
})

it('shold reset value and open drawer on clear button click', async () => {
const on_focus = jest.fn()
render(
<Autocomplete
show_clear_button
data={mockData}
{...mockProps}
on_focus={on_focus}
/>
)

const inputElement = document.querySelector(
'.dnb-input__input'
) as HTMLInputElement
const clearElement = () =>
document.querySelector('.dnb-input__clear-button')

// Reset with click
{
await userEvent.type(inputElement, 'aa')

expect(inputElement.value).toBe('aa')
expect(document.activeElement).toBe(inputElement)

fireEvent.click(clearElement())

expect(inputElement.value).toBe('')
expect(document.activeElement).toBe(inputElement)
}

// Reset with keyboard
{
await userEvent.type(inputElement, 'bb')

expect(inputElement.value).toBe('bb')
expect(document.activeElement).toBe(inputElement)

await userEvent.type(clearElement(), '{Enter}')

expect(inputElement.value).toBe('')
expect(document.activeElement).toBe(inputElement)
}
})

describe('input blur', () => {
const mainElement = () => document.querySelector('.dnb-autocomplete')
const inputElement = () => document.querySelector('.dnb-input__input')
const inputElement = () =>
document.querySelector('.dnb-input__input') as HTMLInputElement
const inputComponent = () => document.querySelector('.dnb-input')
const listElement = () =>
document.querySelector('.dnb-autocomplete__list')
Expand Down

0 comments on commit 30007c4

Please sign in to comment.