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(TextInput): fix handleAdditionalContentClick handler #1558

Merged
merged 1 commit into from
May 2, 2024
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
6 changes: 4 additions & 2 deletions src/components/controls/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ export const TextInput = React.forwardRef<HTMLSpanElement, TextInputProps>(
};

const handleAdditionalContentClick: React.MouseEventHandler<HTMLDivElement> = (event) => {
const hasActiveElement = event.currentTarget.contains(document.activeElement);
const needActivateInput =
!event.currentTarget.contains(document.activeElement) &&
event.currentTarget.contains(event.target as HTMLElement);
const hasSelection = Boolean(document.getSelection()?.toString());

if (!hasActiveElement && !hasSelection) {
if (needActivateInput && !hasSelection) {
innerControlRef.current?.focus();
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';

import userEvent from '@testing-library/user-event';

import {render, screen} from '../../../../../test-utils/utils';
import {Button} from '../../../Button';
import {Sheet} from '../../../Sheet';
import {TextInput} from '../TextInput';

const TextInputWithButtonAndSheet = () => {
const [open, setOpen] = React.useState(false);
const startContent = (
<React.Fragment>
<Button onClick={() => setOpen(true)}>Open</Button>
<Sheet visible={open}>Sheet content</Sheet>
</React.Fragment>
);
return <TextInput startContent={startContent} />;
};

describe('TextInput additional content', () => {
test('TextInput should not be focused', async () => {
render(<TextInputWithButtonAndSheet />);
const user = userEvent.setup();
const button = await screen.findByText('Open');
await user.click(button);
const sheetContent = await screen.findByText('Sheet content');
await user.click(sheetContent);
const input = screen.getByRole('textbox');
expect(input).not.toHaveFocus();
});
});
Loading