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

Add option to show stepper buttons in number fields only when focused #7468

Open
TatuLund opened this issue Jun 4, 2024 · 0 comments
Open
Labels
enhancement New feature or request vaadin-number-field workaround There is a workaround in the comments.

Comments

@TatuLund
Copy link
Contributor

TatuLund commented Jun 4, 2024

Describe your motivation

When using number fields with mobile devices, there could be confusion with what field is focused and use of the stepper buttons. Hence it would be preferable that stepper buttons appear only when field is focused, and disappear when blurred. This is also related to native popup soft keyboard behavior.

This should be configurable option, e.g. attribute value. Naturally Java API needed too, for example something like follows could replace the boolean version of the function:

field.setStepButtonsVisible(StepButtonsVisible.AUTO); // Visible when focused
field.setStepButtonsVisible(StepButtonsVisible.HIDDEN); // Hide fully
field.setStepButtonsVisible(StepButtonsVisible.VISIBLE); // Always visible

Describe the solution you'd like

It is possible to create workaround by following JavaScript

        IntegerField field = new IntegerField("Integer");
        field.setStepButtonsVisible(false);
        field.getElement().executeJs(
                """
                        const field = this;
                        var buttonPressed = false;
                        field.addEventListener('focus', (e) => { 
                          if (!this.getAttribute('step-buttons-visible')) {
                            buttonPressed = false;
                            this.setAttribute('step-buttons-visible','');
                          }
                        });
                        const container = this.shadowRoot.querySelector('vaadin-input-container');
                        for (let i=0;i<container.children.length;i++) {
                            if (container.children[i].getAttribute('part')
                                    && container.children[i].getAttribute('part').endsWith('button')) {
                                container.children[i].addEventListener('click', (e) => {
                                   buttonPressed = true;
                                });
                            }
                        };
                        field.addEventListener('blur', (e) => {
                          setTimeout(() => {  
                            if (!buttonPressed) {
                              field.removeAttribute('step-buttons-visible');
                            }
                          }, 200);
                        });
                                """);

Describe alternatives you've considered

No response

Additional context

No response

@TatuLund TatuLund added enhancement New feature or request workaround There is a workaround in the comments. labels Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vaadin-number-field workaround There is a workaround in the comments.
Projects
None yet
Development

No branches or pull requests

2 participants