Skip to content

Commit

Permalink
fix: Sender.Header can not focus (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Nov 1, 2024
1 parent 6bddb3d commit 86787e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 24 additions & 0 deletions components/sender/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,28 @@ describe('Sender Component', () => {
expect(onSubmit).toHaveBeenCalledWith('bamboo');
});
});

it('Sender.Header can be focus', () => {
const { container } = render(
<Sender
header={
<Sender.Header open>
<input className="target" />
</Sender.Header>
}
/>,
);

const inputEle = container.querySelector<HTMLInputElement>('.target')!;
inputEle.focus();
expect(document.activeElement).toEqual(inputEle);

// Click on the header
fireEvent.mouseDown(container.querySelector('.ant-sender-header')!);
expect(document.activeElement).toEqual(inputEle);

// Click on the content
fireEvent.mouseDown(container.querySelector('.ant-sender-content')!);
expect(document.activeElement).toEqual(container.querySelector('textarea'));
});
});
5 changes: 2 additions & 3 deletions components/sender/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function Sender(props: SenderProps, ref: React.Ref<HTMLDivElement>) {
};

// ============================ Focus =============================
const onInternalMouseDown: React.MouseEventHandler<HTMLDivElement> = (e) => {
const onContentMouseDown: React.MouseEventHandler<HTMLDivElement> = (e) => {
// If input focused but click on the container,
// input will lose focus.
// We call `preventDefault` to prevent this behavior
Expand Down Expand Up @@ -250,14 +250,13 @@ function Sender(props: SenderProps, ref: React.Ref<HTMLDivElement>) {
ref={mergedContainerRef}
className={mergedCls}
style={{ ...contextConfig.style, ...style }}
onMouseDown={onInternalMouseDown}
>
{/* Header */}
{header && (
<SendHeaderContext.Provider value={{ prefixCls }}>{header}</SendHeaderContext.Provider>
)}

<div className={`${prefixCls}-content`}>
<div className={`${prefixCls}-content`} onMouseDown={onContentMouseDown}>
{/* Prefix */}
{prefix && (
<div
Expand Down

0 comments on commit 86787e6

Please sign in to comment.