Skip to content
5 changes: 4 additions & 1 deletion src/frontend/src/components/forms/StandaloneField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export function StandaloneField({
fieldName = 'field',
defaultValue,
hideLabels,
error
error,
selectAndFocus = false
}: Readonly<{
fieldDefinition: ApiFormFieldType;
fieldName?: string;
defaultValue?: any;
hideLabels?: boolean;
error?: string;
selectAndFocus?: boolean;
}>) {
// Field must have a defined name
const name = useMemo(() => fieldName ?? 'field', [fieldName]);
Expand Down Expand Up @@ -49,6 +51,7 @@ export function StandaloneField({
control={form.control}
hideLabels={hideLabels}
setFields={undefined}
selectAndFocus={selectAndFocus}
/>
</FormProvider>
);
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/src/components/forms/fields/ApiFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function ApiFormField({
hideLabels,
url,
setFields,
onKeyDown
onKeyDown,
selectAndFocus = false
}: Readonly<{
fieldName: string;
definition: ApiFormFieldType;
Expand All @@ -37,6 +38,7 @@ export function ApiFormField({
url?: string;
setFields?: React.Dispatch<React.SetStateAction<ApiFormFieldSet>>;
onKeyDown?: (value: any) => void;
selectAndFocus?: boolean;
}>) {
const fieldId = useId();
const controller = useController({
Expand Down Expand Up @@ -191,6 +193,7 @@ export function ApiFormField({
onChange={(value: any) => {
onChange(value);
}}
selectAndFocus={selectAndFocus}
/>
);
case 'choice':
Expand Down
25 changes: 22 additions & 3 deletions src/frontend/src/components/forms/fields/NumberField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NumberInput } from '@mantine/core';
import { useId, useMemo } from 'react';
import { useEffect, useId, useMemo, useRef } from 'react';
import type { FieldValues, UseControllerReturn } from 'react-hook-form';
import AutoFillRightSection, { AutoFillWarning } from './AutoFillRightSection';

Expand All @@ -14,7 +14,8 @@ export default function NumberField({
placeholderAutofill,
placeholderWarningCompare,
placeholderWarning,
onChange
onChange,
selectAndFocus = false
}: Readonly<{
controller: UseControllerReturn<FieldValues, any>;
definition: any;
Expand All @@ -23,8 +24,11 @@ export default function NumberField({
placeholderWarningCompare?: number | string;
placeholderWarning?: string;
onChange: (value: any) => void;
selectAndFocus?: boolean;
}>) {
const fieldId = useId();
const inputRef = useRef<HTMLInputElement | null>(null);

const {
field,
fieldState: { error }
Expand Down Expand Up @@ -60,6 +64,18 @@ export default function NumberField({
return val;
}, [definition.field_type, value]);

useEffect(() => {
if (!selectAndFocus) return;
if (!inputRef.current) return;

inputRef.current.focus();
requestAnimationFrame(() => {
const el = inputRef.current;
if (!el) return;
el.setSelectionRange(0, el.value.length);
});
}, [selectAndFocus]);

const rightSection = useMemo(() => {
if (
definition.placeholder &&
Expand Down Expand Up @@ -101,7 +117,10 @@ export default function NumberField({
<NumberInput
{...definition}
radius={'sm'}
ref={field.ref}
ref={(node) => {
inputRef.current = node;
field.ref(node);
}}
id={fieldId}
aria-label={`number-field-${field.name}`}
error={definition.error ?? error?.message}
Expand Down
47 changes: 42 additions & 5 deletions src/frontend/src/forms/StockForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@
setMax?: boolean;
merge?: boolean;
record?: any;
focusAndSelect?: boolean;

Check warning on line 560 in src/frontend/src/forms/StockForms.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'focusAndSelect' PropType is defined but prop is never used

See more on https://sonarcloud.io/project/issues?id=inventree_InvenTree&issues=AZ5gx8dH7TqawLdB-rUS&open=AZ5gx8dH7TqawLdB-rUS&pullRequest=11539
}) {
const statusOptions: ApiFormFieldChoice[] = useMemo(() => {
return (
Expand Down Expand Up @@ -659,6 +660,7 @@
<Table.Td>
<StandaloneField
fieldName='quantity'
selectAndFocus={true}
fieldDefinition={{
field_type: 'number',
value: quantity,
Expand Down Expand Up @@ -1215,7 +1217,8 @@
title,
preFormContent,
successMessage,
modalFunc = useCreateApiFormModal
modalFunc = useCreateApiFormModal,
onOpen
}: {
items?: object;
pk?: number;
Expand All @@ -1228,6 +1231,7 @@
preFormContent?: JSX.Element;
successMessage?: string;
modalFunc?: apiModalFunc;
onOpen?: () => void;
}) {
const baseParams: any = {
part_detail: true,
Expand Down Expand Up @@ -1284,7 +1288,10 @@
successMessage: successMessage,
onFormSuccess: () => refresh(),
onClose: () => setOpened(false),
onOpen: () => setOpened(true)
onOpen: () => {
setOpened(true);
onOpen?.();
}
});
}

Expand All @@ -1299,7 +1306,17 @@
<Alert color='blue'>
{t`Increase the quantity of the selected stock items by a given amount.`}
</Alert>
)
),
onOpen: () => {
setTimeout(() => {
const input = document.querySelector<HTMLInputElement>(
'#number-field-quantity, input[aria-label="number-field-quantity"]'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that is the best way to solve it as i can see these label changing in the future but I also do not have a better proposal; thoughts @SchrodingersGat ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a bit of an anti-pattern to me - we should be able to set it with normal properties, not trying to introspect the DOM

);

input?.focus();
input?.select();
}, 0);
}
});
}

Expand All @@ -1314,7 +1331,17 @@
<Alert color='blue'>
{t`Decrease the quantity of the selected stock items by a given amount.`}
</Alert>
)
),
onOpen: () => {
setTimeout(() => {
const input = document.querySelector<HTMLInputElement>(
'input[aria-label="number-field-quantity"]'
);

input?.focus();
input?.select();
}, 0);
}
});
}

Expand Down Expand Up @@ -1359,7 +1386,17 @@
<Alert color='blue'>
{t`Count the selected stock items, and adjust the quantity accordingly.`}
</Alert>
)
),
onOpen: () => {
setTimeout(() => {
const input = document.querySelector<HTMLInputElement>(
'input[aria-label="number-field-quantity"]'
);

input?.focus();
input?.select();
}, 0);
}
});
}

Expand Down
Loading