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

ISSUE #4548 - Smart group value fields only get autofocus when adding a new field #4727

Merged
merged 1 commit into from
Jan 24, 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 @@ -100,6 +100,7 @@ export const GroupRulesForm = ({ onSave, onClose, rule, existingRules = [] }: IG
<InputsContainer>
<FormTextField
required
autoFocus
name="name"
label={formatMessage({ id: 'tickets.groups.filterPanel.name', defaultMessage: 'Name' })}
formError={errors.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { get } from 'lodash';
import { ListboxComponent } from './listboxComponent/listboxComponent.component';
import { Autocomplete } from './fieldValueInput.styles';

export const FieldValueInput = ({ name, disabled }) => {
export const FieldValueInput = ({ name, autoFocus = false, disabled }) => {
const { formState: { errors } } = useFormContext();
const fields = useSelector(selectMetaKeys);

Expand All @@ -49,7 +49,7 @@ export const FieldValueInput = ({ name, disabled }) => {
error={!!get(errors, name)}
InputProps={{
...InputProps,
autoFocus: true,
autoFocus,
}}
{...renderInputParams}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { useFormContext, useFieldArray } from 'react-hook-form';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import AddValueIcon from '@assets/icons/outlined/add_circle-outlined.svg';
import RemoveValueIcon from '@assets/icons/outlined/remove_circle-outlined.svg';
import { range } from 'lodash';
Expand All @@ -26,12 +26,16 @@ import { FieldValueInput } from './fieldValueInput/fieldValueInput.component';

export const RuleFieldValues = ({ disabled }) => {
const name = 'field.values';
const [autoFocus, setAutoFocus] = useState(false);
const { control, watch, setValue } = useFormContext<IFormRule>();
const { fields, append, remove } = useFieldArray({ control, name });

const operator = watch('field.operator');

const appendEmptyValue = () => append({ value: '' });
const appendEmptyValue = () => {
append({ value: '' });
setAutoFocus(!!fields.length);
};

useEffect(() => {
if (fields.length) return;
Expand All @@ -53,7 +57,7 @@ export const RuleFieldValues = ({ disabled }) => {
<>
{fields.map((field, i) => (
<ValuesContainer key={field.id}>
<FieldValueInput name={`field.values.${i}.value`} disabled={disabled} />
<FieldValueInput name={`field.values.${i}.value`} autoFocus={autoFocus} disabled={disabled} />
<ValueIconContainer onClick={() => remove(i)} disabled={disabled || fields.length === 1}>
<RemoveValueIcon />
</ValueIconContainer>
Expand Down
Loading