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

Fixed autosuggest input with special characters #1443

Merged
merged 7 commits into from Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/src/text-input/Suggestion.tsx
Expand Up @@ -2,6 +2,19 @@ import React from "react";
import styled from "styled-components";
import { SuggestionProps } from "./types";

const transformSpecialChars = (str: string) => {
const specialCharsRegex = /[\\*()\[\]{}+?/]/;
let value = str;
if (specialCharsRegex.test(value)) {
const regexAsString = specialCharsRegex.toString().split("");
const uniqueSpecialChars = regexAsString.filter((item, index) => regexAsString.indexOf(item) === index);
uniqueSpecialChars.forEach((specialChar) => {
if (str.includes(specialChar)) value = value.replace(specialChar, "\\" + specialChar);
});
}
return value;
};

const Suggestion = ({
id,
value,
Expand All @@ -11,7 +24,8 @@ const Suggestion = ({
visuallyFocused,
highlighted,
}: SuggestionProps): JSX.Element => {
const regEx = new RegExp(value, "i");
const specialChar = transformSpecialChars(value);
aidamag marked this conversation as resolved.
Show resolved Hide resolved
const regEx = new RegExp(specialChar, "i");
const matchedWords = suggestion.match(regEx);
const noMatchedWords = suggestion.replace(regEx, "");

Expand Down
7 changes: 6 additions & 1 deletion lib/src/text-input/TextInput.stories.tsx
Expand Up @@ -61,6 +61,7 @@ const countries = [
"Dominica",
"Denmark",
"Djibouti",
"*",
];

export const Chromatic = () => (
Expand Down Expand Up @@ -308,7 +309,11 @@ const AutosuggestListbox = () => {
<ExampleContainer>
<Title title="Autosuggest listbox" theme="light" level={2} />
<ExampleContainer>
<Title title="List dialog uses a Radix Popover to appear over elements with a certain z-index" theme="light" level={3} />
<Title
title="List dialog uses a Radix Popover to appear over elements with a certain z-index"
theme="light"
level={3}
/>
<div
style={{
display: "flex",
Expand Down
39 changes: 39 additions & 0 deletions lib/src/text-input/TextInput.test.js
Expand Up @@ -44,6 +44,8 @@ const countries = [
"Djibouti",
];

const specialCharacters = ["/", "\\", "*", "(", ")", "[", "]", "+", "?", "*{[]}|"];

describe("TextInput component tests", () => {
test("Renders with correct error aria attributes", () => {
const { getByText, getByRole } = render(
Expand Down Expand Up @@ -643,6 +645,43 @@ describe("TextInput component synchronous autosuggest tests", () => {
expect(input.value).toBe("");
expect(queryByRole("listbox")).toBeFalsy();
});

test("Autosuggest escapes special characters", () => {
const onChange = jest.fn();
const { getAllByText, getByText, getByRole } = render(
<DxcTextInput label="Autocomplete Countries" suggestions={specialCharacters} onChange={onChange} />
);
const input = getByRole("combobox");
fireEvent.focus(input);
const list = getByRole("listbox");
fireEvent.change(input, { target: { value: "/" } });
expect(list).toBeTruthy();
expect(getAllByText("/").length).toBe(1);
fireEvent.change(input, { target: { value: "\\" } });
expect(list).toBeTruthy();
expect(getAllByText("\\").length).toBe(1);
fireEvent.change(input, { target: { value: "*" } });
expect(list).toBeTruthy();
expect(getAllByText("*").length).toBe(2);
fireEvent.change(input, { target: { value: "(" } });
expect(list).toBeTruthy();
expect(getAllByText("(").length).toBe(1);
fireEvent.change(input, { target: { value: ")" } });
expect(list).toBeTruthy();
expect(getAllByText(")").length).toBe(1);
fireEvent.change(input, { target: { value: "[" } });
expect(list).toBeTruthy();
expect(getAllByText("[").length).toBe(1);
fireEvent.change(input, { target: { value: "]" } });
expect(list).toBeTruthy();
expect(getAllByText("]").length).toBe(1);
fireEvent.change(input, { target: { value: "+" } });
expect(list).toBeTruthy();
expect(getAllByText("+").length).toBe(1);
fireEvent.change(input, { target: { value: "?" } });
expect(list).toBeTruthy();
expect(getAllByText("?").length).toBe(1);
});
});

describe("TextInput component asynchronous autosuggest tests", () => {
Expand Down