Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions cypress/integration/textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -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="getSelectionPosition"]')
.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();

Expand Down
17 changes: 17 additions & 0 deletions example/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -172,6 +180,15 @@ class App extends React.Component {
<button data-test="getCaretPosition" onClick={this._getCaretPosition}>
getCaretPosition();
</button>
<button
data-test="getSelectionPosition"
onClick={this._getSelectionPosition}
>
getSelectionPosition();
</button>
<button data-test="getSelectedText" onClick={this._getSelectedText}>
getSelectedText();
</button>
<div>
Actual token in "[" provider:{' '}
<span data-test="actualToken">{actualTokenInProvider}</span>
Expand Down
24 changes: 24 additions & 0 deletions src/Textarea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down