From 25f2f61115318af9f8527b5114e83355bf543081 Mon Sep 17 00:00:00 2001 From: Hailuabk Date: Mon, 20 May 2019 10:19:41 +0700 Subject: [PATCH 1/6] Update cursor pointer When I typing in input, I got wrong cursor pointer position. I fixed it. --- src/components/IntlTelInput.js | 16 +++++++++++----- src/components/RootModal.js | 24 ++++++++++++++---------- src/components/TelInput.js | 2 -- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/components/IntlTelInput.js b/src/components/IntlTelInput.js index 03ec81a01..2cb528608 100644 --- a/src/components/IntlTelInput.js +++ b/src/components/IntlTelInput.js @@ -24,7 +24,7 @@ class IntlTelInput extends Component { }; } - if (prevState.disabled !== nextProps.disabled) { + if (nextProps.disabled && prevState.disabled !== nextProps.disabled) { newState = { disabled: nextProps.disabled, }; @@ -151,6 +151,9 @@ class IntlTelInput extends Component { componentDidUpdate(prevProps) { if (this.props.value !== prevProps.value) { this.updateFlagFromNumber(this.props.value); + this.updateCursorPosition( + (this.props.value || this.props.defaultValue).length + ); } if ( @@ -159,10 +162,6 @@ class IntlTelInput extends Component { ) { this.updatePlaceholder(this.props); } - - if (this.props.allowDropdown !== prevProps.allowDropdown) { - this.allowDropdown = this.props.allowDropdown; - } } componentWillUnmount() { @@ -684,6 +683,12 @@ class IntlTelInput extends Component { return number; }; + updateCursorPosition = cursorPosition => { + this.setState({ + cursorPosition + }); + }; + // update the input's value to the given val (format first if possible) // if doNotify is true, calls notifyPhoneNumberChange with the formatted value // NOTE: this is called from _setInitialState, handleUtils and setNumber @@ -999,6 +1004,7 @@ class IntlTelInput extends Component { }; generateMarkup = () => { + this.wrapperClass['allow-dropdown'] = this.allowDropdown; this.wrapperClass['separate-dial-code'] = this.props.separateDialCode; if (this.isMobile && this.props.useMobileFullscreenDropdown) { diff --git a/src/components/RootModal.js b/src/components/RootModal.js index 58aa98be4..11fc227e2 100644 --- a/src/components/RootModal.js +++ b/src/components/RootModal.js @@ -1,4 +1,4 @@ -import React, { Component, Fragment } from 'react'; +import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ReactDOM from 'react-dom'; @@ -7,25 +7,29 @@ export default class RootModal extends Component { children: PropTypes.node, }; - constructor(props) { - super(props); - + componentDidMount() { this.modalTarget = document.createElement('div'); this.modalTarget.className = 'intl-tel-input iti-container'; + document.body.appendChild(this.modalTarget); + this._render(); } - componentDidMount() { - document.body.appendChild(this.modalTarget); + shouldComponentUpdate() { + this._render(); + + return false; } componentWillUnmount() { + ReactDOM.unmountComponentAtNode(this.modalTarget); document.body.removeChild(this.modalTarget); } + _render() { + ReactDOM.render(
{this.props.children}
, this.modalTarget); + } + render() { - return ReactDOM.createPortal( - {this.props.children}, - this.modalTarget - ); + return