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 Gboard paste action when rises from the clipboard section of keypad #4641

Merged
merged 2 commits into from
Sep 5, 2023
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
11 changes: 11 additions & 0 deletions src/mantine-core/src/PinInput/PinInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ describe('@mantine/core/PinInput', () => {
await userEvent.keyboard('{Backspace}');
expect(container.querySelectorAll('.mantine-PinInput-input')[0]).toHaveFocus();
});

it('inputs will be filled when the value is bigger than 2 chars(ex: Gboard paste action from keypad)', async () => {
const spy = jest.fn();
const { container } = render(<PinInput {...defaultProps} onComplete={spy} length={6} />);

const element = container.querySelectorAll('.mantine-PinInput-input')[0];

const expectedValue = '123456';
fireEvent.change(element, { target: { value: expectedValue } });
expect(spy).toHaveBeenCalledWith(expectedValue);
});
});
20 changes: 12 additions & 8 deletions src/mantine-core/src/PinInput/PinInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,20 @@ export const PinInput = forwardRef<HTMLDivElement, PinInputProps>((props, ref) =

const handleChange = (event: React.ChangeEvent<HTMLInputElement>, index: number) => {
const inputValue = event.target.value;
const nextChar =
inputValue.length > 1 ? inputValue.split('')[inputValue.length - 1] : inputValue;
const nextCharOrValue =
inputValue.length === 2 ? inputValue.split('')[inputValue.length - 1] : inputValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not it break the case when we paste values from regular keyboard, for example when 1234 is pasted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the regular keyboard is fully handled by the onPaste event handler, logically it should not have any breaks.
I also tried it out using Ctrl + v and RightClick + paste that was ok too.


const isValid = validate(nextChar);
const isValid = validate(nextCharOrValue);

if (isValid) {
setFieldValue(nextChar, index);
focusInputField('next', index);
} else {
setFieldValue('', index);
if (nextCharOrValue.length < 2) {
if (isValid) {
setFieldValue(nextCharOrValue, index);
focusInputField('next', index);
} else {
setFieldValue('', index);
}
} else if (isValid) {
setValues(inputValue);
}
};

Expand Down