Skip to content

Commit

Permalink
ERM-3386, On New agreement the agreement name field should have focus (
Browse files Browse the repository at this point in the history
…#1360)

* ERM-3386, On New agreement the agreement name field should have focus

* fix: Fix for startDate no longer grabbing focus

Proposed fix removed the focus being applied when a new period is added. This solution differentiates between bootstrapped startDate ('') and added startDates (undefined) to work out when to grab focus

ERM-3386

---------

Co-authored-by: Ethan Freestone <[email protected]>
  • Loading branch information
MonireRasouli and EthanFreestone authored Oct 21, 2024
1 parent ea68cc7 commit 150bf02
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import { get } from 'lodash';

import { FormattedMessage, useIntl } from 'react-intl';
import { Field } from 'react-final-form';
import { Field, useFormState } from 'react-final-form';

import get from 'lodash/get';

import {
AppValidatedDatepicker,
Expand All @@ -29,12 +31,14 @@ const AgreementPeriodField = ({ index, input: { name } }) => {
const dateFormat = getLocaleDateFormat({ intl });
const backendDateStandard = 'YYYY-MM-DD';

const { values } = useFormState();

useEffect(() => {
const value = get(startDateInputRef, 'current.value');
if ((value === '' || value === undefined) && get(startDateInputRef, 'current')) {
const startDateValue = get(values, `${name}.startDate`);
if (startDateValue === undefined && startDateInputRef?.current) {
startDateInputRef.current.focus();
}
}, [startDateInputRef]);
}, [name, startDateInputRef, values]);

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const AgreementPeriodsFieldArray = ({
>
{renderPeriods()}
</div>
{/* Initial startDate is '', but all subsequent should be undefined to ensure we can grab focus */}
<Button id="add-period-button" onClick={() => onAddField()}>
<FormattedMessage id="ui-agreements.agreementPeriods.addPeriod" />
</Button>
Expand Down
8 changes: 7 additions & 1 deletion src/routes/AgreementCreateRoute/AgreementCreateRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ const AgreementCreateRoute = ({
};

const getInitialValues = () => {
const periods = [{}];
/*
* We initialise startDate to an empty string
* so that we can differentiate the always
* present period from those added by the user.
* This allows us to focus them as we please.
*/
const periods = [{ startDate: '' }];
let items;

// if authority && referenceId exist we can assume the call came from eholdings
Expand Down

0 comments on commit 150bf02

Please sign in to comment.