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

fix(PN-10282): implement pasteHandler for OTP #1159

Merged
merged 27 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
cdc8cd7
fix(PN-10282): fix paste OTP code in CodeInput component
SarahDonvito Mar 15, 2024
cc5344c
fix(PN-10282): add test on pasteHandler
SarahDonvito Mar 15, 2024
7ff4b73
Merge branch 'develop' of github.com:pagopa/pn-frontend into fix/PN-1…
SarahDonvito Mar 19, 2024
258d429
fix(PN-10282): change request - improve pasteHandler function and fi…
SarahDonvito Mar 20, 2024
b18d4bb
Merge branch 'develop' of github.com:pagopa/pn-frontend into fix/PN-1…
SarahDonvito Mar 20, 2024
bb02875
fix(PN-10282): change request
SarahDonvito Mar 20, 2024
2783364
Merge branch 'develop' of github.com:pagopa/pn-frontend into fix/PN-1…
SarahDonvito Mar 26, 2024
ec464f0
fix(PN-10282): wip on code input paste handler
SarahDonvito Mar 26, 2024
2040387
Merge branch 'develop' of github.com:pagopa/pn-frontend into fix/PN-1…
SarahDonvito Mar 26, 2024
363d3dd
fix(PN-10282): update ux when user pastes wrong code (longer, shorter…
SarahDonvito Mar 27, 2024
20c8a8c
fix(PN-10282): fix focus input when less than 5 char
SarahDonvito Mar 27, 2024
13912e2
fix(PN-10282): wip on tests
SarahDonvito Mar 27, 2024
f8ef93b
fix(PN-102828): wip on tests
SarahDonvito Mar 27, 2024
244a4bb
fix(PN-10282): implement tests on Code Input and Code Modal
SarahDonvito Mar 27, 2024
63d092c
fix(PN-10282): change request applied
SarahDonvito Mar 29, 2024
97b749c
Merge branch 'develop' of github.com:pagopa/pn-frontend into fix/PN-1…
SarahDonvito Mar 29, 2024
4240209
fix(PN-10282): fix tests, rename key for error message in case of let…
SarahDonvito Mar 29, 2024
7bbc83f
enforced check on code value inserted
AndreaCimini90 Mar 29, 2024
6493a30
added some comment
AndreaCimini90 Mar 29, 2024
9b11e50
fix(PN-10282): fix test and wip on fix bug on input 0 and backspace a…
SarahDonvito Mar 29, 2024
a6710c4
Merge branch 'fix/PN-10282' of github.com:pagopa/pn-frontend into fix…
SarahDonvito Mar 29, 2024
5541107
fix(PN-10282): change requests
SarahDonvito Mar 29, 2024
7b09ad9
fix(PN-10282): fix test
SarahDonvito Mar 29, 2024
9657172
fix(PN-10282): fix tests
SarahDonvito Mar 29, 2024
449c78e
Merge branch 'develop' of github.com:pagopa/pn-frontend into fix/PN-1…
SarahDonvito Mar 29, 2024
a386aa6
fix(PN-10282): update copy for error, fix change handler and wip on test
SarahDonvito Mar 29, 2024
25b19dd
fixed tests
AndreaCimini90 Apr 2, 2024
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
13 changes: 13 additions & 0 deletions packages/pn-commons/src/components/CodeModal/CodeInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ChangeEvent,
ClipboardEvent,
Fragment,
KeyboardEvent,
memo,
Expand Down Expand Up @@ -116,6 +117,17 @@ const CodeInput = ({ initialValues, isReadOnly, hasError, onChange }: Props) =>
}
};

const pasteHandler = (event: ClipboardEvent<HTMLDivElement>, index: number) => {
const copiedCode = event.clipboardData.getData('text');
if (Number(copiedCode)) {
const values = copiedCode.split('');
values.map((element, i) => {
changeInputValue(element, i);
focusInput(index + 1);
});
}
};

useEffect(() => {
onChange(currentValues);
}, [currentValues]);
Expand Down Expand Up @@ -143,6 +155,7 @@ const CodeInput = ({ initialValues, isReadOnly, hasError, onChange }: Props) =>
onKeyDown={(event) => keyDownHandler(event, index)}
onChange={(event) => changeHandler(event, index)}
onFocus={(event) => event.target.select()}
onPaste={(event) => pasteHandler(event, index)}
value={currentValues[index]}
// eslint-disable-next-line functional/immutable-data
inputRef={(node) => (inputsRef.current[index] = node)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,24 @@ describe('CodeInput Component', () => {
expect(codeInputs[0]).toBe(document.activeElement);
});
});

it('handles paste event', async () => {
// render component
const { getAllByTestId } = render(
<CodeInput initialValues={new Array(5).fill('')} onChange={handleChangeMock} />
);
const codeInputs = getAllByTestId(/code-input-[0-4]/);

// paste the value of the input and check that it is updated correctly
// set the cursor position to the beggining
act(() => (codeInputs[2] as HTMLInputElement).focus());
(codeInputs[2] as HTMLInputElement).setSelectionRange(0, 0);

// we must use userEvent because the keyboard event must trigger also the change event (fireEvent doesn't do that)
await userEvent.paste('12345');
await waitFor(() => {
expect(codeInputs[0]).toHaveValue('1');
expect(codeInputs[4]).toHaveValue('5');
});
});
});