Skip to content
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
40 changes: 40 additions & 0 deletions src/Dom/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,43 @@ export function limitTabRange(node: HTMLElement, e: KeyboardEvent) {
}
}
}

export interface InputFocusOptions extends FocusOptions {
cursor?: 'start' | 'end' | 'all';
}

// Used for `rc-input` `rc-textarea` `rc-input-number`
/**
* Focus element and set cursor position for input/textarea elements.
*/
export function triggerFocus(
element?: HTMLElement,
option?: InputFocusOptions,
) {
if (!element) return;

element.focus(option);

// Selection content
const { cursor } = option || {};
if (
cursor &&
(element instanceof HTMLInputElement ||
element instanceof HTMLTextAreaElement)
) {
const len = element.value.length;

switch (cursor) {
case 'start':
element.setSelectionRange(0, 0);
break;

case 'end':
element.setSelectionRange(len, len);
break;

default:
element.setSelectionRange(0, len);
}
Comment on lines +126 to +137

Choose a reason for hiding this comment

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

medium

switch 语句使用 default 来处理 cursor'all' 的情况。虽然功能上是正确的,但这不够明确。如果未来 cursor 的类型增加了新的选项,它们会意外地执行 default 的逻辑(即全选文本),这可能不是期望的行为。建议明确地处理 'all' 情况,让代码更具可读性和健壮性。

    switch (cursor) {
      case 'start':
        element.setSelectionRange(0, 0);
        break;

      case 'end':
        element.setSelectionRange(len, len);
        break;

      case 'all':
        element.setSelectionRange(0, len);
        break;
    }

}
}
27 changes: 26 additions & 1 deletion tests/focus.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable class-methods-use-this */
import { spyElementPrototype } from '../src/test/domHook';
import { getFocusNodeList } from '../src/Dom/focus';
import { getFocusNodeList, triggerFocus } from '../src/Dom/focus';

describe('focus', () => {
beforeAll(() => {
Expand Down Expand Up @@ -31,4 +31,29 @@ describe('focus', () => {
const tabFocusList = getFocusNodeList(div, true);
expect(tabFocusList).toHaveLength(5);
});

it('triggerFocus should set cursor position for textarea', () => {
const textarea = document.createElement('textarea');
textarea.value = 'test content';

const focusSpy = jest.spyOn(textarea, 'focus');
const setSelectionRangeSpy = jest.spyOn(textarea, 'setSelectionRange');

// Test cursor: 'start'
triggerFocus(textarea, { cursor: 'start' });
expect(setSelectionRangeSpy).toHaveBeenCalledWith(0, 0);

// Test cursor: 'end'
triggerFocus(textarea, { cursor: 'end' });
expect(setSelectionRangeSpy).toHaveBeenCalledWith(12, 12); // 'test content'.length = 12

// Test cursor: 'all'
triggerFocus(textarea, { cursor: 'all' });
expect(setSelectionRangeSpy).toHaveBeenCalledWith(0, 12); // select all text

expect(focusSpy).toHaveBeenCalledTimes(3);

focusSpy.mockRestore();
setSelectionRangeSpy.mockRestore();
});
});
Loading