Skip to content

Commit

Permalink
FIX: Default action was not executed after pressing enter on empty mo…
Browse files Browse the repository at this point in the history
…dal window (#2532)
  • Loading branch information
JindrichSusen committed Mar 14, 2024
1 parent 923b029 commit 834c728
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
@@ -0,0 +1,25 @@
/*
Copyright 2005 - 2024 Advantage Solutions, s. r. o.
This file is part of ORIGAM (http://www.origam.org).
ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/

.root {
width: 0;
height: 0;
padding: 0;
border: none;
}
@@ -0,0 +1,52 @@
/*
Copyright 2005 - 2024 Advantage Solutions, s. r. o.
This file is part of ORIGAM (http://www.origam.org).
ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useEffect, useRef } from "react";
import { getFormFocusManager } from "model/selectors/DataView/getFormFocusManager";
import { getDataView } from "model/selectors/DataView/getDataView";
import uiActions from "model/actions-ui-tree";
import S from "gui/Workbench/ScreenArea/FormView/BackupFocusPlaceHolder.module.scss";


// This components should get focus if there are no other focusable inputs on the screen section.
// This will ensure that the focus does not stay where we don't expect it to be. For example on
// the parent screen after opening a model window.
export const BackupFocusPlaceHolder = (props: { ctx: any; }) => {
const refInput = useRef<HTMLInputElement>(null);
useEffect(() => {
const focusManager = getFormFocusManager(getDataView(props.ctx));
focusManager.setBackupFocusPlaceHolder(refInput.current);
}, []);

return (
<input
className={S.root}
onKeyDown={(event) => {
if (event.key === "Enter") {
const dataView = getDataView(props.ctx);
if (dataView.firstEnabledDefaultAction) {
uiActions.actions.onActionClick(dataView.firstEnabledDefaultAction)(
event,
dataView.firstEnabledDefaultAction
);
}
}
}}
ref={refInput} />);
};
Expand Up @@ -43,6 +43,7 @@ import { getRowStateForegroundColor } from "model/selectors/RowState/getRowState
import { dimensionsFromProperty, dimensionsFromXmlNode } from "gui/Components/Form/FieldDimensions";
import { findStrings } from "xmlInterpreters/xmlUtils";
import { TabIndex } from "model/entities/TabIndexOwner";
import { BackupFocusPlaceHolder } from "gui/Workbench/ScreenArea/FormView/BackupFocusPlaceHolder";


@inject(({dataView}) => {
Expand Down Expand Up @@ -92,6 +93,7 @@ export class FormBuilder extends React.Component<{
style={{backgroundColor}}
>
{xfo.elements.map((child: any) => recursive(child))}
<BackupFocusPlaceHolder ctx={self.props.dataView}/>
</FormRoot>
);
} else if (xfo.name === "FormElement" && xfo.attributes.Type === "FormSection") {
Expand Down
14 changes: 13 additions & 1 deletion frontend-html/src/model/entities/FormFocusManager.ts
Expand Up @@ -24,6 +24,7 @@ import { getDataView } from "model/selectors/DataView/getDataView";

export class FormFocusManager {
autoFocusDisabled = false;
backupFocusPlaceholder: HTMLInputElement | undefined | null;

private isFormPerspectiveActive(){
const dataView = getDataView(this.parent);
Expand Down Expand Up @@ -61,6 +62,10 @@ export class FormFocusManager {
this.focusableContainers = this.focusableContainers.sort(compareTabIndexOwners);
}

setBackupFocusPlaceHolder(input: HTMLInputElement | null) {
this.backupFocusPlaceholder = input
}

focus(name: string) {
let focusable = this.focusableContainers.find((container) => container.name === name)?.focusable;
this.focusAndRemember(focusable);
Expand All @@ -83,6 +88,12 @@ export class FormFocusManager {
}

forceAutoFocus() {
if (this.focusableContainers.length === 0 && this.backupFocusPlaceholder) {
setTimeout(() => {
this.backupFocusPlaceholder?.focus();
}, 0);
return;
}
const focusable = this.focusableContainers[0].focusable;
if (focusable.disabled) {
// (focusable as any).readOnly returns always false => readonly fields cannot be skipped
Expand All @@ -95,7 +106,8 @@ export class FormFocusManager {
}

autoFocus() {
if (this.focusableContainers.length === 0 || this.autoFocusDisabled || isGlobalAutoFocusDisabled(this.parent)) {
const nothingToFocus = this.focusableContainers.length === 0 && !this.backupFocusPlaceholder;
if (nothingToFocus || this.autoFocusDisabled || isGlobalAutoFocusDisabled(this.parent)) {
return;
}
this.forceAutoFocus();
Expand Down
2 changes: 1 addition & 1 deletion frontend-html/yarn.lock
Expand Up @@ -4653,7 +4653,7 @@ react-highlight-words@^0.20.0:
memoize-one "^4.0.0"
prop-types "^15.5.8"

react-inlinesvg@*, react-inlinesvg@^4.0.3, react-inlinesvg@^4.1.3:
react-inlinesvg@*, react-inlinesvg@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/react-inlinesvg/-/react-inlinesvg-4.1.3.tgz#671f083fd78934ec58afa5128460252dcae61177"
integrity sha512-p1+wkr1UQZyLw/3bdpnHO3v3tMNVWyxWnAEY6ML/Ql9ldDYTBTy6HqAyNl7u3au925XPffLMiXKnQrqZeJAldw==
Expand Down

0 comments on commit 834c728

Please sign in to comment.