Skip to content

Commit

Permalink
fix(TextInput): fix handleAdditionalContentClick handler (#1558)
Browse files Browse the repository at this point in the history
  • Loading branch information
korvin89 authored May 2, 2024
1 parent 95fb990 commit 911624f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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();
});
});

0 comments on commit 911624f

Please sign in to comment.