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

Wrong cursor position on component update #260

Open
tomegz opened this issue Jan 8, 2019 · 4 comments
Open

Wrong cursor position on component update #260

tomegz opened this issue Jan 8, 2019 · 4 comments
Assignees
Labels

Comments

@tomegz
Copy link
Contributor

tomegz commented Jan 8, 2019

I'm using formatted number from onPhoneNumberChange to be value of the input. The cursor position is fine when formatted number is exactly the same as input, but when the formatted number is different than the value, then cursor is misplaced.

Live minimal example here: https://tomegz.github.io/react-intl-tel-input-cursor-example/

Code:

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      value: '',
      infoVisible: false,
    }
    this.onPhoneNumberChange = this.onPhoneNumberChange.bind(this)
  }

  onPhoneNumberChange(isValid, value, countryData, number) {
    this.setState({
      value: number,
      infoVisible: value !== number
    })
  }

  render() {
    const { value, infoVisible } = this.state
    return (
      <div className="container">
        <p>Type here:</p>
        <IntlTelInput
          css={['intl-tel-input', 'form-control']}
          value={value}
          onPhoneNumberChange={this.onPhoneNumberChange}
        />
        {infoVisible && <p style={{ position: 'absolute', color: 'red', top: '30px' }}>Cursor position just broke!</p>}
      </div>
    );
  }
}

I submitted #261 to fix this issue

tomegz pushed a commit to tomegz/react-intl-tel-input that referenced this issue Jan 8, 2019
@superhit0 superhit0 added the bug label Jan 8, 2019
@superhit0 superhit0 self-assigned this Jan 8, 2019
@hailuabk
Copy link

hailuabk commented Apr 29, 2019

I'm using formatted number from onPhoneNumberChange to be value of the input. The cursor position is fine when formatted number is exactly the same as input, but when the formatted number is different than the value, then cursor is misplaced.

Live minimal example here: https://tomegz.github.io/react-intl-tel-input-cursor-example/

Code:

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      value: '',
      infoVisible: false,
    }
    this.onPhoneNumberChange = this.onPhoneNumberChange.bind(this)
  }

  onPhoneNumberChange(isValid, value, countryData, number) {
    this.setState({
      value: number,
      infoVisible: value !== number
    })
  }

  render() {
    const { value, infoVisible } = this.state
    return (
      <div className="container">
        <p>Type here:</p>
        <IntlTelInput
          css={['intl-tel-input', 'form-control']}
          value={value}
          onPhoneNumberChange={this.onPhoneNumberChange}
        />
        {infoVisible && <p style={{ position: 'absolute', color: 'red', top: '30px' }}>Cursor position just broke!</p>}
      </div>
    );
  }
}

I submitted #261 to fix this issue

I got the same problem with you, how can I fix it?

@m-nathani
Copy link

This issue is still not fixed... please let us know if there is a solution for it ?

@erezcarmel
Copy link

I've found a temporary walkaround for this issue.
I'm setting the input cursor position on every typing using onPhoneNumberChange. The setSelectionRange must be used under setTimeout because the input event fires before the browser is moving the caret (I'm using Chrome).
Hope that helps.

const PhoneInput = () => {
	const [phone, setPhone] = useState('');

        const setCaretPosition = caretPos => {
                const elem = document.getElementsByClassName('phone-input')[0];

		if (elem) {
			elem.focus();
			setTimeout(() => {
				elem.setSelectionRange(caretPos, caretPos);
			}, 0);
		}
	};

	return <IntlTelInput
		containerClassName="intl-tel-input"
		inputClassName="phone-input"
		defaultCountry="us"
		autoPlaceholder={false}
		value={phone}
		onPhoneNumberChange={
			(status, value, countryData, number) => {
				if (/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/.test(number) || number === '') {
					setCaretPosition(number.length);
					setPhone(number);
				}
			}
		}
	/>
};

@SIRIUS-webkit
Copy link

I still have the problems can anyone help me ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants