Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cursor Issue#205 #221

Merged
merged 1 commit into from
Jul 7, 2018
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
26 changes: 26 additions & 0 deletions __tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,30 @@ describe('utils', () => {

expect(utils.findIndex(array, predicate)).toEqual(-1);
});

it('getCursorPositionAfterFormating', () => {
let previousStringBeforeCursor = '9123';
let previousString = '912345';
let nextString = '912345';

expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(4);

previousStringBeforeCursor = '0912 345';
previousString = '0912 345 678';
nextString = '91234678';

expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(5);

previousStringBeforeCursor = '91234';
previousString = '91234678';
nextString = '0912 345 678';

expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(7);

previousStringBeforeCursor = '(201) 5';
previousString = '(201) 55-01';
nextString = '201-5501';

expect(utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousString, nextString)).toEqual(5);
});
});
16 changes: 14 additions & 2 deletions src/components/IntlTelInputApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class IntlTelInputApp extends Component {
title: '',
countryCode: 'us',
dialCode: '',
cursorPosition: (props.value || props.defaultValue).length,
};

this.selectedCountryData = {};
Expand Down Expand Up @@ -1055,14 +1056,24 @@ class IntlTelInputApp extends Component {
// Either notify phoneNumber changed if component is controlled
// or udpate the state and notify change if component is uncontrolled
handleInputChange(e) {
let cursorPosition = e.target.selectionStart;
const previousValue = e.target.value;
const previousStringBeforeCursor = previousValue === '' ? previousValue : previousValue.substring(0, cursorPosition);
const value = this.props.format ? this.formatNumber(e.target.value) : e.target.value;

cursorPosition = utils.getCursorPositionAfterFormating(previousStringBeforeCursor, previousValue, value);

if (this.props.value !== undefined) {
this.updateFlagFromNumber(value);
this.notifyPhoneNumberChange(value);
this.setState({
cursorPosition,
}, () => {
this.updateFlagFromNumber(value);
this.notifyPhoneNumberChange(value);
});
} else {
this.setState({
value,
cursorPosition,
}, () => {
this.updateFlagFromNumber(value);
this.notifyPhoneNumberChange(value);
Expand Down Expand Up @@ -1170,6 +1181,7 @@ class IntlTelInputApp extends Component {
autoFocus={ this.props.autoFocus }
autoComplete={ this.props.autoComplete }
inputProps={ this.props.telInputProps }
cursorPosition={ this.state.cursorPosition }
/>
</div>
);
Expand Down
18 changes: 17 additions & 1 deletion src/components/TelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';

class TelInput extends Component {
constructor(props) {
super(props);

this.refHandler = this.refHandler.bind(this);
}

componentDidUpdate() {
this.tel.setSelectionRange(this.props.cursorPosition, this.props.cursorPosition);
}

refHandler(element) {
this.tel = element;
this.props.refCallback(element);
}

render() {
return (
<input
{ ...this.props.inputProps }
ref={ this.props.refCallback }
ref={ this.refHandler }
type="tel"
autoComplete={ this.props.autoComplete }
className={ this.props.className }
Expand Down Expand Up @@ -38,6 +53,7 @@ TelInput.propTypes = {
autoComplete: PropTypes.string,
inputProps: PropTypes.object, // eslint-disable-line react/forbid-prop-types
refCallback: PropTypes.func.isRequired,
cursorPosition: PropTypes.number,
};

export default TelInput;
40 changes: 40 additions & 0 deletions src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,44 @@ export default {

return index;
},

// Get the location of cursor after formatting is done on the phone number
getCursorPositionAfterFormating(prevBeforeCursor, prev, next) {
if (prev === next) {
return prevBeforeCursor.length;
}
let cursorShift = 0;

if (prev.length > next.length) {
for (let i = 0, j = 0; i < prevBeforeCursor.length && j < next.length; i += 1) {
if (prevBeforeCursor[i] !== next[j]) {
if (isNaN(next[j]) && !isNaN(prevBeforeCursor[i])) {
i -= 1;
j += 1;
cursorShift += 1;
} else {
cursorShift -= 1;
}
} else {
j += 1;
}
}
} else {
for (let i = 0, j = 0; i < prevBeforeCursor.length && j < next.length; j += 1) {
if (prevBeforeCursor[i] !== next[j]) {
if (isNaN(prevBeforeCursor[i]) && !isNaN(next[j])) {
j -= 1;
i += 1;
cursorShift -= 1;
} else {
cursorShift += 1;
}
} else {
i += 1;
}
}
}

return prevBeforeCursor.length + cursorShift;
}
};