Skip to content

Commit

Permalink
fix: multichar trigger replaces entire text
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszmn committed Jul 15, 2021
1 parent a30649a commit 17db7ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cypress/integration/textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ describe("React Textarea Autocomplete", () => {
cy.get(".rta__textarea").should("have.value", "This is test test2");
});

// https://github.com/webscopeio/react-textarea-autocomplete/issues/219
it("test multi-character trigger doesn't replace entire text", () => {
cy.get(".rta__textarea").type("This is test /filtere");
cy.get(".rta__autocomplete").should("be.visible");
cy.get(".rta__list")
.get("li:nth-child(1)")
.click();
cy.get(".rta__textarea").should("have.value", "This is test emily");
});

it("change value from outside should trigger the autocomplete as well", () => {
cy.get(".rta__autocomplete").should("not.be.visible");
cy.get('[data-test="changeValueTo"]').click();
Expand Down
11 changes: 11 additions & 0 deletions example/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@ class App extends React.Component {
component: Item,
output: this._outputCaretEnd
},
"/filter": {
dataProvider: token => [
{ name: "a", char: "amy" },
{ name: "b", char: "ben" },
{ name: "c", char: "cheryl" },
{ name: "d", char: "daniel" },
{ name: "e", char: "emily" }
].filter(x => x.name == token.slice(6) || token.length === 6),
component: Item,
output: this._outputCaretEnd
},
"(": {
dataProvider: token => [
{ name: "country", char: "country" },
Expand Down
4 changes: 2 additions & 2 deletions src/Textarea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@ class ReactTextareaAutocomplete extends React.Component<
* It's important to escape the currentTrigger char for chars like [, (,...
*/
new RegExp(
`${escapeRegex(currentTrigger)}${`[^${escapeRegex(currentTrigger)}${
`${escapeRegex(currentTrigger)}${`.*(?!${escapeRegex(currentTrigger)}${
trigger[currentTrigger].allowWhitespace ? "" : "\\s"
}]`}*$`
})`}*$`
)
);

Expand Down

0 comments on commit 17db7ae

Please sign in to comment.