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(Autocomplete): support query with unicodes when using search_numbers #4419

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ class AutocompleteInstance extends React.PureComponent {
.filter(({ word, wordIndex }) => {
if (searchNumbers) {
// Remove all other chars, except numbers, so we can compare
word = word.replace(/[^\d\w]/g, '')
word = word.replace(/[^\p{L}\p{N}]+/gu, '')
} else {
// To ensure we escape regex chars
word = escapeRegexChars(word)
Expand Down Expand Up @@ -1474,7 +1474,7 @@ class AutocompleteInstance extends React.PureComponent {

if (searchNumbers) {
word.split('').forEach((char) => {
if (/[\d\w]/.test(char)) {
if (/[\p{L}\p{N}]/u.test(char)) {
segment = segment.replace(
new RegExp(`(${char})`, 'gi'),
`${strS}$1${strE}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,40 @@ describe('Autocomplete component', () => {
).toBe(mockData[3])
})

it('has correct options when using search_numbers, and searching with æøå', () => {
const mockData = [
['Åge Ørn Ærlig', format('12345678901')],
["Andrè Ørjåsæter O'Neill", format('12345678901')],
] as DrawerListData

render(
<Autocomplete
data={mockData}
search_numbers
show_submit_button
{...mockProps}
/>
)

toggle()

fireEvent.change(document.querySelector('.dnb-input__input'), {
target: { value: 'Åge Ørn Ærlig' },
})
expect(
document.querySelectorAll('li.dnb-drawer-list__option')[0]
.textContent
).toBe('Åge Ørn Ærlig12 345 678 901')

fireEvent.change(document.querySelector('.dnb-input__input'), {
target: { value: "Andrè Ørjåsæter O'Neill" },
})
expect(
document.querySelectorAll('li.dnb-drawer-list__option')[0]
.textContent
).toBe("Andrè Ørjåsæter O'Neill12 345 678 901")
})

it('has correct options when using search_numbers and search_in_word_index=1', () => {
const mockData = ['100.222.333,40', '123456', '100 222 444,50']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ export const SearchNumbers = () => {
)
}

export const SearchNumbersNonAlphaNumericChars = () => {
return (
<Autocomplete
label="Label:"
data={[
['Edgar Wuckert', '1234.56.78901'],
['Megan Abshire Jr.', '1234 56 78901'],
['Åge Ørn Ærlig', '12345678901'],
["Andrè O'Neill", '12345678901'],
]}
search_numbers
/>
)
}

const accounts = [
{ selectedKey: 1, content: 'A' },
{ selectedKey: 2, content: 'B' },
Expand Down
Loading