Skip to content

Commit

Permalink
ISSUE #4548 - fix ticket group rule value autofocus
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Daniel committed Jan 23, 2024
1 parent ebab5d2 commit 05f8fad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
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

0 comments on commit 05f8fad

Please sign in to comment.