Skip to content

Commit a35dc70

Browse files
oklasjukben
authored andcommitted
fix(listener): refCount instead of flag, +tests (#73) (#74)
I've added one more conditilonally additional instance to test-example app. And also I added the cypress tests which add and remove the instance and many tests falls. This fix makes everything work fine. This close the #73.
1 parent 77d9d72 commit a35dc70

File tree

6 files changed

+1280
-329
lines changed

6 files changed

+1280
-329
lines changed

cypress/integration/textarea.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ describe('React Textarea Autocomplete', () => {
4242
.should('have.value', 'This is test 🤣');
4343
});
4444

45+
it('basic keyboard test after unmounting second instance', () => {
46+
cy.get('[data-test="showSecondTextarea"]').click();
47+
cy.get('[data-test="showSecondTextarea"]').click();
48+
cy
49+
.get('.rta__textarea')
50+
.type('This is test :ro{downarrow}{enter}')
51+
.should('have.value', 'This is test 🤣');
52+
});
53+
4554
it('should change only correct part of the word', () => {
4655
cy
4756
.get('.rta__textarea')

example/App.jsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class App extends React.Component {
3131
text: '',
3232
optionsCaret: 'start',
3333
actualTokenInProvider: '',
34+
showSecondTextarea: false,
3435
};
3536

3637
_handleOptionsCaretEnd = () => {
@@ -57,6 +58,12 @@ class App extends React.Component {
5758
}));
5859
};
5960

61+
_handleShowSecondTextarea = () => {
62+
this.setState(({ showSecondTextarea }) => ({
63+
showSecondTextarea: !showSecondTextarea,
64+
}));
65+
};
66+
6067
_handleMovePopupAsYouType = () => {
6168
this.setState(({ movePopupAsYouType }) => ({
6269
movePopupAsYouType: !movePopupAsYouType,
@@ -116,6 +123,7 @@ class App extends React.Component {
116123
clickoutsideOption,
117124
movePopupAsYouType,
118125
actualTokenInProvider,
126+
showSecondTextarea,
119127
text,
120128
} = this.state;
121129

@@ -163,6 +171,17 @@ class App extends React.Component {
163171
Close when click outside
164172
</label>
165173
</div>
174+
<div>
175+
<label>
176+
<input
177+
data-test="showSecondTextarea"
178+
type="checkbox"
179+
defaultChecked={showSecondTextarea}
180+
onChange={this._handleShowSecondTextarea}
181+
/>
182+
Show second textarea
183+
</label>
184+
</div>
166185
<div>
167186
<label>
168187
<input
@@ -289,6 +308,35 @@ class App extends React.Component {
289308
},
290309
}}
291310
/>
311+
{!showSecondTextarea ? null :
312+
<ReactTextareaAutocomplete
313+
style={{
314+
padding: 5,
315+
}}
316+
containerStyle={{
317+
marginTop: 20,
318+
width: 400,
319+
height: 100,
320+
margin: '20px auto',
321+
}}
322+
loadingComponent={Loading}
323+
trigger={{
324+
':': {
325+
dataProvider: token =>
326+
emoji(token)
327+
.slice(0, 10)
328+
.filter(({ char }) => char)
329+
.map(({ name, char }) => ({ name, char })),
330+
component: Item,
331+
output: {
332+
start: this._outputCaretStart,
333+
end: this._outputCaretEnd,
334+
next: this._outputCaretNext,
335+
}[optionsCaret],
336+
},
337+
}}
338+
/>
339+
}
292340
</div>
293341
);
294342
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"flow-bin": "^0.60.1",
5858
"html-webpack-plugin": "^2.30.1",
5959
"husky": "^0.14.3",
60-
"jest": "^21.2.1",
60+
"jest": "^23.2.0",
6161
"lint-staged": "^6.0.0",
6262
"prettier": "^1.8.2",
6363
"react": "^15.6.1",

setupJest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
* Polyfill for CI
33
*/
44
import 'babel-polyfill';
5+
6+
window.HTMLElement.prototype.scrollIntoView = function() {};

src/listener.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class Listener {
1616
[number]: {| keyCode: Array<number>, fn: Function |},
1717
};
1818

19-
isListening: boolean;
19+
refCount: number;
2020

2121
f: Function;
2222

2323
constructor() {
2424
this.index = 0;
2525
this.listeners = {};
26-
this.isListening = false;
26+
this.refCount = 0;
2727

2828
this.f = (e: KeyboardEvent) => {
2929
const code = e.keyCode || e.which;
@@ -35,16 +35,19 @@ class Listener {
3535
}
3636

3737
startListen = () => {
38-
if (!this.isListening) {
38+
if (!this.refCount) {
3939
// prevent multiple listeners in case of multiple TextareaAutocomplete components on page
4040
document.addEventListener('keydown', this.f);
41-
this.isListening = true;
4241
}
42+
this.refCount++;
4343
};
4444

4545
stopListen = () => {
46-
document.removeEventListener('keydown', this.f);
47-
this.isListening = false;
46+
this.refCount--;
47+
if (!this.refCount) {
48+
// prevent disable listening in case of multiple TextareaAutocomplete components on page
49+
document.removeEventListener('keydown', this.f);
50+
}
4851
};
4952

5053
add = (keyCodes: Array<number> | number, fn: Function) => {

0 commit comments

Comments
 (0)