From 0e27773fc61e6446eda13f4f48cf06c82b525e9c Mon Sep 17 00:00:00 2001 From: Jakub Benes Date: Sun, 13 May 2018 18:54:16 +0200 Subject: [PATCH 1/2] feat: added new methods selectionPosition and selectedText --- README.md | 2 ++ cypress/integration/textarea.js | 24 ++++++++++++++++++++++++ example/App.jsx | 17 +++++++++++++++++ src/Textarea.jsx | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/README.md b/README.md index e1557d2..7d51229 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ The methods below can be called on the React component's ref (see: [React Docs]( | :------------- | :------------- | getCaretPosition() : number | Gets the current caret position in the textarea | setCaretPosition(position : number) : void | Sets the caret position to the integer value passed as the argument +| getSelectionPositions(): {selectionStar: number, selectionEnd: number} | Returns selectionStart and selectionEnd of the textarea +| getSelectedText(): ?string | Returns currently selected word Example: ```javascript diff --git a/cypress/integration/textarea.js b/cypress/integration/textarea.js index 48f2c49..2874ad0 100644 --- a/cypress/integration/textarea.js +++ b/cypress/integration/textarea.js @@ -172,6 +172,30 @@ describe('React Textarea Autocomplete', () => { }); }); + it('get currently selected word', () => { + cy.get('.rta__textarea').type(`test{selectall}`); + + const stub = cy.stub(); + + cy.on('window:alert', stub); + + cy + .get('[data-test="getSelectedText"]') + .click() + .then(() => { + expect(stub.getCall(0)).to.be.calledWith('test'); + }); + + cy + .get('[data-test="getSelectionPositions"]') + .click() + .then(() => { + expect(stub.getCall(1)).to.be.calledWith( + '{"selectionStart":0,"selectionEnd":4}' + ); + }); + }); + it('should close the textarea when click outside happens', () => { cy.get('[data-test="clickoutsideOption"]').click(); diff --git a/example/App.jsx b/example/App.jsx index f195091..96f759b 100644 --- a/example/App.jsx +++ b/example/App.jsx @@ -92,6 +92,14 @@ class App extends React.Component { _outputCaretNext = item => ({ text: item.char, caretPosition: 'next' }); + _getSelectionPosition = () => { + alert(JSON.stringify(this.rtaRef.getSelectionPosition())); + }; + + _getSelectedText = () => { + alert(this.rtaRef.getSelectedText()); + }; + /** * it's the same as _outputCaretNext */ @@ -172,6 +180,15 @@ class App extends React.Component { + +
Actual token in "[" provider:{' '} {actualTokenInProvider} diff --git a/src/Textarea.jsx b/src/Textarea.jsx index a8a70a7..28a44d0 100644 --- a/src/Textarea.jsx +++ b/src/Textarea.jsx @@ -81,6 +81,30 @@ class ReactTextareaAutocomplete extends React.Component< Listeners.stopListen(); } + getSelectionPosition = (): ?{| + selectionStart: number, + selectionEnd: number, + |} => { + if (!this.textareaRef) return null; + + return { + selectionStart: this.textareaRef.selectionStart, + selectionEnd: this.textareaRef.selectionEnd, + }; + }; + + getSelectedText = (): ?string => { + if (!this.textareaRef) return null; + const { selectionStart, selectionEnd } = this.textareaRef; + + if (selectionStart === selectionEnd) return null; + + return this.state.value.substr( + selectionStart, + selectionEnd - selectionStart + ); + }; + setCaretPosition = (position: number = 0) => { if (!this.textareaRef) return; From b056750fdedd6a0ef6804c8807a01a9134fad1cc Mon Sep 17 00:00:00 2001 From: Jakub Benes Date: Sun, 13 May 2018 18:56:12 +0200 Subject: [PATCH 2/2] fix: cypress --- cypress/integration/textarea.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/integration/textarea.js b/cypress/integration/textarea.js index 2874ad0..37621d5 100644 --- a/cypress/integration/textarea.js +++ b/cypress/integration/textarea.js @@ -187,7 +187,7 @@ describe('React Textarea Autocomplete', () => { }); cy - .get('[data-test="getSelectionPositions"]') + .get('[data-test="getSelectionPosition"]') .click() .then(() => { expect(stub.getCall(1)).to.be.calledWith(