Skip to content

Commit f0cad33

Browse files
authored
Merge pull request #1 from sayrhino/fix-focus-issues
Fix focus issues
2 parents e9f5be6 + 23ec1e8 commit f0cad33

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CurrencyInput extends Component {
1313
this.prepareProps = this.prepareProps.bind(this);
1414
this.handleChange = this.handleChange.bind(this);
1515
this.handleFocus = this.handleFocus.bind(this);
16+
this.setSelectionRange = this.setSelectionRange.bind(this);
1617
this.state = this.prepareProps(this.props);
1718

1819
this.inputSelectionStart = 0;
@@ -120,7 +121,7 @@ class CurrencyInput extends Component {
120121
let selectionEnd = Math.min(node.selectionEnd, this.theInput.value.length - this.props.suffix.length);
121122
let selectionStart = Math.min(node.selectionStart, selectionEnd);
122123
//console.log("normal", selectionStart, selectionEnd);
123-
node.setSelectionRange(selectionStart, selectionEnd);
124+
this.setSelectionRange(node, selectionStart, selectionEnd);
124125

125126
}
126127

@@ -171,12 +172,25 @@ class CurrencyInput extends Component {
171172
selectionStart = selectionEnd;
172173
}
173174

174-
node.setSelectionRange(selectionStart, selectionEnd);
175+
this.setSelectionRange(node, selectionStart, selectionEnd);
175176
this.inputSelectionStart = selectionStart;
176177
this.inputSelectionEnd = selectionEnd;
177178
}
178179

179180

181+
/**
182+
* Set selection range only if input is in focused state
183+
* @param node DOMElement
184+
* @param start number
185+
* @param end number
186+
*/
187+
setSelectionRange(node, start, end) {
188+
if (document.activeElement === node) {
189+
node.setSelectionRange(start, end);
190+
}
191+
}
192+
193+
180194
/**
181195
* onChange Event Handler
182196
* @param event

0 commit comments

Comments
 (0)