Correctly query the primary button in a form (#32438)

The "primary button" is used at many places, but sometimes they might
conflict (due to button switch, hidden panel, dropdown menu, etc).

Sometimes we could add a special CSS class for the buttons, but
sometimes not (see the comment of QuickSubmit)

This PR introduces `querySingleVisibleElem` to help to get the correct
primary button (the only visible one), and prevent from querying the
wrong buttons.

Fix #32437

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
wxiaoguang 2024-11-07 04:21:53 +08:00 committed by GitHub
parent 41b4ef825d
commit b573512312
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 40 additions and 16 deletions

View file

@ -1,3 +1,5 @@
import {querySingleVisibleElem} from '../../utils/dom.ts';
export function handleGlobalEnterQuickSubmit(target) {
let form = target.closest('form');
if (form) {
@ -12,7 +14,11 @@ export function handleGlobalEnterQuickSubmit(target) {
}
form = target.closest('.ui.form');
if (form) {
form.querySelector('.ui.primary.button')?.click();
// A form should only have at most one "primary" button to do quick-submit.
// Here we don't use a special class to mark the primary button,
// because there could be a lot of forms with a primary button, the quick submit should work out-of-box,
// but not keeps asking developers to add that special class again and again (it could be forgotten easily)
querySingleVisibleElem<HTMLButtonElement>(form, '.ui.primary.button')?.click();
return true;
}
return false;