Skip to content

Commit bfa4a10

Browse files
authored
feat: innerRef prop to get Ref to textarea (#69)
Now you can easily get Ref of underlying textarea. The other approach is to access to `this.rtaRef.textareaRef` but `innerRef={(ref) => {this.textareaRef = ref}}` is now preffered.
1 parent ded5d84 commit bfa4a10

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ or there is UMD build available. [Check out this pen as example](https://codepen
3434
3535
## Props
3636

37-
*Note: Every other props than the mentioned below will be propagated to textarea itself*
37+
> *☝️ Note: Every other props than the mentioned below will be propagated to the textarea itself*
3838
3939
| Props | Type | Description
4040
| :------------- | :------------- | ---------
4141
| **trigger*** | Object: Trigger type | Define triggers and their corresponding behavior
4242
| **loadingComponent*** | React Component | Gets `data` props which is already fetched (and displayed) suggestion
43+
| innerRef | Function: (HTMLTextAreaElement) => void) | Allows you to get React ref of the underlying textarea
4344
| minChar | Number | Number of characters that user should type for trigger a suggestion. Defaults to 1.
4445
| onCaretPositionChange | Function: (number) => void | Listener called every time the textarea's caret position is changed. The listener is called with one attribute - caret position denoted by an integer number.
4546
| closeOnClickOutside | boolean | When it's true autocomplete will close when use click outside. Defaults to false.
@@ -83,11 +84,11 @@ class App extends Component {
8384
}
8485

8586
resetCaretPosition = () => {
86-
this.textarea.setCaretPosition(0);
87+
this.rta.setCaretPosition(0);
8788
}
8889

8990
printCurrentCaretPosition = () => {
90-
const caretPosition = this.textarea.getCaretPosition();
91+
const caretPosition = this.rta.getCaretPosition();
9192
console.log(`Caret position is equal to ${caretPosition}`);
9293
}
9394

@@ -102,7 +103,7 @@ class App extends Component {
102103
className="my-textarea"
103104
loadingComponent={() => <span>Loading</span>}
104105
trigger={{ ... }}
105-
ref={(textarea) => { this.textarea = textarea; } }
106+
ref={(rta) => { this.rta = rta; } }
106107
onCaretPositionChange={this.onCaretPositionChange}
107108
/>
108109
</div>
@@ -184,6 +185,8 @@ class App extends Component {
184185
lineHeight: "20px",
185186
padding: 5
186187
}}
188+
ref={(rta) => { this.rta = rta; } }
189+
innerRef={(textarea) => { this.textarea = textarea; } }
187190
containerStyle={{
188191
marginTop: 20,
189192
width: 400,

__tests__/index.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ describe('object-based items without keys and custom unique generator', () => {
495495

496496
it('should generate unique value by the output generator', () => {
497497
const items = rta.find(Item);
498-
expect(items.at(0).key()).toEqual("11");
499-
expect(items.at(1).key()).toEqual("12");
500-
expect(items.at(2).key()).toEqual("13");
498+
expect(items.at(0).key()).toEqual('11');
499+
expect(items.at(1).key()).toEqual('12');
500+
expect(items.at(2).key()).toEqual('13');
501501
});
502502
});

cypress/integration/textarea.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ describe('React Textarea Autocomplete', () => {
2424
cy.get('.rta__textarea');
2525
});
2626

27+
it('focus textarea by innerRef', () => {
28+
cy.focused().should('not.be.visible');
29+
cy.get('[data-test="focus"]').click();
30+
cy.focused().should('be.visible');
31+
});
32+
2733
context('basic', () => {
2834
beforeEach(() => {
2935
cy.get('.rta__textarea').clear({ force: true });

example/App.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class App extends React.Component {
105105
*/
106106
_outputCaretDefault = item => item.char;
107107

108+
_focus = () => {
109+
this.textareaRef.focus();
110+
};
111+
108112
render() {
109113
const {
110114
optionsCaret,
@@ -189,6 +193,9 @@ class App extends React.Component {
189193
<button data-test="getSelectedText" onClick={this._getSelectedText}>
190194
getSelectedText();
191195
</button>
196+
<button data-test="focus" onClick={this._focus}>
197+
Focus the textarea
198+
</button>
192199
<div>
193200
Actual token in "[" provider:{' '}
194201
<span data-test="actualToken">{actualTokenInProvider}</span>
@@ -199,6 +206,9 @@ class App extends React.Component {
199206
ref={ref => {
200207
this.rtaRef = ref;
201208
}}
209+
innerRef={ref => {
210+
this.textareaRef = ref;
211+
}}
202212
loadingComponent={Loading}
203213
style={{
204214
padding: 5,

src/Textarea.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ class ReactTextareaAutocomplete extends React.Component<
363363
'containerStyle',
364364
'minChar',
365365
'ref',
366+
'innerRef',
366367
'onChange',
367368
'onCaretPositionChange',
368369
'className',
@@ -591,6 +592,7 @@ class ReactTextareaAutocomplete extends React.Component<
591592
<textarea
592593
{...this._cleanUpProps()}
593594
ref={ref => {
595+
this.props.innerRef && this.props.innerRef(ref);
594596
this.textareaRef = ref;
595597
}}
596598
className={`rta__textarea ${className || ''}`}

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type TextareaProps = {
6767
onSelect: ?(SyntheticEvent<*> | Event) => void,
6868
onBlur: ?(SyntheticEvent<*> | Event) => void,
6969
onCaretPositionChange: ?(number) => void,
70+
innerRef: ?(HTMLTextAreaElement) => void,
7071
closeOnClickOutside?: boolean,
7172
movePopupAsYouType?: boolean,
7273
minChar: ?number,

0 commit comments

Comments
 (0)