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

JNG-5422 rework autofocus #300

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public static String justifyContent(Flex flex) {
}

public static Boolean shouldElementHaveAutoFocus(VisualElement element) {
if (!element.getPageContainer().isForm()) {
return false;
}
Input input = findFirstInput(element.getPageContainer());

return input != null && element.getFQName().equals(input.getFQName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export const fadeTimeout: number = 1200;
export const slideTimeout: number = 300;

export const dialogStackCloseDelay: number = 50;

export const autoFocusRefDelay: number = 200;
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ export default function {{ containerComponentName container }}(props: {{ contain
const { service: customContainerHook } = useTrackService<{{ containerComponentName container }}ContainerHook>(`(${OBJECTCLASS}=${ {{~ camelCaseNameToInterfaceKey (containerComponentName container) }}_CONTAINER_ACTIONS_HOOK_INTERFACE_KEY})`);
const containerActions: {{ pageContainerActionDefinitionTypeName container }} = customContainerHook?.({{# unless container.table }}data, editMode, storeDiff{{/ unless }}) || {};
const actions = useMemo(() => ({...containerActions, ...pageActions}), [containerActions, pageActions]);
{{# if container.form }}
const autoFocusInputRef = useRef<any>(null);

useEffect(() => {
const timeout = setTimeout(() => {
if (typeof autoFocusInputRef?.current?.focus === 'function') {
autoFocusInputRef.current.focus();
}
}, autoFocusRefDelay);

return () => clearTimeout(timeout);
}, []);
{{/ if }}
{{/ unless }}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{/ if }}
<DatePicker
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
slotProps={ {
textField: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'JUDO-required': {{ boolValue child.attributeType.isRequired }},
}) }
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
slotProps={ {
textField: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
name="{{ child.attributeType.name }}"
id="{{ getXMIID child }}"
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
label={ t('{{ getTranslationKeyForVisualElement child }}', { defaultValue: '{{ child.label }}' }) as string }
value={ data.{{ child.attributeType.name }} || '' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
name="{{ child.attributeType.name }}"
id="{{ getXMIID child }}"
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
label={ t('{{ getTranslationKeyForVisualElement child }}', { defaultValue: '{{ child.label }}' }) as string }
customInput={TextField}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
name="{{ child.attributeType.name }}"
id="{{ getXMIID child }}"
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
label={ t('{{ getTranslationKeyForVisualElement child }}', { defaultValue: '{{ child.label }}' }) as string }
value={ data.{{ child.attributeType.name }} ?? '' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
name="{{ child.attributeType.name }}"
id="{{ getXMIID child }}"
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
label={ t('{{ getTranslationKeyForVisualElement child }}', { defaultValue: '{{ child.label }}' }) as string }
value={ data.{{ child.attributeType.name }} ?? '' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{/ if }}
<TimePicker
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus
inputRef={autoFocusInputRef}
{{/ if }}
ampm={false}
ampmInClock={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
<TrinaryLogicCombobox
name="{{ child.attributeType.name }}"
id="{{ getXMIID child }}"
{{# if (shouldElementHaveAutoFocus child) }}
autoFocus={true}
{{/ if }}
required={actions?.is{{ firstToUpper child.attributeType.name }}Required ? actions.is{{ firstToUpper child.attributeType.name }}Required(data, editMode) : ({{# if child.requiredBy }}data.{{ child.requiredBy.name }} ||{{/ if }} {{ boolValue child.attributeType.isRequired }})}
label={ t('{{ getTranslationKeyForVisualElement child }}', { defaultValue: '{{ child.label }}' }) as string }
value={ data?.{{ child.attributeType.name }} }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Dispatch, SetStateAction, FC } from 'react';
import { forwardRef, useEffect, useState, useCallback, useImperativeHandle, useMemo } from 'react';
import { forwardRef, useEffect, useState, useCallback, useImperativeHandle, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { LoadingButton } from '@mui/lab';
import type { JudoIdentifiable } from '~/services/data-api/common';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ import {
{{/ if }}
} from '~/components/widgets';
import { useConfirmationBeforeChange } from '~/hooks';
{{# if container.form }}
import { autoFocusRefDelay } from '~/config';
{{/ if }}
Loading