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

InputNumber: wrong selection after ajax #9458

Closed
SirError opened this issue Nov 30, 2022 · 7 comments · Fixed by #9466
Closed

InputNumber: wrong selection after ajax #9458

SirError opened this issue Nov 30, 2022 · 7 comments · Fixed by #9466
Assignees
Labels
enhancement Additional functionality to current component workaround A workaround has been provided
Milestone

Comments

@SirError
Copy link

Describe the bug

If the inputNumber is updated by an ajax call before getting focus, and the value is bigger than the old value,
just part of the new value is selected

This image shows what happens
image

This other shows what shoul happen
image

Reproducer

primefaces-test-master.zip

1 - open http://localhost:8080/primefaces-test/
2 - type 10 on first inputNumber
3 - tab and type 1000 on the second inputNumber
4 - tab again, the third input will receive the value 10000.00 but only the first three digits will be selected instead of the whole value.

Expected behavior

the whole value should be selected.

PrimeFaces edition

Community

PrimeFaces version

12.0.0

Theme

No response

JSF implementation

Mojarra

JSF version

any

Java version

11

Browser(s)

any

@SirError SirError added ‼️ needs-triage Issue needs triaging 🐞 defect Bug...Something isn't working labels Nov 30, 2022
@melloware
Copy link
Member

Not sure I want to fix this because this is a race between your AJAX update and the tab key and AutoNumeric but here is a nice little workaround using AJAX oncomplete

<h:form>
	<p:inputNumber id="value" value="#{testView.number}">
		<p:ajax update="value3" listener="#{testView.calc()}"/>
	</p:inputNumber>
	<p:inputNumber id="value2" value="#{testView.number2}">
		<p:ajax update="value3" listener="#{testView.calc()}" oncomplete="PF('wgtValue3').input.select();"/>
	</p:inputNumber>
	<p:inputNumber id="value3" widgetVar="wgtValue3" value="#{testView.number3}"/>
</h:form>

@melloware melloware added workaround A workaround has been provided third-party Issue is tracked in 3rd party library and removed 🐞 defect Bug...Something isn't working ‼️ needs-triage Issue needs triaging labels Nov 30, 2022
@melloware
Copy link
Member

I marked this 3rd party as I actually believe its AutoNumeric that automatically selects all the text when you tab in. so its selecting the range of 3 then your AJAX update wipes it out and its still only has the range select of 3

@SirError
Copy link
Author

SirError commented Dec 1, 2022

Hi @melloware , is there some way of inject some autonumeric configuration through Primefaces or some JS file?
reading autonumeric's docs, I think I could solve it globally with these options:

{
    caretPositionOnFocus: AutoNumeric.options.caretPositionOnFocus.start,
    selectOnFocus: AutoNumeric.options.selectOnFocus.doNotSelect
}

@melloware
Copy link
Member

Let me take a look I think I can give you something!

@melloware
Copy link
Member

OK here is a working project I added this monkeyPatch to test.js.
pf-9458.zip

if (PrimeFaces.widget.InputNumber) {
    PrimeFaces.widget.InputNumber.prototype.init = function(cfg) {
        PrimeFaces.widget.BaseWidget.prototype.init.call(this, cfg);
        this.input = $(this.jqId + '_input');
        this.hiddenInput = $(this.jqId + '_hinput');
        this.plugOptArray = cfg.pluginOptions;
        this.valueToRender = cfg.valueToRender;
        this.disabled = cfg.disabled;

        // GitHub #8125 minValue>0 shows js warning and quirky behavior
        if (this.cfg.minimumValue > 0.0000001 || this.cfg.maximumValue < 0) {
            this.cfg.overrideMinMaxLimits = 'invalid';
        }

        //bind events if not disabled
        if (this.disabled) {
            this.input.attr("disabled", "disabled");
            this.input.addClass("ui-state-disabled");
            this.hiddenInput.attr("disabled", "disabled");
        }

        //Visual effects
        PrimeFaces.skinInput(this.input);

        this.wrapEvents();
        this.bindInputEvents();

        this.cfg.caretPositionOnFocus = AutoNumeric.options.caretPositionOnFocus.start;
        this.cfg.selectOnFocus = AutoNumeric.options.selectOnFocus.doNotSelect;

        this.autonumeric = new AutoNumeric(this.jqId + '_input', this.cfg);

        if (this.valueToRender !== "") {
            //set the value to the input the plugin will format it.
            this.autonumeric.set(this.valueToRender);
            // GitHub #6940 blur firing too many change events
            this.autonumeric.rawValueOnFocus = this.valueToRender;
        }

        this.setValueToHiddenInput(this.getValue());

        //pfs metadata
        this.input.data(PrimeFaces.CLIENT_ID_DATA, this.id);
        this.hiddenInput.data(PrimeFaces.CLIENT_ID_DATA, this.id);
    }
}

@melloware
Copy link
Member

I think I need to add these two props to the component for the future if someone else wants to set these.

melloware added a commit to melloware/primefaces that referenced this issue Dec 2, 2022
@melloware melloware self-assigned this Dec 2, 2022
@melloware melloware added this to the 13.0.0 milestone Dec 2, 2022
@melloware melloware added enhancement Additional functionality to current component and removed third-party Issue is tracked in 3rd party library labels Dec 2, 2022
@melloware
Copy link
Member

in 13.0 you will be able to do this selectOnFocus="false" caretPositionOnFocus="start"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additional functionality to current component workaround A workaround has been provided
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants