Skip to content

Commit abca492

Browse files
Remove unsupported autoSelectNext date input feature
1 parent f38c7fe commit abca492

File tree

5 files changed

+1
-95
lines changed

5 files changed

+1
-95
lines changed

src/components/form-elements/date-input/DateInput.tsx

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export type DateInputChangeEvent = ChangeEvent<
2020
interface DateInputProps
2121
extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'defaultValue'>,
2222
FormElementProps {
23-
autoSelectNext?: boolean;
2423
value?: Partial<DateInputValue>;
2524
defaultValue?: Partial<DateInputValue>;
2625
onChange?: (e: DateInputChangeEvent) => void;
@@ -29,15 +28,12 @@ interface DateInputProps
2928
type InputType = 'day' | 'month' | 'year';
3029

3130
const DateInputComponent = ({
32-
autoSelectNext,
3331
children,
3432
onChange,
3533
value,
3634
defaultValue,
3735
...rest
3836
}: DateInputProps) => {
39-
let monthRef: HTMLInputElement | null = null;
40-
let yearRef: HTMLInputElement | null = null;
4137
const [internalDate, setInternalDate] = useState<Record<InputType, string>>({
4238
day: value?.day ?? '',
4339
month: value?.month ?? '',
@@ -54,17 +50,7 @@ const DateInputComponent = ({
5450
return setInternalDate(newState);
5551
}, [value]);
5652

57-
const handleFocusNextInput = (inputType: InputType, value: string): void => {
58-
if (!autoSelectNext) return;
59-
if (inputType === 'day' && value.length === 2 && monthRef) {
60-
monthRef.focus();
61-
} else if (inputType === 'month' && value.length === 2 && yearRef) {
62-
yearRef.focus();
63-
}
64-
};
65-
6653
const handleChange = (inputType: InputType, event: ChangeEvent<HTMLInputElement>): void => {
67-
handleFocusNextInput(inputType, event.target.value);
6854
event.stopPropagation();
6955

7056
const newEventValue: DateInputValue = {
@@ -82,26 +68,20 @@ const DateInputComponent = ({
8268
setInternalDate(newEventValue);
8369
};
8470

85-
const registerRef = (inputType: InputType, ref: HTMLInputElement | null): void => {
86-
if (inputType === 'month') monthRef = ref;
87-
if (inputType === 'year') yearRef = ref;
88-
};
89-
9071
return (
9172
<SingleInputFormGroup<Omit<DateInputProps, 'value' | 'defaultValue'>>
9273
inputType="dateinput"
9374
{...rest}
9475
>
9576
{/* eslint-disable-next-line @typescript-eslint/no-unused-vars */}
96-
{({ className, name, id, error, autoSelectNext, ...restRenderProps }) => {
77+
{({ className, name, id, error, ...restRenderProps }) => {
9778
const contextValue: IDateInputContext = {
9879
id,
9980
name,
10081
error,
10182
value,
10283
defaultValue,
10384
handleChange,
104-
registerRef,
10585
};
10686
return (
10787
<div className={classNames('nhsuk-date-input', className)} {...restRenderProps} id={id}>

src/components/form-elements/date-input/DateInputContext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ export type IDateInputContext = {
66
error: string | boolean | undefined;
77
value?: { day?: string; month?: string; year?: string };
88
defaultValue?: { day?: string; month?: string; year?: string };
9-
registerRef: (inputType: 'day' | 'month' | 'year', ref: null | HTMLInputElement) => void;
109
handleChange: (inputType: 'day' | 'month' | 'year', event: ChangeEvent<HTMLInputElement>) => void;
1110
};
1211

1312
const DateInputContext = createContext<IDateInputContext>({
1413
/* eslint-disable @typescript-eslint/no-empty-function */
1514
id: '',
1615
name: '',
17-
registerRef: () => {},
1816
handleChange: () => {},
1917
error: undefined,
2018
});

src/components/form-elements/date-input/__tests__/DateInput.test.tsx

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,6 @@ describe('DateInput', () => {
99
expect(container).toMatchSnapshot();
1010
});
1111

12-
it.each`
13-
autoSelectNext | inputValue | monthFocusExpected
14-
${false} | ${'1'} | ${false}
15-
${false} | ${'11'} | ${false}
16-
${true} | ${'1'} | ${false}
17-
${true} | ${'11'} | ${true}
18-
`(
19-
'When autoSelectNext is $autoSelectNext, the day input value is $inputValue, then month focus is expected to be $monthFocusExpected',
20-
({ autoSelectNext, inputValue, monthFocusExpected }) => {
21-
const { container } = render(
22-
<DateInput id="testInput" name="testInput" autoSelectNext={autoSelectNext} />,
23-
);
24-
25-
const dayInput = container.querySelector('#testInput-day')!;
26-
const monthInput = container.querySelector('#testInput-month')!;
27-
28-
expect(monthInput).not.toHaveFocus();
29-
30-
fireEvent.change(dayInput, { target: { value: inputValue } });
31-
32-
if (monthFocusExpected) {
33-
expect(monthInput).toHaveFocus();
34-
} else {
35-
expect(monthInput).not.toHaveFocus();
36-
}
37-
},
38-
);
39-
40-
it.each`
41-
autoSelectNext | inputValue | yearFocusExpected
42-
${false} | ${'1'} | ${false}
43-
${false} | ${'11'} | ${false}
44-
${true} | ${'1'} | ${false}
45-
${true} | ${'11'} | ${true}
46-
`(
47-
'When autoSelectNext is $autoSelectNext, the day input value is $inputValue, then year focus is expected to be $yearFocusExpected',
48-
({ autoSelectNext, inputValue, yearFocusExpected }) => {
49-
const { container } = render(
50-
<DateInput id="testInput" name="testInput" autoSelectNext={autoSelectNext} />,
51-
);
52-
53-
const monthInput = container.querySelector('#testInput-month')!;
54-
const yearInput = container.querySelector('#testInput-year')!;
55-
56-
expect(yearInput).not.toHaveFocus();
57-
58-
fireEvent.change(monthInput, { target: { value: inputValue } });
59-
60-
if (yearFocusExpected) {
61-
expect(yearInput).toHaveFocus();
62-
} else {
63-
expect(yearInput).not.toHaveFocus();
64-
}
65-
},
66-
);
67-
6812
it('Invokes the provided onChange function prop if provided', () => {
6913
let onChangeParam: DateInputChangeEvent | null = null;
7014
const onChange = jest.fn().mockImplementation((val) => (onChangeParam = val));

src/components/form-elements/date-input/components/IndividualDateInputs.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const IndividualDateInput: FC<IndividualDateInputProps> = ({
4141
value: ctxValue,
4242
defaultValue: ctxDefaultValue,
4343
handleChange: ctxHandleChange,
44-
registerRef,
4544
} = useContext<IDateInputContext>(DateInputContext);
4645

4746
const { className: labelClassName, ...restLabelProps } = labelProps || {};
@@ -61,7 +60,6 @@ const IndividualDateInput: FC<IndividualDateInputProps> = ({
6160
};
6261

6362
const refCallback = (ref: HTMLInputElement | null) => {
64-
registerRef(inputType, ref);
6563
if (inputRef) inputRef(ref);
6664
};
6765

stories/Form Elements/DateInput.stories.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,6 @@ export const StandardWithError: Story = {
6060
),
6161
};
6262

63-
export const StandardWithAutoSelect: Story = {
64-
render: () => (
65-
<div style={{ padding: 20 }}>
66-
<h2>Scenario: autoSelectNext prop is set to enable the auto focus functionality</h2>
67-
<h5>Expected Behaviour</h5>
68-
<ul className="nhsuk-hint">
69-
<li>When day field has 2 or month field has 2 characters, the next field is focused</li>
70-
</ul>
71-
<h5>Component</h5>
72-
<DateInput autoSelectNext />
73-
</div>
74-
),
75-
};
76-
7763
export const PrePopulatedIndividualComponents: Story = {
7864
render: () => {
7965
const defaultValue = { day: '20', month: '09', year: '1996' };

0 commit comments

Comments
 (0)