Skip to content

Commit

Permalink
fix (segmentio#1652): resolve error trim for data object in AutoCompl…
Browse files Browse the repository at this point in the history
…ete component
  • Loading branch information
DevJoaoLopes committed Jul 29, 2023
1 parent cce2742 commit b23db01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
27 changes: 27 additions & 0 deletions src/autocomplete/__tests__/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,32 @@ describe('Autocomplete', () => {
expect(textInput).toHaveValue('A')
})
})

describe('when inputvalues is object', () => {
it.only('should read data autocomplete', async () => {
const items = [{name: 'Apple', id: 1}, {name: 'Orange', id: 2}]

render(
makeAutocompleteFixture({
allowOtherValues: true,
items,
itemToString: (item) => (item ? item.name : ""),
})
)

// Type 'A' into the input to filter items down containing the string
const textInput = await screen.findByTestId('TextInput')
userEvent.click(textInput)
userEvent.type(textInput, 'A')

// Click the 'Apple' option, which should also update the input element
const item = await screen.findByText('Apple')
userEvent.click(item)


// an object will be returned in the input without error
expect(textInput).toHaveValue('Apple')
})
});
})
})
11 changes: 4 additions & 7 deletions src/autocomplete/src/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { memo, forwardRef, useState, useEffect, useCallback } from 'react
import VirtualList from '@segment/react-tiny-virtual-list'
import Downshift from 'downshift'
import fuzzaldrin from 'fuzzaldrin-plus'
import PropTypes from 'prop-types'
import PropTypes, { string } from 'prop-types'
import { Position } from '../../constants'
import { Pane } from '../../layers'
import { Popover } from '../../popover'
Expand Down Expand Up @@ -46,7 +46,7 @@ const AutocompleteItems = ({
width
}) => {
itemsFilter = itemsFilter || fuzzyFilter(itemToString)
const items = isFilterDisabled || inputValue.trim() === '' ? originalItems : itemsFilter(originalItems, inputValue)
const items = isFilterDisabled || (typeof inputValue === 'string' && inputValue.trim() === '') ? originalItems : itemsFilter(originalItems, inputValue)

if (items.length <= 0) return null

Expand Down Expand Up @@ -128,11 +128,8 @@ const Autocomplete = memo(
}

if (props.allowOtherValues && state.isOpen && !changes.isOpen) {
return {
...changes,
selectedItem: changes.selectedItem || state.inputValue,
inputValue: changes.selectedItem || state.inputValue
}
const valueItem = changes.selectedItem || state.inputValue;
return typeof valueItem === 'object' ? {...changes, selectedItem: valueItem} : {...changes, selectedItem: valueItem, inputValue: valueItem}
}

return changes
Expand Down

0 comments on commit b23db01

Please sign in to comment.