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

auto focus false not working #90

Open
Deboracgs opened this issue Mar 20, 2019 · 6 comments
Open

auto focus false not working #90

Deboracgs opened this issue Mar 20, 2019 · 6 comments

Comments

@Deboracgs
Copy link

Deboracgs commented Mar 20, 2019

Auto focus false not working, when i click in other input, scrolling automaticaly to currencyInput, any solution ?

https://media.giphy.com/media/1mssFMXlG3mORJt03j/giphy.gif

import React, { Component } from 'react';
import CurrencyInput from 'react-currency-input';
class InputMoneyComponent extends Component {

    constructor(props){
        super(props);

        this.input;
    }

    handleChange(event){
        event.preventDefault();
        let value = event.target.value;
        this.props.onChange(value);
    };


    render() {
        const { classes } = this.props;
        return (
            <CurrencyInput 
                className={"form-control"}
                value={this.props.value}
                disabled={this.props.disabled}
                onChangeEvent={this.handleChange.bind(this)}
                thousandSeparator={"."}
                decimalSeparator={","}
                allowNegative={false}
                ref={ref => this.input = ref }
                inputType={"text"}
                autoFocus={false}
            />
        );
    }
}
export default (InputMoneyComponent);

@peterblunden-egrowcery
Copy link

I'm seeing this issue too on safari iOS 12 (ipad). Seems to be something to do with componentDidMount and componentDidUpdate because if i override those methods then the problem goes away. Could it be this line causing the input to receive focus?

node.setSelectionRange(selectionStart, selectionEnd);

@0xicingdeath
Copy link

I am running into the same issue with using this component on Safari. However, overriding componentDidMount and componentDidUpdate didn't work for me. Still experiencing the same problem with the focus.

@peterblunden-egrowcery
Copy link

Here's my override code that got it to work

// Fix autofocus issues with CurrencyInput
// on iOS it will still auto focus even if autoFocus=false
// see https://github.com/jsillitoe/react-currency-input/issues/90
let componentDidMount_super = CurrencyInput.prototype.componentDidMount;
CurrencyInput.prototype.componentDidMount = function() {
    if(!this.props.autoFocus) {
        let setSelectionRange_super = this.theInput.setSelectionRange;
        this.theInput.setSelectionRange = () => {};
        componentDidMount_super.call(this, ...arguments);
        this.theInput.setSelectionRange = setSelectionRange_super;
    }
    else {
        componentDidMount_super.call(this, ...arguments);
    }
};
let componentDidUpdate_super = CurrencyInput.prototype.componentDidUpdate;
CurrencyInput.prototype.componentDidUpdate = function() {
    if(!this.props.autoFocus) {
        let setSelectionRange_super = this.theInput.setSelectionRange;
        this.theInput.setSelectionRange = () => {};
        componentDidUpdate_super.call(this, ...arguments);
        this.theInput.setSelectionRange = setSelectionRange_super;
    }
    else {
        componentDidMount_super.call(this, ...arguments);
    }
};

@efibe
Copy link

efibe commented Dec 4, 2019

Here's my override code that got it to work

This broke the backspace key when there is a suffix enabled.

@efibe
Copy link

efibe commented Dec 4, 2019

This is fixed in #74 but still not published to npm

I made this small hack to get it working right now:

let componentDidMount_super = CurrencyInput.prototype.componentDidMount; CurrencyInput.prototype.componentDidMount = function() { this.theInput.setSelectionRange_super = this.theInput.setSelectionRange; this.theInput.setSelectionRange = (start, end) => { if (document.activeElement === this.theInput) { this.theInput.setSelectionRange_super(start, end); } }; componentDidMount_super.call(this, ...arguments); };

edit: I couldn't format this comment to save my life.

@WesJourdan
Copy link

This drove me mad. How is this still not published to npm?

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

No branches or pull requests

5 participants